Generic default type in `type` revealed as `Any`
Nobody has claimed this yet.
- 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]
- https://gist.github.com/mypy-play/099705d9f12556f66776949caa6f0450
- https://pyright-play.net/?strict=true&code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCCKcANFAOICmKlISAxmQCqKUBqAhiGSJQG6UOAGwD68BJQCwAKBkiAkjEoQmI%2BmCgBeKCwmcQACgBECpSrVgjZACaVgHAK5CYmoqSjq%2BXJBxQumIA6UAJQyMvRCHADOUVBo1LQMBlQ0dPQA2qbKquoAusEAXFAAdKVh0iIAyvTCXDka2rrsXMZVNZEg9VZQAEZgDijWmvGpDDZ2js6uxGSe3r7%2BgSHlEdGx1uKUyQlpmdW1nRb5RaXF5SIAIkwAbAAsitkWWjqs%2BgYyUJ9QJld3D%2Bbqbp9AZDVD4AA%2BUAAcmAaON7E4XGCoJCYXCPGAvHQFpoAkEZKFZNJVjEoNYOEoYFhKHcDCNEhlLjd7mZ6rkyCkGZlfizHnlCiUykTOWl6s8mm8jCKGF0yMDBsMdmMyRNEdN3HNsX5cUtCdL6Exxa8WlKlQagf0FfS0vDJi43ITwpFSZVEpQottRhl9WzCh8vrZgLAwCIUA5sHARFEsAghEhgEhKNZ3tIvmmoFFKEJgDZNkUNqx0j7cijYIXi-7PsEoABaAB8FDNTBOQpkvAEwjErAMrro7oMwWKMBDYYjUZjcYTSYM5Mp1LuwUJQA
- https://play.ty.dev/8c070d59-a962-4cd0-823f-fa8dd9941f4f
Expected Behavior
datetime64[int | None]
Actual Behavior
datetime64[Any]
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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