python / python/mypy

Generic default type in `type` revealed as `Any`

Open
#21,608 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-pep-696
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy reveals a certain nested default generic type in type as Any, while other type checkers reveal it as the default type.

This issue is motivated by pandas-dev/pandas-stubs#1764.

To Reproduce

This is complicated, see https://github.com/python/mypy/issues/21608#issuecomment-4692751812 for a much simplified example.

from typing import Any, Generic, TypeVar, reveal_type

_ItemT_co = TypeVar("_ItemT_co", default=Any, covariant=True)

class generic(Generic[_ItemT_co]): ...

_ScalarT_co = TypeVar("_ScalarT_co", bound=generic, default=Any, covariant=True)

class dtype(Generic[_ScalarT_co]): ...

_DT64ItemT_co = TypeVar(
    "_DT64ItemT_co", bound=int | None, default=int | None, covariant=True
)

class datetime64(generic[_DT64ItemT_co], Generic[_DT64ItemT_co]): ...

GenericT_co = TypeVar("GenericT_co", bound=generic, default=Any, covariant=True)
GenericT = TypeVar("GenericT", bound=generic, default=Any)

class Series(Generic[GenericT_co]):
    def to_numpy_simplified(
        self, dtype: dtype[GenericT] | type[GenericT]
    ) -> GenericT: ...

reveal_type(Series().to_numpy_simplified(datetime64))  # mypy: datetime64[Any], pyright & pyright: datetime64[int | None]

Expected Behavior

datetime64[int | None]

Actual Behavior

datetime64[Any]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimized Python reproduction in the issue and run mypy to confirm that reveal_type reports datetime64[Any]. Compare the result with the expected datetime64[int | None] from the linked pyright examples; done means mypy resolves the generic default type consistently with that expectation and regression coverage is added.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.