Skip to content

Commit

Permalink
Fix crash in match statement if class name is undefined (#12417)
Browse files Browse the repository at this point in the history
Fixes #12416.
  • Loading branch information
JukkaL committed Mar 22, 2022
1 parent 718b50e commit b814ae1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mypy/checkpattern.py
Expand Up @@ -463,7 +463,8 @@ def visit_class_pattern(self, o: ClassPattern) -> PatternType:
# Check class type
#
type_info = o.class_ref.node
assert type_info is not None
if type_info is None:
return PatternType(AnyType(TypeOfAny.from_error), AnyType(TypeOfAny.from_error), {})
if isinstance(type_info, TypeAlias) and not type_info.no_args:
self.msg.fail(message_registry.CLASS_PATTERN_GENERIC_TYPE_ALIAS, o)
return self.early_non_match()
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-python310.test
Expand Up @@ -567,6 +567,20 @@ match m:
reveal_type(m) # N: Revealed type is "builtins.tuple[Any, ...]"
[builtins fixtures/primitives.pyi]

[case testMatchInvalidClassPattern]
m: object

match m:
case xyz(y): # E: Name "xyz" is not defined
reveal_type(m) # N: Revealed type is "Any"
reveal_type(y) # E: Cannot determine type of "y" \
# N: Revealed type is "Any"

match m:
case xyz(z=x): # E: Name "xyz" is not defined
reveal_type(x) # E: Cannot determine type of "x" \
# N: Revealed type is "Any"

[case testMatchClassPatternCaptureDataclass]
from dataclasses import dataclass

Expand Down

0 comments on commit b814ae1

Please sign in to comment.