python / python/mypy

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

Aperta
#21,533 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla riproduzione mypy-play collegata ed esegui il comando riportato con --enable-error-code redundant-expr. Confronta il comportamento di narrowing delle condizioni combinate e annidate, quindi verifica che entrambi gli esempi producano le diagnosi previste senza il falso positivo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.