From af15940429d515abffaaea3819648b3ae32d6987 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 4 Mar 2026 01:20:19 -0800 Subject: [PATCH] Fix match statement semantic reachability Fixes #12778 --- mypy/reachability.py | 5 +---- test-data/unit/check-python310.test | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/mypy/reachability.py b/mypy/reachability.py index 132c269e96af2..37c7a9715600a 100644 --- a/mypy/reachability.py +++ b/mypy/reachability.py @@ -91,10 +91,7 @@ def infer_reachability_of_match_statement(s: MatchStmt, options: Options) -> Non ): # The case is considered always false, so we skip the case body. mark_block_unreachable(s.bodies[i]) - elif pattern_value in (ALWAYS_FALSE, MYPY_TRUE) and guard_value in ( - ALWAYS_TRUE, - MYPY_TRUE, - ): + elif pattern_value in (ALWAYS_TRUE, MYPY_TRUE) and guard_value in (ALWAYS_TRUE, MYPY_TRUE): for body in s.bodies[i + 1 :]: mark_block_unreachable(body) diff --git a/test-data/unit/check-python310.test b/test-data/unit/check-python310.test index 466b466868317..e9fbf4eb12195 100644 --- a/test-data/unit/check-python310.test +++ b/test-data/unit/check-python310.test @@ -2899,9 +2899,27 @@ def f4(e: int | str | bytes) -> int: return 0 reveal_type(e) # N: Revealed type is "builtins.int | builtins.bytes" return 0 - [builtins fixtures/primitives.pyi] +[case testMatchGuardAlwaysTrueAlwaysFalse] +# flags: --warn-unreachable --always-true TRUEFLAG --always-false FALSEFLAG +TRUEFLAG = False +FALSEFLAG = True + +def true_flag(e: int) -> None: + match e: + case _ if TRUEFLAG: + pass + case 0: + # No error + 1 + "asdf" + +def false_flag(e: int) -> None: + match e: + case _ if FALSEFLAG: + # No error + 1 + "asdf" + [case testMatchSequencePatternVariadicTupleNotTooShort] from typing import Tuple from typing_extensions import Unpack