Skip to content
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]