Skip to content

Generic default type in type revealed as Any #21608

@cmp0xff

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-696TypeVar defaults
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions