"error: only instance methods can be decorated with @property" is incorrect for Python 3.9 and later
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
Bug Report
The code shown in the "to reproduce" section works correctly in Python 3.10 and later (and would work correctly in 3.9 and later if it didn't use inspect.get_annotations), but triggers an unconditional error message from mypy: "Only instance methods can be decorated with @property".
As far as I can tell, that was true in 3.8 and earlier, but that restriction was relaxed as a new feature of 3.9. See the "Changed in 3.9" note at the end of https://docs.python.org/3.9/library/functions.html#classmethod, which specifically calls out @property as something that can now be wrapped in @classmethod.
This is closely related to, but not quite the same as, #2563.
To Reproduce
from inspect import get_annotations
class BugDemo:
@classmethod
@property
def permitted_keys(cls) -> frozenset[str]:
return frozenset(get_annotations(cls).keys())
a: str
b: int
if __name__ == "__main__":
print(repr(BugDemo.permitted_keys))
playground: https://mypy-play.net/?mypy=latest&python=3.10&gist=91eef6c76933a64774b3ac310976da27
Expected Behavior
This program should be considered type-correct.
Actual Behavior
$ python3.10 bug.py
frozenset({'b', 'a'})
$ mypy bug.py
bug.py:4: error: Only instance methods can be decorated with @property [misc]
$ mypy --python-version=3.10 bug.py
bug.py:4: error: Only instance methods can be decorated with @property [misc]
Your Environment
- Mypy version used: 1.18.2 (compiled: yes)
- Mypy command-line flags: see "actual behavior" section
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10
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
Inizia eseguendo la riproduzione segnalata con Python 3.10 e mypy, includendo --python-version=3.10, e conferma la diagnosi errata. Traccia la gestione della combinazione di @classmethod e @property, quindi aggiungi o aggiorna la copertura in modo che l'esempio superi il controllo dei tipi senza l'errore.
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
- Abbastanza chiara
- Idoneità per principianti
- 45/100