Parameter TypeVar unexpectedly becomes optional when unpacking call results
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
When calling a generic function mypy unexpectedly introduces optionality in the TypeVar when the result is unpacked and raises an error on a perfectly valid function call:
from typing import Iterable
def fun[T](values: Iterable[T], value: list[T]) -> tuple[T | None, T | None]:
...
return None, None
values: Iterable[int]
my_list: list[int]
result = fun(values, my_list) # works fine
m, n = result
a, b = fun(values, my_list) # raises error in mypy
Mypy error:
main.py:15: error: Argument 2 to "fun" has incompatible type "list[int]"; expected "list[int | None]" [arg-type]
This has something to do with the return type of the function being a tuple of optional T's. If the return type is not a union with None or not a tuple, the error disappears.
To reproduce
Here is playground gist of the example: gist
Expected Behavior
In the example I expect both calls to be approved by mypy. Also, I don't expect unpacking to influence what arguments can given in a function call, at least not in this case.
Actual Behavior
mypy raises an error on the second call.
Your Environment
I used mypy playground to confirm this error in various versions:
- Mypy version used:
1.17.11.18.2master branch - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used:
3.123.133.14
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 l’esempio minimo di funzione generica dell’issue ed eseguilo con mypy usando le versioni e le configurazioni di Python indicate. Traccia l’inferenza dei tipi per l’unpacking delle tuple e i TypeVars opzionali; il lavoro è completo quando entrambe le chiamate vengono accettate senza introdurre un’aspettativa incompatibile di list[int | None].
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