python / python/mypy

Warn about non-optional conditions that don't provide __nonzero__?

Offen
#8,284 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

needs discussion
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Maybe mypy should complain about Iterable[...] as an if condition, for example, since it generally doesn't define __nonzero__, so the check is likely unsafe? More generally, we could do this for all classes that don't define __nonzero__, but this could generate too many errors. One option would be to make this an optional check enabled by --strict, at least initially.

Examples:

def f(x: Iterable[int]) -> None:
    if not x:  # Might never be taken, or at least this could be inconsistently taken
        print('x')

class A:
    pass  # No __nonzero__

def g(a: A) -> None:
    if not a:   # Similar to above? What if there are subclasses of 'A'?
        print('x')

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

The issue names no files, tests, or entry points. Start with the Iterable[int] and class A examples, then review how mypy currently handles conditions and strict-mode checks. Done would require a settled scope for the diagnostic, including subclass behavior and whether it is optional or enabled by --strict.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.