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
9 changes: 7 additions & 2 deletions mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
return self.early_non_match()
elif isinstance(p_typ, FunctionLike) and p_typ.is_type_obj():
typ = fill_typevars_with_any(p_typ.type_object())
type_range = TypeRange(typ, is_upper_bound=False)
elif (
isinstance(type_info, Var)
and type_info.type is not None
Expand All @@ -578,8 +579,10 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
fallback = self.chk.named_type("builtins.function")
any_type = AnyType(TypeOfAny.unannotated)
typ = callable_with_ellipsis(any_type, ret_type=any_type, fallback=fallback)
elif isinstance(p_typ, TypeType) and isinstance(p_typ.item, NoneType):
type_range = TypeRange(typ, is_upper_bound=False)
elif isinstance(p_typ, TypeType):
typ = p_typ.item
type_range = TypeRange(p_typ.item, is_upper_bound=True)
elif not isinstance(p_typ, AnyType):
self.msg.fail(
message_registry.CLASS_PATTERN_TYPE_REQUIRED.format(
Expand All @@ -588,9 +591,11 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
o,
)
return self.early_non_match()
else:
type_range = get_type_range(typ)

new_type, rest_type = self.chk.conditional_types_with_intersection(
current_type, [get_type_range(typ)], o, default=current_type
current_type, [type_range], o, default=current_type
)
if is_uninhabited(new_type):
return self.early_non_match()
Expand Down
87 changes: 83 additions & 4 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ match x:
case [str()]:
pass

[case testMatchSequencePatternWithInvalidClassPattern]
[case testMatchSequencePatternWithTypeObject]
# flags: --strict-equality --warn-unreachable
class Example:
__match_args__ = ("value",)
def __init__(self, value: str) -> None:
Expand All @@ -353,10 +354,10 @@ class Example:
SubClass: type[Example]

match [SubClass("a"), SubClass("b")]:
case [SubClass(value), *rest]: # E: Expected type in class pattern; found "type[__main__.Example]"
reveal_type(value) # E: Cannot determine type of "value" \
# N: Revealed type is "Any"
case [SubClass(value), *rest]:
reveal_type(value) # N: Revealed type is "builtins.str"
reveal_type(rest) # N: Revealed type is "builtins.list[__main__.Example]"

[builtins fixtures/tuple.pyi]

# Narrowing union-based values via a literal pattern on an indexed/attribute subject
Expand Down Expand Up @@ -1257,6 +1258,84 @@ reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(z) # N: Revealed type is "builtins.int"
[builtins fixtures/dict-full.pyi]

[case testMatchClassPatternTypeObject]
# flags: --strict-equality --warn-unreachable
class Example:
__match_args__ = ("value",)
def __init__(self, value: str) -> None:
self.value = value

def f1(subclass: type[Example]) -> None:
match subclass("a"):
case Example(value):
reveal_type(value) # N: Revealed type is "builtins.str"
case anything:
reveal_type(anything) # E: Statement is unreachable

def f2(subclass: type[Example]) -> None:
match Example("a"):
case subclass(value):
reveal_type(value) # N: Revealed type is "builtins.str"
case anything:
reveal_type(anything) # N: Revealed type is "__main__.Example"

def f3(subclass: type[Example]) -> None:
match subclass("a"):
case subclass(value):
reveal_type(value) # N: Revealed type is "builtins.str"
case anything:
reveal_type(anything) # N: Revealed type is "__main__.Example"

class Example2:
__match_args__ = ("value",)
def __init__(self, value: str) -> None:
self.value = value

def f4(T: type[Example | Example2]) -> None:
match T("a"):
case Example(value):
reveal_type(value) # N: Revealed type is "builtins.str"
case anything:
reveal_type(anything) # N: Revealed type is "__main__.Example2"

def f5(T: type[Example | Example2]) -> None:
match Example("a"):
case T(value): # E: Expected type in class pattern; found "type[__main__.Example] | type[__main__.Example2]"
reveal_type(value) # E: Statement is unreachable
case anything:
reveal_type(anything) # N: Revealed type is "__main__.Example"

def f6(T: type[Example | Example2]) -> None:
match T("a"):
case T(value): # E: Expected type in class pattern; found "type[__main__.Example] | type[__main__.Example2]"
reveal_type(value) # E: Statement is unreachable
case anything:
reveal_type(anything) # N: Revealed type is "__main__.Example | __main__.Example2"

def f7(m: object, t: type[object]) -> None:
match m:
case t():
reveal_type(m) # N: Revealed type is "builtins.object"
case _:
reveal_type(m) # N: Revealed type is "builtins.object"
[builtins fixtures/tuple.pyi]

[case testMatchClassPatternTypeObjectGeneric]
# flags: --strict-equality --warn-unreachable
from typing import TypeVar
T = TypeVar("T")

def print_test(m: object, typ: type[T]) -> T:
match m:
case typ():
reveal_type(m) # N: Revealed type is "T`-1"
return m
case str():
reveal_type(m) # N: Revealed type is "builtins.str"
case _:
reveal_type(m) # N: Revealed type is "builtins.object"
raise

[case testMatchNonFinalMatchArgs]
class A:
__match_args__ = ("a", "b")
Expand Down