mypy is unable to correctly disambiguate a union when there is a dependent function type
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
mypy is unable to correctly disambiguate a union when there is a dependent function type.
The call meow(s)(s) is always valid as s is either int or str and should not be able to take on both in the line.
However, the dependency of the return type of meow(s) on s is not remembered and mypy becomes upset.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=50bc3d85db8bc5120d881d37b891fd2a
from typing import overload, Callable, Union
@overload
def meow(a: int) -> Callable[[int], int]:
...
@overload
def meow(a: str) -> Callable[[str], str]:
...
def meow(a):
return lambda x: x
# OK
a: str = 's'
meow(a)(a)
# Not ok though it should be
s: Union[int, str] = 's'
meow(s)(s)
Expected Behavior
Expected Behavior
Success: no issues found in 1 source file
Actual Behavior
main.py:20: error: Argument 1 has incompatible type "Union[int, str]"; expected "int" [arg-type]
main.py:20: error: Argument 1 has incompatible type "Union[int, str]"; expected "str" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 0.991
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.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
Inizia eseguendo l’esempio mypy-play collegato con Python 3.11 e mypy 0.991, quindi confronta la chiamata con tipo concreto accettata con la chiamata con Union rifiutata. Analizza come vengono gestiti in questo caso i tipi di ritorno dipendenti derivati dagli overload. Il lavoro è completo quando l’esempio con Union produce “Success: no issues found in 1 source file” senza i due errori arg-type.
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
- 35/100