[stubtest] False positive involving overloaded function with default argument for parameter with type variable
Aperta
Nessuno ha ancora preso questa issue.
bug
topic-overloads
topic-stubtest
topic-type-variables
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Given the runtime function:
def foo(x=0, ret=1):
return ret
stubtest is happy with these stubs:
from typing import TypeVar, overload
_T = TypeVar("_T")
@overload
def foo(x: int = 0) -> int: ...
@overload
def foo(x: int, ret: _T) -> _T: ...
but complains about these stubs (with a third overload added):
from typing import TypeVar, overload
_T = TypeVar("_T")
@overload
def foo(x: int = 0) -> int: ...
@overload
def foo(x: int, ret: _T) -> _T: ...
@overload
def foo(x: int = 0, *, ret: _T) -> _T: ... # added
with the error:
error: main.foo is inconsistent, runtime parameter "ret" has a default value of type Literal[1], which is incompatible with stub parameter type _T | _T. This is often caused by overloads failing to account for explicitly passing in the default value.
Stub: in file main.pyi:5
Overload(def (x: int =) -> int, def [_T] (x: int, ret: _T) -> _T, def [_T] (x: int =, *, ret: _T) -> _T)
Inferred signature: def (x: int = ..., ret: _T | _T = ...)
Runtime: in file main.py:1
def (x=0, ret=1)
Found 1 error (checked 1 module)
Your Environment
- Mypy version used: master,
mypy 2.2.0+dev.e15a6d58d - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.13.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 riproducendo con stubtest le combinazioni mostrate di runtime e stub di foo, quindi segui il controllo di coerenza tra overload e argomenti predefiniti. Il lavoro è completato quando il caso del terzo overload non segnala più un falso positivo, mentre i valori predefiniti del runtime realmente incompatibili continuano a essere rilevati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools, testing-qa
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Tranquilla
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100