False positive `redundant-expr`, but only for `if x and y:`, not for `if x: if y:`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der verlinkten mypy-play-Reproduktion und führe den gemeldeten Befehl mit --enable-error-code redundant-expr aus. Vergleiche das Narrowing-Verhalten der kombinierten und verschachtelten Bedingungen und überprüfe anschließend, dass beide Beispiele die erwarteten Diagnosen ohne den False Positive erzeugen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 48/100