Skip to content

False positive redundant-expr, but only for if x and y:, not for if x: if y: #21533

@MarcoGorelli

Description

@MarcoGorelli

Bug Report

In the code below

if not _is_seq_of(a, Cat) and not _is_seq_of(a, int):

is treated differently to

if not _is_seq_of(a, Cat):
    if not _is_seq_of(a, int):

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.14&enable-error-code=redundant-expr&gist=470e934d38b23a68087d2cd4ebcb9a2a

from typing import TypeIs, TypeVar, Sequence, Any, Self, reveal_type, Iterable, TypeAlias

class Cat:
    def foo(self) -> Self:  # type: ignore[empty-body]
        ...


MyType = TypeVar('MyType', int, str, Cat)
T = TypeVar('T')

def _is_seq_of(seq: Sequence[Any], tp: type[T]) -> TypeIs[Sequence[T]]:  # type: ignore[empty-body]
    ...


def main1(a: Sequence[MyType], how: str) -> MyType:
    if how.startswith('align'):
        if not _is_seq_of(a, Cat) and not _is_seq_of(a, int):
            msg = 'unexpected'
            raise TypeError(msg)
    return a[0]

def main2(a: Sequence[MyType], how: str) -> MyType:
    if how.startswith('align'):
        if not _is_seq_of(a, Cat):
            if not _is_seq_of(a, int):
                msg = 'unexpected'
                raise TypeError(msg)
    return a[0]

Mypy accepts main2 just fine, but for main1, it reports

$ mypy --enable-error-code redundant-expr t.py
t.py:17: error: Left operand of "and" is always true  [redundant-expr]
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

I think main1 and main2 should be treated the same. My expectation is that they should both pass (not report any errors)

Actual Behavior

$ mypy --enable-error-code redundant-expr t.py
t.py:17: error: Left operand of "and" is always true  [redundant-expr]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 2.1.0
  • Mypy command-line flags: --enable-error-code redundant-expr
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.14.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions