Incompatible return value type when using typevar with bound and self returning method
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Using a typevar bound to a class that has a self returning method leads to an error when using in a function that operates on the typevar and returns the typevar.
Using constraint does not present the issue but looses the generic on the class.
To Reproduce
# pyright: strict
from typing import Any, Generic, TypeVar
from typing_extensions import Self, reveal_type
T = TypeVar("T")
class X(Generic[T]):
def m(self, v: int) -> Self:
return self
class Y:
def m(self, v: int) -> Self:
return self
# using bound: tb_self errors with Incompatible return value type
TB = TypeVar("TB", bound=X[Any] | Y)
def tb_self(value: TB) -> TB:
return value.m(42)
def tb(value: TB) -> TB:
return value
reveal_type(tb_self(X[int]()))
reveal_type(tb(X[int]()))
# using constraint: return type is X[Any] in both cases
TC = TypeVar("TC", X[Any], Y)
def tc_self(value: TC) -> TC:
return value.m(42)
def tc(value: TC) -> TC:
return value
reveal_type(tc_self(X[int]()))
reveal_type(tc(X[int]()))
https://gist.github.com/mypy-play/3f911f7de9e2282fb6463a7146ffae47
The output is:
file.py:20:12: error: Incompatible return value type (got "X[Any] | Y", expected "TB") [return-value]
file.py:25:13: note: Revealed type is "try_.X[builtins.int]"
file.py:26:13: note: Revealed type is "try_.X[builtins.int]"
file.py:37:13: note: Revealed type is "try_.X[Any]"
file.py:38:13: note: Revealed type is "try_.X[Any]"
Found 1 error in 1 file (checked 1 source file)
For reference pyright on the same input reports no error
Expected Behavior
The is no error.
Actual Behavior
Mypy reports an error when the using a self returning method
Your Environment
- Mypy version used: 1.9.0, 1.7.1
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): - - Python version used: 3.10, 3.12
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, das bereitgestellte Beispiel mit mypy 1.9.0 und den angegebenen Python-Versionen zu reproduzieren, und vergleiche dann die Fälle mit gebundenem und eingeschränktem TypeVar sowie deren aufgelöste Typen. Als erledigt gilt die Aufgabe, wenn die Methode, die sich selbst zurückgibt, keine Diagnose wegen einer inkompatiblen Rückgabe erzeugt und dabei den generischen Typ beibehält, der in der erwarteten reveal-Ausgabe angezeigt wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100