Skip to content

Generic default type in type revealed as Any #21608

Open

Description

@cmp0xff

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 #21608 (comment) 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]

Activity

  1. JelleZijlstra commented on Jun 12, 2026

    @JelleZijlstra
    Member

    Simpler repro case:

    from typing import Any, Generic, TypeVar, reveal_type
    
    _DT64ItemT_co = TypeVar("_DT64ItemT_co", default=int | None)
    
    class datetime64(Generic[_DT64ItemT_co]): ...
    
    def f[T](x: type[T]) -> T:
        raise Exception
    
    reveal_type(f(datetime64)) # ty, pyright: datetime64[int | None], mypy: datetime64[Any]
  2. added a commit that references this issue on Jun 12, 2026
    110baf3
  3. changed the title [-]Nested generic default type revealed as `Any`[/-] [+]Generic default type in `type` revealed as `Any`[/+] on Jun 12, 2026
  4. added a commit that references this issue on Jun 12, 2026
    6255df5
  5. ilevkivskyi commented on Jun 14, 2026

    @ilevkivskyi
    Member

    Isn't this a duplicate of #20354?

    That said, it is not obvious that the current behavior is wrong.

  6. added a commit that references this issue on Aug 28, 2026
    503dace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-696TypeVar defaults

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions