From f4e78eaaf972b2b371f32c907c5daa228621efd3 Mon Sep 17 00:00:00 2001 From: Piyush Ss Bhakuni Date: Mon, 22 Jun 2026 23:40:13 +0530 Subject: [PATCH 1/2] Fix false positive redundant-expr for 'if x and y' when body always raises --- mypy/checker.py | 5 +++++ test-data/unit/check-narrowing.test | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/mypy/checker.py b/mypy/checker.py index 33705c98e10c3..79831766fb223 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -5345,6 +5345,11 @@ def visit_if_stmt(self, s: IfStmt) -> None: # Fall-through to the original frame is handled explicitly in each block. with self.binder.frame_context(can_skip=False, conditional_frame=True, fall_through=0): for e, b in zip(s.expr, s.body): + # If condition is 'x and y' and body always raises, + # suppress false redundant-expr warning (see issue #21533) + if isinstance(e, OpExpr) and e.op == "and": + if b.body and all(self.is_noop_for_reachability(s) for s in b.body): + e.right_always = True t = get_proper_type(self.expr_checker.accept(e)) if isinstance(t, DeletedType): diff --git a/test-data/unit/check-narrowing.test b/test-data/unit/check-narrowing.test index 7bab7baa6cebd..00262a1c8edc6 100644 --- a/test-data/unit/check-narrowing.test +++ b/test-data/unit/check-narrowing.test @@ -4281,3 +4281,27 @@ def func(y: H) -> H: else: return y [builtins fixtures/primitives.pyi] + +[case testRedundantExprAndWithRaise] +# flags: --enable-error-code redundant-expr +from typing_extensions import TypeIs +from typing import TypeVar, Sequence, Any + +class Cat: ... + +T = TypeVar('T') +MyType = TypeVar('MyType', int, str, Cat) + +def _is_seq_of(seq: Sequence[Any], tp: type[T]) -> TypeIs[Sequence[T]]: ... + +def main1(a: Sequence[MyType]) -> MyType: + if not _is_seq_of(a, Cat) and not _is_seq_of(a, int): + raise Exception("unexpected") + return a[0] + +def main2(a: Sequence[MyType]) -> MyType: + if not _is_seq_of(a, Cat): + if not _is_seq_of(a, int): + raise Exception("unexpected") + return a[0] +[builtins fixtures/exception.pyi] \ No newline at end of file From 6bbbafddbbe8b689cd1386c109e44954601b1553 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Jun 2026 18:59:10 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test-data/unit/check-narrowing.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-narrowing.test b/test-data/unit/check-narrowing.test index 00262a1c8edc6..d635773f3cdb2 100644 --- a/test-data/unit/check-narrowing.test +++ b/test-data/unit/check-narrowing.test @@ -4304,4 +4304,4 @@ def main2(a: Sequence[MyType]) -> MyType: if not _is_seq_of(a, int): raise Exception("unexpected") return a[0] -[builtins fixtures/exception.pyi] \ No newline at end of file +[builtins fixtures/exception.pyi]