Skip to content

Fix match crash on unions including tuples #13514

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
merged 2 commits into from
Aug 25, 2022
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
10 changes: 9 additions & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
coerce_to_literal,
make_simplified_union,
try_getting_str_literals_from_type,
tuple_fallback,
)
from mypy.types import (
AnyType,
Expand Down Expand Up @@ -325,7 +326,9 @@ def get_sequence_type(self, t: Type) -> Type | None:
else:
return None

if self.chk.type_is_iterable(t) and isinstance(t, Instance):
if self.chk.type_is_iterable(t) and isinstance(t, (Instance, TupleType)):
if isinstance(t, TupleType):
t = tuple_fallback(t)
return self.chk.iterable_item_type(t)
else:
return None
Expand Down Expand Up @@ -645,6 +648,9 @@ def construct_sequence_child(self, outer_type: Type, inner_type: Type) -> Type:

For example:
construct_sequence_child(List[int], str) = List[str]

TODO: this doesn't make sense. For example if one has class S(Sequence[int], Generic[T])
or class T(Sequence[Tuple[T, T]]), there is no way any of those can map to Sequence[str].
"""
proper_type = get_proper_type(outer_type)
if isinstance(proper_type, UnionType):
Expand All @@ -657,6 +663,8 @@ def construct_sequence_child(self, outer_type: Type, inner_type: Type) -> Type:
sequence = self.chk.named_generic_type("typing.Sequence", [inner_type])
if is_subtype(outer_type, self.chk.named_type("typing.Sequence")):
proper_type = get_proper_type(outer_type)
if isinstance(proper_type, TupleType):
proper_type = tuple_fallback(proper_type)
assert isinstance(proper_type, Instance)
empty_type = fill_typevars(proper_type.type)
partial_type = expand_type_by_instance(empty_type, sequence)
Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1600,3 +1600,31 @@ def foo(x: NoneType): # E: NoneType should not be used as a type, please use Non
reveal_type(x) # N: Revealed type is "None"

[builtins fixtures/tuple.pyi]

[case testMatchTupleInstanceUnionNoCrash]
from typing import Union

def func(e: Union[str, tuple[str]]) -> None:
match e:
case (a,) if isinstance(a, str):
reveal_type(a) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]

[case testMatchTupleOptionalNoCrash]
# flags: --strict-optional
foo: tuple[int] | None
match foo:
case x,:
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testMatchUnionTwoTuplesNoCrash]
var: tuple[int, int] | tuple[str, str]

# TODO: we can infer better here.
match var:
case (42, a):
reveal_type(a) # N: Revealed type is "Union[builtins.int, builtins.str]"
case ("yes", b):
reveal_type(b) # N: Revealed type is "Union[builtins.int, builtins.str]"
[builtins fixtures/tuple.pyi]