Descriptor protocol is executed for properties returning descriptors
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Consider this example:
from __future__ import annotations
class X: ...
class Y:
@property
def some_property(self) -> Descriptor: # return Descriptor instance
return Descriptor()
class Descriptor:
def __get__(self, obj: X, cls: type[X]) -> int: # can work only with X instances
print(f'Descriptor.__get__({self}, {obj}, {cls}) called')
return 0
y = Y()
print(y.some_property)
# arg-type: Argument 1 to "__get__" of "Descriptor" has incompatible type "Y"; expected "X"
# arg-type: Argument 2 to "__get__" of "Descriptor" has incompatible type "Type[Y]"; expected "Type[X]"
# stdout: <__main__.Descriptor object at 0x0000020BF8A5BE20>
# note that Descriptor.__get__ isnt called
I am defining descriptor that can work only with X instances, and i am returning this descriptor from property of Y instances.
At runtime Descriptor.__get__ isnt called, but mypy thinks that Y.some_property is Descriptor (not a property returning Descriptor instance), tries to execute descriptor protocol and fails.
At runtime i am getting no errors, descriptor isnt called and this code is safe.
To reproduce:
- just run mypy with this code
Expected Behavior
- no errors
Actual Behavior
- error
Your Environment
- Mypy version used:
mypy 0.971 (compiled: yes) - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10.6
- Operating system and version: Win10
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 Inline-Reproducer mit mypy 0.971 auszuführen, und bestätige die beiden deskriptorbezogenen Fehler. Verfolge mypy's Behandlung von Eigenschaften, deren Rückgabewerte Deskriptoren implementieren, und überprüfe anschließend, dass das Beispiel keine Fehler erzeugt, während der Zugriff auf echte Deskriptoren weiterhin geprüft 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
- 35/100