Type detection broken on attribute override and not-equal-assertion
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
Bug Report
I commonly find myself writing simplified list init parameters. So, if the user provides only a type X, I automatically convert that to list type [X]. In doing so, I found two issue that prevent a proper type detection. See the example code below.
To Reproduce
#!/usr/bin/env python3
from typing import Union, List, Optional
def test(a: Union[List[str], str, None] = None) -> Optional[List[str]]:
if type(a) == str:
a = [a]
assert(type(a) != str)
return a
Expected Behavior
- First, the
type(a) == strcheck and consequent list assignment should remove the str type from the Union choices. - Second, the
assert(!=)check should also remove the str type from the Union options.
Actual Behavior
Both checks do not work and the type is still guessed incorrectly:

Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: pip install
- Mypy configuration options from
mypy.ini(and other config files): - - Python version used: 3.8
- Operating system and version: macOS 10.15
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
Inizia eseguendo il riproduttore Python fornito con mypy e analizza il comportamento del restringimento dei tipi per l’assegnazione type(a) == str e il controllo assert(type(a) != str). Il lavoro è completato quando entrambi i rami rimuovono str dalla Union inferita e la funzione viene accettata con il tipo di ritorno atteso Optional[List[str]].
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