Skip to content

Use normalized tuples for fallback calculation #19111

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion mypy/semanal_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
TypeVarLikeType,
TypeVarTupleType,
UnpackType,
flatten_nested_tuples,
get_proper_type,
)

Expand Down Expand Up @@ -290,7 +291,7 @@ def calculate_tuple_fallback(typ: TupleType) -> None:
fallback = typ.partial_fallback
assert fallback.type.fullname == "builtins.tuple"
items = []
for item in typ.items:
for item in flatten_nested_tuples(typ.items):
# TODO: this duplicates some logic in typeops.tuple_fallback().
if isinstance(item, UnpackType):
unpacked_type = get_proper_type(item.type)
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -2628,3 +2628,11 @@ def fn(f: Callable[[*tuple[T]], int]) -> Callable[[*tuple[T]], int]: ...
def test(*args: Unpack[tuple[T]]) -> int: ...
reveal_type(fn(test)) # N: Revealed type is "def [T] (T`1) -> builtins.int"
[builtins fixtures/tuple.pyi]

[case testNoCrashSubclassingTUpleWithTrivialUnpack]
# https://github.com/python/mypy/issues/19105
from typing import Unpack

class A(tuple[Unpack[tuple[int]]]): ...
Copy link
Member

Choose a reason for hiding this comment

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

Could you please also add a test for

class C(tuple[Unpack[tuple[int, ...]]]): ...

(unless there is already such test case)? I am not sure we properly handle such types. Also maybe test not just class creation, but some tuple ops as well, such as e.g. unpacking etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Surprisingly we don't have such a test too, but it was already working before my patch - *tuple[int, ...] is not a trivial unpack. But your feeling is overall correct - I'm able to make it crash with

a: A
tuple(a)

and also get a terrible diagnostic on

b: B
_, = b  # E: Variadic tuple unpacking requires a star target

despite B clearly not being variadic. I'll move this to draft for now, there's little benefit from merging a crash fix that causes a next one immediately...

class B(tuple[Unpack[tuple[()]]]): ...
[builtins fixtures/tuple.pyi]