python / python/mypy

Class inheriting from property doesn't typecheck properly when used as a decorator

Offen
#6,158 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug false-positive priority-1-normal topic-descriptors
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

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Ö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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.