FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix crash on unhandled meet variadic tuple vs indtance by ilevkivskyi · Pull Request #21558 · python/mypy · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .test  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
23 changes: 22 additions & 1 deletion mypy/meet.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,28 @@ def visit_tuple_type(self, t: TupleType) -> ProperType:
elif isinstance(self.s, Instance):
# meet(Tuple[t1, t2, <...>], Tuple[s, ...]) == Tuple[meet(t1, s), meet(t2, s), <...>].
if self.s.type.fullname in TUPLE_LIKE_INSTANCE_NAMES and self.s.args:
return t.copy_modified(items=[meet_types(it, self.s.args[0]) for it in t.items])
arg = self.s.args[0]
new_items: list[Type] = []
for it in t.items:
# Unpack items need to be handled by the caller.
if isinstance(it, UnpackType):
unpacked = get_proper_type(it.type)
if isinstance(unpacked, TypeVarTupleType):
# We can't infer anything in this case.
new_arg = UninhabitedType()
instance = unpacked.tuple_fallback
else:
assert (
isinstance(unpacked, Instance)
and unpacked.type.fullname == "builtins.tuple"
)
new_arg = meet_types(unpacked.args[0], arg)
instance = unpacked
new_items.append(UnpackType(instance.copy_modified(args=[new_arg])))
else:
# All other items can be processed in a regular way.
new_items.append(meet_types(it, arg))
return t.copy_modified(items=new_items)
elif is_proper_subtype(t, self.s):
# A named tuple that inherits from a normal class
return t
Expand Down
3 changes: 1 addition & 2 deletions test-data/unit/check-typeis.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ def test5(t: tuple[A | B, ...]) -> None:

def test6(t: tuple[B, Unpack[tuple[A | B, ...]], B]) -> None:
if is_tuple_of_B(t):
# Should this be tuple[B, *tuple[B, ...], B]
reveal_type(t) # N: Revealed type is "tuple[__main__.B, Never, __main__.B]"
reveal_type(t) # N: Revealed type is "tuple[__main__.B, Unpack[builtins.tuple[__main__.B, ...]], __main__.B]"
else:
reveal_type(t) # N: Revealed type is "tuple[__main__.B, Unpack[builtins.tuple[__main__.A | __main__.B, ...]], __main__.B]"
[builtins fixtures/tuple.pyi]
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-typevar-tuple.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2873,3 +2873,15 @@ reveal_type(x) # N: Revealed type is "builtins.list[tuple[()]]"
reveal_type(y) # N: Revealed type is "builtins.list[tuple[()]]"
reveal_type(z) # N: Revealed type is "builtins.list[tuple[()]]"
[builtins fixtures/tuple.pyi]

[case testMeetTupleWithUnpackVsInstanceWorks_no_verbose_reveal]
from typing import TypeVar, Unpack, Union

K = TypeVar("K")
Prefix = tuple[Union[K, list[int]], ...]

def g(x: K) -> None:
d: Union[Prefix[K], tuple[Unpack[Prefix[int]], int], None] = None
assert d is not None
reveal_type(d) # N: Revealed type is "tuple[K | list[int], ...] | tuple[*tuple[int | list[int], ...], int]"
[builtins fixtures/tuple.pyi]
Loading

Back | FazBrowse Home | New Git URL