Skip to content

Commit 1952636

Browse files
authored
Fix match statement semantic reachability (#20968)
Fixes #12778
1 parent e6d41eb commit 1952636

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

mypy/reachability.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,7 @@ def infer_reachability_of_match_statement(s: MatchStmt, options: Options) -> Non
9191
):
9292
# The case is considered always false, so we skip the case body.
9393
mark_block_unreachable(s.bodies[i])
94-
elif pattern_value in (ALWAYS_FALSE, MYPY_TRUE) and guard_value in (
95-
ALWAYS_TRUE,
96-
MYPY_TRUE,
97-
):
94+
elif pattern_value in (ALWAYS_TRUE, MYPY_TRUE) and guard_value in (ALWAYS_TRUE, MYPY_TRUE):
9895
for body in s.bodies[i + 1 :]:
9996
mark_block_unreachable(body)
10097

test-data/unit/check-python310.test

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2899,9 +2899,27 @@ def f4(e: int | str | bytes) -> int:
28992899
return 0
29002900
reveal_type(e) # N: Revealed type is "builtins.int | builtins.bytes"
29012901
return 0
2902-
29032902
[builtins fixtures/primitives.pyi]
29042903

2904+
[case testMatchGuardAlwaysTrueAlwaysFalse]
2905+
# flags: --warn-unreachable --always-true TRUEFLAG --always-false FALSEFLAG
2906+
TRUEFLAG = False
2907+
FALSEFLAG = True
2908+
2909+
def true_flag(e: int) -> None:
2910+
match e:
2911+
case _ if TRUEFLAG:
2912+
pass
2913+
case 0:
2914+
# No error
2915+
1 + "asdf"
2916+
2917+
def false_flag(e: int) -> None:
2918+
match e:
2919+
case _ if FALSEFLAG:
2920+
# No error
2921+
1 + "asdf"
2922+
29052923
[case testMatchSequencePatternVariadicTupleNotTooShort]
29062924
from typing import Tuple
29072925
from typing_extensions import Unpack

0 commit comments

Comments
 (0)