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

Correctly narrow types for tuple[type[X], ...] #15691

Merged
merged 3 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7118,6 +7118,8 @@ def flatten_types(t: Type) -> list[Type]:
t = get_proper_type(t)
if isinstance(t, TupleType):
return [b for a in t.items for b in flatten_types(a)]
elif is_named_instance(t, "builtins.tuple"):
return [t.args[0]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it correct to use all args in former case but just first arg in second case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because tuple has only one arg. FWIW the only real question here is whether we need to recurse, i.e. also support:

  • tuple[tuple[type[A], type[B]], ...]
  • tuple[tuple[type[A], ...], ...]

But TBH I don't really care, because it would be a corner case to already rare special case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've decided not to, because this can be quite complex / slow with no real users asking for it.

else:
return [t]

Expand Down
47 changes: 47 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -1268,3 +1268,50 @@ def g() -> None:
def foo(): ...
foo()
[builtins fixtures/dict.pyi]


[case testNarrowingWithTupleOfTypes]
from typing import Tuple, Type

class Base: ...

class Impl1(Base): ...
class Impl2(Base): ...

impls: Tuple[Type[Base], ...] = (Impl1, Impl2)
some: object

if isinstance(some, impls):
reveal_type(some) # N: Revealed type is "__main__.Base"
else:
reveal_type(some) # N: Revealed type is "builtins.object"

raw: Tuple[type, ...]
if isinstance(some, raw):
reveal_type(some) # N: Revealed type is "builtins.object"
else:
reveal_type(some) # N: Revealed type is "builtins.object"
[builtins fixtures/dict.pyi]


[case testNarrowingWithTupleOfTypesPy30Plus]
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
# flags: --python-version 3.10
class Base: ...

class Impl1(Base): ...
class Impl2(Base): ...

impls: tuple[type[Base], ...] = (Impl1, Impl2)
some: int | Base
sobolevn marked this conversation as resolved.
Show resolved Hide resolved

sobolevn marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(some, impls):
reveal_type(some) # N: Revealed type is "__main__.Base"
else:
reveal_type(some) # N: Revealed type is "Union[builtins.int, __main__.Base]"

raw: tuple[type, ...]
if isinstance(some, raw):
reveal_type(some) # N: Revealed type is "Union[builtins.int, __main__.Base]"
else:
reveal_type(some) # N: Revealed type is "Union[builtins.int, __main__.Base]"
[builtins fixtures/dict.pyi]