Descriptor access fails for complex generics
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Mypy performs descriptor access by binding __get__ first and then evaluating the bound method for instance and owner. This causes problems when __get__ has an explicit self annotation that depends on the type of instance.
Mypy correctly infers the type when evaluating unbound __get__ and passing (descriptor, instance, owner) together.
It would be nice if Mypy could somehow make this work for any method (by delaying expansion of type variables in bound methods), but I understand that's a much bigger change than fixing for special method lookup, which doesn't actually do any binding at runtime.
To Reproduce
from collections.abc import Callable
from typing import overload, Concatenate, Self
class Descriptor[C: Callable]:
def __init__(self, impl: C) -> None: ...
@overload
def __get__(
self, instance: None, owner: type, /,
) -> Self: ...
@overload
def __get__[S, **P, R](
self: Descriptor[Callable[Concatenate[S, P], R]],
instance: S,
owner: type[S] | None = None,
/,
) -> Callable[P, R]: ...
def __get__(self, *_) -> object:
pass
class Test:
@Descriptor
# Explicit `self` type is necessary to prevent
# Mypy from assuming this is a classmethod when
# accessing via the class.
def method[S: Test](self: S, foo: int, bar: str) -> S:
return self
reveal_type(Test().method)
reveal_type(type(Test.method).__get__(Test.method, Test()))
Expected Behavior
main.py:34: note: Revealed type is "def (foo: int, bar: str) -> __main__.Test"
main.py:35: note: Revealed type is "def (foo: int, bar: str) -> __main__.Test"
Success: no issues found in 1 source file
Actual Behavior
main.py:34: error: Argument 1 to "__get__" of "Descriptor" has incompatible type "Test"; expected "None" [arg-type]
main.py:34: note: Revealed type is "__main__.Descriptor[def [S <: __main__.Test] (self: S, foo: int, bar: str) -> S]"
main.py:35: note: Revealed type is "def (foo: int, bar: str) -> __main__.Test"
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 2.1.0
- Python version used: 3.12
Related Issues
#18036
#16554
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, Mypys Behandlung des Deskriptorzugriffs und die Suche nach speziellen Methoden nachzuverfolgen, wobei du dich darauf konzentrierst, wie gebundene und ungebundene get-Aufrufe ausgewertet werden. Verwende den Reproducer und vergleiche beide angezeigten Typen mit der erwarteten Ausgabe; fertig ist es, wenn Test().method den aufrufbaren Wert erzeugt, der Test ohne einen Argumenttypfehler zurückgibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100