Skip to content

Commit

Permalink
Fix type inference in pattern matching by positional argument (#13618)
Browse files Browse the repository at this point in the history
Oh, this was very interesting.
During the debug sessions I learned a lot about how pattern matching and
its analysis do work.

But, the problem was that `expand_type` did not preserve
`.last_known_value` for some reason.
I used `.copy_modified` to preserve everything we know about `Instance`.

However, I expect that some tests might fail now. This code even has
something similar in `TODO` some lines above:

https://github.com/python/mypy/blob/88aed94ae3de2542491f6cd65d1236c4f0cdedb1/mypy/expandtype.py#L144-L148

Let's see what will happen.

Closes #13612
  • Loading branch information
sobolevn committed Sep 7, 2022
1 parent 80dfb36 commit b031f1c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/expandtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def visit_erased_type(self, t: ErasedType) -> Type:
def visit_instance(self, t: Instance) -> Type:
args = self.expand_types_with_unpack(list(t.args))
if isinstance(args, list):
return Instance(t.type, args, t.line, t.column)
return t.copy_modified(args=args)
else:
return args

Expand Down
67 changes: 67 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -1628,3 +1628,70 @@ match var:
case ("yes", b):
reveal_type(b) # N: Revealed type is "Union[builtins.int, builtins.str]"
[builtins fixtures/tuple.pyi]

[case testMatchNamedAndKeywordsAreTheSame]
from typing import Generic, TypeVar, Union
from typing_extensions import Final
from dataclasses import dataclass

T = TypeVar("T")

class Regular:
x: str
y: int
__match_args__ = ("x",)
class ReveresedOrder:
x: int
y: str
__match_args__ = ("y",)
class GenericRegular(Generic[T]):
x: T
__match_args__ = ("x",)
class GenericWithFinal(Generic[T]):
x: T
__match_args__: Final = ("x",)
class RegularSubtype(GenericRegular[str]): ...

@dataclass
class GenericDataclass(Generic[T]):
x: T

input_arg: Union[
Regular,
ReveresedOrder,
GenericRegular[str],
GenericWithFinal[str],
RegularSubtype,
GenericDataclass[str],
]

# Positional:
match input_arg:
case Regular(a):
reveal_type(a) # N: Revealed type is "builtins.str"
case ReveresedOrder(a):
reveal_type(a) # N: Revealed type is "builtins.str"
case GenericWithFinal(a):
reveal_type(a) # N: Revealed type is "builtins.str"
case RegularSubtype(a):
reveal_type(a) # N: Revealed type is "builtins.str"
case GenericRegular(a):
reveal_type(a) # N: Revealed type is "builtins.str"
case GenericDataclass(a):
reveal_type(a) # N: Revealed type is "builtins.str"

# Keywords:
match input_arg:
case Regular(x=a):
reveal_type(a) # N: Revealed type is "builtins.str"
case ReveresedOrder(x=b): # Order is different
reveal_type(b) # N: Revealed type is "builtins.int"
case GenericWithFinal(x=a):
reveal_type(a) # N: Revealed type is "builtins.str"
case RegularSubtype(x=a):
reveal_type(a) # N: Revealed type is "builtins.str"
case GenericRegular(x=a):
reveal_type(a) # N: Revealed type is "builtins.str"
case GenericDataclass(x=a):
reveal_type(a) # N: Revealed type is "builtins.str"
[builtins fixtures/dataclasses.pyi]

0 comments on commit b031f1c

Please sign in to comment.