Constant vs variable as index for none-checking List[Optional[...]]
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Can you explain to me why there is this difference between using a constant and a variable as index for none-checking here?
from typing import List, Optional
my_list: List[Optional[float]] = []
if my_list[0] is not None:
result_1: float = my_list[0] # no error
result_2: float = my_list[1] # error: Incompatible types in assignment (expression has type "Optional[float]", variable has type "float")
id_0 = 0
if my_list[id_0] is not None:
result_3: float = my_list[id_0] # error: Incompatible types in assignment (expression has type "Optional[float]", variable has type "float")
for i in range(10):
if my_list[i] is not None:
a: float = my_list[i] # error: Incompatible types in assignment (expression has type "Optional[float]", variable has type "float")
- Mypy version used:
0.8000.941 - Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: python3.6
- Operating system and version: Ubuntu 18.04 in WSL2 (Windows 11)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
La issue non indica alcun file o test del repository. Riproduci gli esempi con mypy 0.941, quindi traccia il comportamento del restringimento dei tipi per le espressioni indicizzate List[Optional[float]]. Il lavoro è completato quando è spiegato il comportamento differente di costanti, variabili e indici di ciclo e, se viene confermato come bug, questo è coperto da un regression test appropriato.
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à
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100