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

Merged
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
6 changes: 6 additions & 0 deletions mypy/semanal_typeargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def visit_type_alias_type(self, t: TypeAliasType) -> None:
# If there was already an error for the alias itself, there is no point in checking
# the expansion, most likely it will result in the same kind of error.
get_proper_type(t).accept(self)
if t.alias is not None:
t.alias.accept(self)
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a duplicate work, did you try deleting the accept just above? (if you delete it, don't forget to update the comment)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That breaks one test:

[case testValidTypeAliasValues]
from typing import TypeVar, Generic, List

T = TypeVar("T", int, str)
S = TypeVar("S", int, bytes)

class C(Generic[T]): ...
class D(C[S]): ...  # E: Invalid type argument value for "C"

U = TypeVar("U")
A = List[C[U]]
x: A[bytes]  # E: Value of type variable "T" of "C" cannot be "bytes"

V = TypeVar("V", bound=int)
class E(Generic[V]): ...
B = List[E[U]]
y: B[str]  # E: Type argument "str" of "E" must be a subtype of "int"

Removing this line makes both errors on alias instantiation disappear. That's quite expected since get_proper_type calls _expand_once, so it is not equivalent to simply accepting the target alias - we do not want to ignore args here. We can limit that to only accepting if args are non-empty, but I don't think this can result in any significant speedup.

Copy link
Member

Choose a reason for hiding this comment

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

OK, this is kind of unfortunate. We already accept args in isolation at the very start (see super call), then we accept expansion, and finally the target in isolation. But I guess this is the price to pay for having both:

  • Support for unrestricted type vars in alias definitions
  • Doing some in-place patching in this visitor


def visit_tuple_type(self, t: TupleType) -> None:
t.items = flatten_nested_tuples(t.items)
Expand Down Expand Up @@ -254,6 +256,10 @@ def visit_unpack_type(self, typ: UnpackType) -> None:
def check_type_var_values(
self, name: str, actuals: list[Type], arg_name: str, valids: list[Type], context: Context
) -> bool:
if self.in_type_alias_expr:
# See testValidTypeAliasValues - we do not enforce typevar compatibility
# at the definition site. We check instantiation validity later.
return False
is_error = False
for actual in get_proper_types(actuals):
# We skip UnboundType here, since they may appear in defn.bases,
Expand Down
27 changes: 27 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -2666,3 +2666,30 @@ def identity(smth: _FT) -> _FT:
class S(tuple[Unpack[Ts]], Generic[T, Unpack[Ts]]):
def f(self, x: T, /) -> T: ...
[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[()]]]): ...

a: A
reveal_type(tuple(a)) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
(x,) = a

b: B
(_,) = b # E: Need more than 0 values to unpack (1 expected)
[builtins fixtures/tuple.pyi]

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

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

a: A
tuple(a)
(x,) = a
(_,) = a
[builtins fixtures/tuple.pyi]
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy