Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent narrowing for builtins.type and typing.Type #16349

Open
tyralla opened this issue Oct 28, 2023 · 3 comments · May be fixed by #16366
Open

Inconsistent narrowing for builtins.type and typing.Type #16349

tyralla opened this issue Oct 28, 2023 · 3 comments · May be fixed by #16366
Labels
bug mypy got something wrong topic-pep-585 PEP 585 (builtin generics) topic-type-narrowing Conditional type narrowing / binder

Comments

@tyralla
Copy link
Contributor

tyralla commented Oct 28, 2023

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 unreachable

So, Mypy maybe misunderstands t2: type?

@tyralla tyralla added the bug mypy got something wrong label Oct 28, 2023
@AlexWaygood AlexWaygood added topic-pep-585 PEP 585 (builtin generics) topic-type-narrowing Conditional type narrowing / binder labels Oct 28, 2023
@tyralla tyralla linked a pull request Oct 29, 2023 that will close this issue
@JelleZijlstra
Copy link
Member

@tyralla
Copy link
Contributor Author

tyralla commented Oct 30, 2023

So, it's not 100% clear that type should be treated as type[Any]. (If I understand correctly, the alternative would be type[object].) Okay, then I wait to finish #16366 until this decision is final.

@tyralla
Copy link
Contributor Author

tyralla commented Nov 14, 2023

@JelleZijlstra

As the discussion came to a halt and it was only a little work, I completed the pull request. I think it agrees with your and Guido's thinking about how plain type annotations should be understood. Would you like to review it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-585 PEP 585 (builtin generics) topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants