python / python/mypy

Truthiness of None Does not Narrow Optional Types

Aperta
#7,249 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

false-positive feature needs discussion priority-2-low topic-type-narrowing
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

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 con la riproduzione minima in Python presente nell'issue e segui il punto di ingresso del restringimento dei tipi basato sulla truthiness di mypy. Nel report non sono indicati file sorgente né test; il lavoro è completato quando il ramo condizionale restringe Optional[int] abbastanza da consentire il controllo del tipo di 100 / maybe_int, con copertura di regressione.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.