Mypy seems to ignore explicitly given type parameter when a overload containing Never matches
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 trying to create an object from a generic class and having overloads, one of which is parameterized with Never, initializing the class with the signature compatible with the overload for Never seems to result in a type parameterized by Never, regardless of the type being specified explicitly (k in the playground). Typing the variable directly seems to resolve this issue (w in the playground).
Pyright seems to give the same expected error in both instances.
I am sorry if this explanation is a bit long-winded, I don't really know how to explain it more succinctly.
To Reproduce
https://mypy-play.net/?mypy=master&python=3.12&gist=9ca64c2cde9f88a1000b10217cd5b2bd&flags=strict
Expected Behavior
Both the w and the k case from the playground should have the same error.
Actual Behavior
Only the w case actually gives an error, and the k case passes without issue, i.e. Test[int]() results in a variable of type Test[Never] for some reason, rather than mypy complaining that the signature is not right.
main.py:25: error: No overload variant of "Test" matches argument type "int" [call-overload]
main.py:25: note: Possible overload variants:
main.py:25: note: def __init__(self, t: None = ...) -> Test[NoReturn]
main.py:25: note: def __init__(self, t: str) -> Test[str]
main.py:27: error: Incompatible types in assignment (expression has type "Test[NoReturn]", variable has type "Test[int]") [assignment]
main.py:30: note: Revealed type is "__main__.Test[builtins.int]"
main.py:31: note: Revealed type is "__main__.Test[Never]"
main.py:32: note: Revealed type is "__main__.Test[Never]"
main.py:33: note: Revealed type is "__main__.Test[builtins.int]"
main.py:34: note: Revealed type is "Any"
main.py:36: note: Revealed type is "__main__.Test[builtins.int]"
main.py:37: note: Revealed type is "__main__.Test[Never]"
Found 2 errors in 1 file (checked 1 source file)
Your Environment
I don't believe any of this matters in this instance because the same issue happens on the playground as well.
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 dal mypy playground collegato e riproduci l’esempio in main.py in strict mode con Python 3.12. Traccia il modo in cui la risoluzione degli overload gestisce il Test[int] fornito esplicitamente quando corrisponde l’overload parametrizzato da Never. Il lavoro è completato quando i casi w e k producono lo stesso errore, come descritto in Expected Behavior.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100