8000 Fix match crash on unions including tuples by ilevkivskyi · Pull Request #13514 · python/mypy · GitHub
[go: up one dir, main page]

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 1 commit
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
Next Next commit
Fix match crash on unions including tuples
  • Loading branch information
ilevkivskyi committed Aug 25, 2022
commit c14e108a07e816507e2455437ab27c0df8e4739b
6 changes: 6 additions & 0 deletions 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 @@ -645,6 +646,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 +661,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
26 changes: 26 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1600,3 +1600,29 @@ 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):
...
[builtins fixtures/tuple.pyi]

[case testMatchTupleOptionalNoCrash]
# flags: --strict-optional
foo: tuple[int] | None
match foo:
case x,: pass
[builtins fixtures/tuple.pyi]

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

match var:
case (42, a):
...
case ("yes", b):
...
[builtins fixtures/tuple.pyi]
0