"error: only instance methods can be decorated with @property" is incorrect for Python 3.9 and later
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
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, die gemeldete Reproduktion mit Python 3.10 und mypy einschließlich --python-version=3.10 auszuführen, und bestätige die fehlerhafte Diagnose. Verfolge die Verarbeitung der Kombination aus @classmethod und @property und füge anschließend Tests hinzu oder aktualisiere die Testabdeckung, sodass das Beispiel ohne den Fehler typgeprü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
- 45/100