python / python/mypy

Truthiness of None Does not Narrow Optional Types

Offen
#7,249 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

false-positive feature needs discussion priority-2-low topic-type-narrowing
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?
    I believe this is a bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

from typing import Optional

def the_method(maybe_int: Optional[int]):
    can_divide = bool(maybe_int)
    if can_divide:
        100 / maybe_int
  • What is the actual behavior/output?
error: Unsupported operand types for / ("int" and "None")
  • What is the behavior/output you expect?
    I would think that the conditional branch would recognize that only int values would ever make it there

  • What are the versions of mypy and Python you are using?
    mypy 0.720 with Python 3.7.3

  • Do you see the same issue after installing mypy from Git master?
    Yes still exists on master

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

Beginne mit der minimalen Python-Reproduktion im Issue und verfolge den Einstiegspunkt für mypys wahrheitswertbasierte Typverengung. Im Bericht ist keine Quelldatei und kein Test genannt; abgeschlossen ist die Aufgabe, wenn der bedingte Zweig Optional[int] so weit verengt, dass 100 / maybe_int typgeprüft werden kann, mit Regressionstestabdeckung.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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