Skip to content

Commit 914f6c8

Browse files
committed
Simplify the check: we can name primitive literals just as well, it should not be common
1 parent 0bf5dc9 commit 914f6c8

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

mypy/checker.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
CallExpr,
7676
ClassDef,
7777
ComparisonExpr,
78-
ComplexExpr,
7978
Context,
8079
ContinueStmt,
8180
Decorator,
@@ -5572,19 +5571,8 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
55725571
def _make_named_statement_for_match(self, s: MatchStmt) -> Expression:
55735572
"""Construct a fake NameExpr for inference if a match clause is complex."""
55745573
subject = s.subject
5575-
expressions_to_preserve = (
5574+
if isinstance(subject, (NameExpr, AssignmentExpr)):
55765575
# Already named - we should infer type of it as given
5577-
NameExpr,
5578-
AssignmentExpr,
5579-
# Primitive literals - their type is known, no need to name them
5580-
IntExpr,
5581-
StrExpr,
5582-
BytesExpr,
5583-
FloatExpr,
5584-
ComplexExpr,
5585-
EllipsisExpr,
5586-
)
5587-
if isinstance(subject, expressions_to_preserve):
55885576
return subject
55895577
elif s.subject_dummy is not None:
55905578
return s.subject_dummy

test-data/unit/check-python310.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,6 +2613,27 @@ match A().foo:
26132613
case other:
26142614
other # E: Statement is unreachable
26152615

2616+
[case testMatchLiteral]
2617+
# flags: --warn-unreachable
2618+
2619+
def int_literal() -> None:
2620+
match 12:
2621+
case 1 as s:
2622+
reveal_type(s) # N: Revealed type is "Literal[1]"
2623+
case int(i):
2624+
reveal_type(i) # N: Revealed type is "Literal[12]?"
2625+
case other:
2626+
other # E: Statement is unreachable
2627+
2628+
def str_literal() -> None:
2629+
match 'foo':
2630+
case 'a' as s:
2631+
reveal_type(s) # N: Revealed type is "Literal['a']"
2632+
case str(i):
2633+
reveal_type(i) # N: Revealed type is "Literal['foo']?"
2634+
case other:
2635+
other # E: Statement is unreachable
2636+
26162637
[case testMatchOperations]
26172638
# flags: --warn-unreachable
26182639

0 commit comments

Comments
 (0)