Skip to content

Commit c98d131

Browse files
committed
Omit errors for class pattern matches against object
1 parent 13fa6c3 commit c98d131

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

mypy/checkpattern.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -671,12 +671,13 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
671671
has_local_errors = local_errors.has_new_errors()
672672
if has_local_errors or key_type is None:
673673
key_type = AnyType(TypeOfAny.from_error)
674-
self.msg.fail(
675-
message_registry.CLASS_PATTERN_UNKNOWN_KEYWORD.format(
676-
typ.str_with_options(self.options), keyword
677-
),
678-
pattern,
679-
)
674+
if not (type_info and type_info.fullname == "builtins.object"):
675+
self.msg.fail(
676+
message_registry.CLASS_PATTERN_UNKNOWN_KEYWORD.format(
677+
typ.str_with_options(self.options), keyword
678+
),
679+
pattern,
680+
)
680681

681682
inner_type, inner_rest_type, inner_captures = self.accept(pattern, key_type)
682683
if is_uninhabited(inner_type):

test-data/unit/check-python310.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,15 +1002,23 @@ match m:
10021002
[builtins fixtures/tuple.pyi]
10031003

10041004
[case testMatchClassPatternNonexistentKeyword]
1005+
from typing import Any
10051006
class A: ...
10061007

10071008
m: object
1009+
n: Any
10081010

10091011
match m:
10101012
case A(a=j): # E: Class "__main__.A" has no attribute "a"
10111013
reveal_type(m) # N: Revealed type is "__main__.A"
10121014
reveal_type(j) # N: Revealed type is "Any"
10131015

1016+
match n:
1017+
# Matching against object should not emit an error for non-existing keys
1018+
case object(a=k):
1019+
reveal_type(n) # N: Revealed type is "builtins.object"
1020+
reveal_type(k) # N: Revealed type is "Any"
1021+
10141022
[case testMatchClassPatternDuplicateKeyword]
10151023
class A:
10161024
a: str

0 commit comments

Comments
 (0)