Incorrect type inferencing when parameterizing with Self-like type variable involving `__ge__`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Found a very strange bug involving self-like type variables while using __ge__ and similar methods. The following example defines:
- a parameterized protocol
SupportsComparisonwhich takesSelfas its first parameter and the type being compared to with its other parameter, - a protocol
SupportsRaddwhich allows type-hintingtype(t + supports_radd) == type(t), - a function
fooshould then acceptfoo(x, y, z)wherexsupports comparison withyandzsupports radd withxand returns(x, y).
In the example, foo(float, float, Fraction) -> tuple[float, float] is the expected result, since:
floatsupports comparison withfloatandselfshould befloat, meaningx: SupportsComparison[float, float],Fractionsupports radd withfloat, meaningz: SupportsRadd[float].
However, the result given by reveal_type(...) gives tuple[Fraction, float]. How is this the case?
from typing import Any, Protocol, TypeVar
T = TypeVar("T")
T_con = TypeVar("T_con", contravariant=True)
Self = TypeVar("Self", bound="SupportsComparison[Any, Any]", covariant=True)
class SupportsComparison(Protocol[Self, T_con]):
def __ge__(self: Self, other: T_con) -> bool: ...
def __gt__(self: Self, other: T_con) -> bool: ...
def __le__(self: Self, other: T_con) -> bool: ...
def __lt__(self: Self, other: T_con) -> bool: ...
class SupportsRadd(Protocol[T]):
def __radd__(self, other: T) -> T: ...
X = TypeVar("X", bound=SupportsComparison[Any, Any])
Y = TypeVar("Y")
Z = TypeVar("Z", bound=SupportsRadd[Any])
def foo(
x: SupportsComparison[X, Y],
y: Y,
z: SupportsRadd[X],
) -> tuple[X, Y]:
return (x, y)
from fractions import Fraction
reveal_type(foo(float(), float(), Fraction())) # Revealed type is "Tuple[fractions.Fraction*, builtins.float*]"
- Mypy version:
0.942. - Python version used:
3.10.0.
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, den bereitgestellten Reproducer mit der aktuellen mypy-Version auszuführen und die Inferenz für foo nachzuverfolgen, insbesondere den Self-ähnlichen SupportsComparison-Parameter und die ge-Methoden. Finde die relevanten Typinferenztests oder füge für das Beispiel einen Regressionstest hinzu; als erledigt gilt die Aufgabe, wenn reveal_type tuple[float, float] statt tuple[Fraction, float] meldet.
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
- 28/100