Inference for Literal arguments not handled
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
If you have a function with Literal arguments, and a variable that is set to one of the options, an error is reported.
To Reproduce
from typing import Literal
def myfun(a: Literal["a", "b", "c"]) -> str:
return a
s = "a"
myfun(s)
def myfun2(a: Literal[1, 2]) -> int:
return a
b = 2
myfun2(b)
c = 1 if s == "a" else 2
myfun2(c)
reports
literalinf.py:10: error: Argument 1 to "myfun" has incompatible type "str"; expected "Union[Literal['a'], Literal['b'], Literal['c']]"
literalinf.py:18: error: Argument 1 to "myfun2" has incompatible type "int"; expected "Union[Literal[1], Literal[2]]"
literalinf.py:21: error: Argument 1 to "myfun2" has incompatible type "int"; expected "Union[Literal[1], Literal[2]]"
Expected Behavior
No error. The values can be inferred to be of the Literal value
Actual Behavior
See above.
Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.8.5
- Operating system and version: Windows 10 21H1
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 traccia come vengono inferiti i valori assegnati e le espressioni condizionali durante il controllo degli argomenti Literal. Aggiungi la copertura per le tre chiamate; il lavoro sarà completo quando non produrranno più gli errori di tipo incompatibile segnalati.
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
- 48/100