Incorrect constraint inference for unions with nested generics
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
From my search, it looks like an issue similar to #6417, but happening to unions of generic and nested generic.
mypy has troubles with inferring a proper constraint and passing a nested generic to function that should accept such nested generic fails.
The issue can be worked around with @overloads.
To Reproduce
Minimum reproduction:
from typing import List, TypeVar, Union
_T = TypeVar("_T")
def func(param: Union[List[_T], List[List[_T]]]) -> _T:
...
func([1])
func([[1]])
A more real-live example of where this is an issue is tornado's gen function (overloads there are the part of workaround for this issue):
https://github.com/tornadoweb/tornado/pull/2909/files
Expected Behavior
No errors.
Actual Behavior
main.py:10: error: Argument 1 to "func" has incompatible type "List[List[int]]"; expected "Union[List[<nothing>], List[List[<nothing>]]]"
Your Environment
- Mypy version used: 0.781
- 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
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 con la riproduzione minima in main.py e verifica il comportamento diverso di func([1]) e func([[1]]) con mypy. Traccia il percorso di inferenza dei vincoli per Union[List[_T], List[List[_T]]] e confrontalo con la issue correlata #6417 e con il workaround basato su overload nella pull request collegata di Tornado. Il lavoro è completato quando entrambe le chiamate passano il controllo dei tipi senza errori.
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