Metaclass confusion with properties
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riproduci la diagnostica usando l’esempio meta-test.py descritto nell’issue con Python 3.8 e mypy 0.740. Inizia tracciando come mypy risolve le proprietà su subvalue rispetto alla sua metaclasse submeta. Il lavoro è completo quando subvalue.attr viene inferito come int senza l’errore incompatible-assignment segnalato, mentre l’accesso all’istanza rimane corretto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 38/100