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

Fix crash in match statement if class name is undefined #12417

Merged
merged 2 commits into from
Mar 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/checkpattern.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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" \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Cannot determine type" is a bit unfortunate; I'd rather it's just Any. Probably fine this way though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Any would be better, but I think that this would require a much bigger change.

# 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