-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Labels
bugmypy got something wrongmypy got something wrongtopic-pep-585PEP 585 (builtin generics)PEP 585 (builtin generics)topic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder
Description
The following difference is due to Mypy handling typing.Type via TypeType, for which it does not try to create an intersection, and builtins.type as an Instance, for which it does:
# flags: --python-version 3.9 --warn-unreachable
from typing import Type
t1: Type
t2: type
class C: ...
if isinstance(t1, C):
reveal_type(t1) # E: Statement is unreachable
if isinstance(t2, C):
reveal_type(t2) # N: Revealed type is "__main__.<subclass of "type" and "C">"I encountered this problem when working on #16330, which affects type checking a sphinx function.
Interestingly, things are consistent when annotating explicitly with Any:
# flags: --python-version 3.9 --warn-unreachable
from typing import Any, Type
t1: Type[Any]
t2: type[Any]
class C: ...
if isinstance(t1, C):
reveal_type(t1) # E: Statement is unreachable
if isinstance(t2, C):
reveal_type(t2) # E: Statement is unreachableSo, Mypy maybe misunderstands t2: type?
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-pep-585PEP 585 (builtin generics)PEP 585 (builtin generics)topic-type-narrowingConditional type narrowing / binderConditional type narrowing / binder