[stubtest] False positive involving overloaded function with default argument for parameter with type variable
Offen
Dieses Issue hat noch niemand übernommen.
bug
topic-overloads
topic-stubtest
topic-type-variables
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die gezeigten Kombinationen aus foo-Laufzeit und Stubs mit stubtest zu reproduzieren, und verfolge dann die Konsistenzprüfung für Overloads und Standardargumente. Als erledigt gilt die Aufgabe, wenn der Fall mit dem dritten Overload keinen False Positive mehr meldet, während tatsächlich inkompatible Laufzeit-Standardwerte weiterhin erkannt werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools, testing-qa
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100