Metaclass confusion with properties
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
I'm using python 3.8 and mypy 0.740. The script below prints "21" and "42" when run, but fails to type check with mypy:
class meta (type):
pass
class value (metaclass=meta):
pass
class submeta (meta):
@property
def attr(cls) -> int:
return 42
class subvalue (metaclass=submeta):
@property
def attr(self) -> int:
return 21
o = subvalue()
a = o.attr
print(a)
a = subvalue.attr
print(a)
The mypy diagnostics say:
meta-test.py:24: error: Incompatible types in assignment (expression has type "Callable[[subvalue], int]", variable has type "int")
Found 1 error in 1 file (checked 1 source file)
The error is incorrect, "subvalue.attr" has type "int", as it resolves to the property defined on the metaclass, not to the property defined on the class.
This is fairly esoteric as written, but is the structure used by PyObjC to expose Objective-C classes. PyObjC uses metaclasses to expose class methods because ObjC classes can have instance- and class-methods with the same name.
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
Reproduziere die Diagnose anhand des im Issue beschriebenen Beispiels meta-test.py mit Python 3.8 und mypy 0.740. Beginne damit nachzuverfolgen, wie mypy Eigenschaften auf subvalue im Vergleich zu seiner submeta-Metaklasse auflöst. Als erledigt gilt, wenn subvalue.attr als int inferiert wird, ohne den gemeldeten incompatible-assignment-Fehler, während der Zugriff auf die Instanz weiterhin korrekt bleibt.
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
- Klar beschrieben
- Anfängerfreundlichkeit
- 38/100