New type inference does not support using singledispatch to decorate a function in which the first argument is a union indexed by a constrained type variable that also appears in the return 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
Apologies for the wordy title, but this issue appears to be somewhat of a corner case.
In some unpublished code, I have a singledispatch function that doesn't pass the new type inference code.
It appears that the problem is that it's going from a union which depends on a constrained typevar to a type that also depends on that typevar.
If it's not a union, not constrained, or doesn't appear in the return type, it passes fine.
To Reproduce
from functools import singledispatch, singledispatchmethod
from typing import AnyStr, TypeVar
T = TypeVar("T")
@singledispatch
def func(arg: list[AnyStr] | None) -> AnyStr:
raise NotImplementedError
@singledispatch
def func_no_union(arg: list[AnyStr]) -> AnyStr:
raise NotImplementedError
@singledispatch
def func_no_return(arg: list[AnyStr] | None) -> None:
raise NotImplementedError
@singledispatch
def func_unconstrained(arg: list[T] | None) -> T:
raise NotImplementedError
https://mypy-play.net/?mypy=master&python=3.11&gist=3bd3f48cd0e1aca5dcdd03cfd44d3d3e
(The use of list does not trigger the bug, but I figured it'd make things less ambiguous in the unconstrained case. Also, it can be any union. The union that triggered this issue in the first place did not include None. Also, singledispatchmethod on a similarly-typed method will also trigger the issue.)
Actual Behavior
Passes successfully pre-1.7.0 so long as --new-type-inference is not passed.
Otherwise, decorating func fails. All others succeed.
main.py:8: error: Argument 1 to "singledispatch" has incompatible type "Callable[[list[AnyStr] | None], AnyStr]"; expected "Callable[..., Never]" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.7.0 and master (failed), 1.6.1 (passed with old inference)
- 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 il reproducer fornito con --new-type-inference e confronta il caso singledispatch che fallisce con i tre casi che hanno esito positivo. Traccia quindi la gestione della nuova inferenza dei tipi e di singledispatch, poi verifica che il caso di unione, la variabile di tipo vincolata, il caso del tipo restituito e l’analogo caso di singledispatchmethod superino il controllo dei tipi senza causare regressioni nei casi di controllo.
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
- 42/100