Skip to content

Commit 8fb6fab

Browse files
committed
Narrow unreachable captures due to guards in match to Never
1 parent 2bd0802 commit 8fb6fab

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

mypy/checker.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5515,6 +5515,9 @@ def visit_match_stmt(self, s: MatchStmt) -> None:
55155515
if isinstance(p, AsPattern):
55165516
case_target = p.pattern or p.name
55175517
if isinstance(case_target, NameExpr):
5518+
if guard_map is None:
5519+
guard_map = {case_target: UninhabitedType()}
5520+
55185521
for type_map in (guard_map, else_map):
55195522
if not type_map:
55205523
continue

test-data/unit/check-narrowing.test

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,6 @@ else:
995995
reveal_type(a) # E: Statement is unreachable \
996996
# N: Revealed type is "Never"
997997
reveal_type(b) # N: Revealed type is "Union[Literal[1], Literal[2], Literal[3], Literal[4]]"
998-
999998
[builtins fixtures/primitives.pyi]
1000999

10011000
[case testNarrowingLiteralTruthiness]
@@ -2112,8 +2111,6 @@ if isinstance(x, (Z, NoneType)): # E: Subclass of "X" and "Z" cannot exist: "Z"
21122111
# E: Subclass of "X" and "NoneType" cannot exist: "NoneType" is final
21132112
reveal_type(x) # E: Statement is unreachable \
21142113
# N: Revealed type is "Never"
2115-
2116-
21172114
[builtins fixtures/isinstance.pyi]
21182115

21192116
[case testTypeNarrowingReachableNegative]

test-data/unit/check-python310.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ m: str
13291329
match m:
13301330
case a if False:
13311331
reveal_type(a) # E: Statement is unreachable \
1332-
# N: Revealed type is "builtins.str"
1332+
# N: Revealed type is "Never"
13331333

13341334
[case testMatchRedefiningPatternGuard]
13351335
m: str

0 commit comments

Comments
 (0)