Incorrect widening of `T | None` with unions to `builtins.object` in generic functions
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
Generic function with signature (fun: Callable[[], T | None]) -> T | None has its type incorrectly widened to builtins.object when T is a union type
To Reproduce
import typing
T = typing.TypeVar("T")
def gen_fun_one(fun: typing.Callable[[], T | int]) -> T | int:
return fun()
def gen_fun_two(fun: typing.Callable[[], T | None]) -> T | None:
return fun()
def foo() -> int | str | None:
pass
typing.reveal_type(gen_fun_one(foo)) # Union[builtins.str, None, builtins.int]
typing.reveal_type(gen_fun_two(foo)) # builtins.object
Gist URL: https://gist.github.com/mypy-play/f2401bd493520f4c72cad4a416195e19
Playground URL: https://mypy-play.net/?mypy=latest&python=3.12&gist=f2401bd493520f4c72cad4a416195e19
Expected Behavior
Both gen_fun_one(foo) and gen_fun_two(foo) are of type str | int | None
Actual Behavior
Function gen_fun_two(foo) is of type builtins.object
Your Environment
- Mypy version used: 1.9.0
- Python version used: 3.12
- Default Mypy playground settings
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 la riproduzione Python fornita nel mypy playground collegato e confronta i risultati di reveal_type per gen_fun_one e gen_fun_two. Traccia l'inferenza dell'unione generica coinvolta nella seconda chiamata; il lavoro è completato quando inferisce str | int | None invece di builtins.object, con una copertura per il caso segnalato.
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
- 44/100