Class inheriting from property doesn't typecheck properly when used as a decorator
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
I have a custom class inheriting from property. When I go to use this as a decorator though mypy seems to miss that it's a property at all. This is with Python 3.6.3 and mypy 0.650.
Example:
class CustomProperty(property):
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
super().__init__(fget, fset, fdel, doc)
class A:
def __init__(self) -> None:
self._val = 1
@CustomProperty
def val(self) -> int:
return self._val
@val.setter
def val(self, v: int) -> None:
self._val = v
def test_a() -> None:
print(A.val)
Running mypy on this gives the following output:
mypy minimal2.py
minimal.py:14: error: Name 'val' already defined on line 10
minimal.py:14: error: Name 'val' is not defined
minimal.py:20: error: "Type[A]" has no attribute "val"
Contrast this with the same code with the builtin property which typechecks fine:
class B:
def __init__(self) -> None:
self._val = 1
@property
def val(self) -> int:
return self._val
@val.setter
def val(self, v: int) -> None:
self._val = v
def test_b() -> None:
print(B.val)
It's possible that this is related to #1529 where @ilevkivskyi suggested that:
The original example now passes mypy, not sure however how useful the subclass can be (property is heavily special-cased in mypy), but at least the original bug is fixed, so closing this.
Is subclassing property frowned upon (or just not supported in mypy?).
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 den Bericht mit dem Beispiel minimal.py unter Verwendung von Python 3.6.3 und mypy 0.650 und vergleiche anschließend den Fall mit dem CustomProperty-Decorator mit dem Fall mit der builtin property. Verfolge, wie mypy Subklassen von property und Setter von Decorators verarbeitet; fertig ist dies, wenn die benutzerdefinierte Klasse wie die builtin property typgeprüft wird, ohne die gemeldeten duplicate-name- oder missing-attribute-Fehler.
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