8000 Fix type inference in pattern matching by positional argument by sobolevn · Pull Request #13618 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix type inference in pattern matching by positional argument #13618

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
Sep 7, 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
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