Wrong return type inferred for overload with a type variable
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 typing import TypeVar, overload
_T = TypeVar("_T")
@overload
def getenv(key: str) -> str | None: ...
@overload
def getenv(key: str, default: _T) -> str | _T: ...
def getenv(key, default):
return key
def test(custom_var: str) -> str:
return getenv(custom_var) or getenv("V", "D")
https://mypy-play.net/?mypy=latest&python=3.12&gist=4d959cd7069a8495533bfeca1cd40729
Actual Behavior
main.py:16: error: Incompatible return value type (got "str | None", expected "str") [return-value]
Expected Behavior
This should type check without errors, since we have an or with a call that always returns str.
It works if I replace type variable with just str:
@overload
def getenv(key: str, default: str) -> str: ...
but something about _T and the union upsets it.
Your Environment
- Mypy version used: 1.13.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10-3.13
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 Python fornita e conferma il comportamento usando mypy 1.13.0 senza flag di configurazione. Traccia la risoluzione degli overload e l’inferenza delle variabili di tipo per le due chiamate a getenv, quindi aggiungi un test di regressione che mostri che l’espressione viene accettata quando il fallback restituisce str.
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
- 42/100