python / python/mypy

Abstract settable properties are not fully supported

Offen
#13,649 2 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Python docs say that properties can have individual abstract parts: https://docs.python.org/3/library/abc.html#abc.abstractproperty

So, let's see what mypy thinks about it.

abstract getter

import abc

class Base(abc.ABC):
    @property
    @abc.abstractmethod
    def x(self) -> int: ...

class Child(Base):
    ...

c = Child()  # error: Cannot instantiate abstract class "Child" with abstract attribute "x"

It is supported ✅

abstract setter

import abc

class Base(abc.ABC):
    @property
    def x(self) -> int: ...

    @x.setter
    @abc.abstractmethod
    def x(self, arg: int) -> None: ...

class Child(Base):
    ...

c = Child()
c.x = 2
reveal_type(c)  # Revealed type is "ex.Child"
# Runtime:
# TypeError: Can't instantiate abstract class Child with abstract method x

Not supported 🚫

Should instead say: # error: Cannot instantiate abstract class "Child" with abstract attribute "x"
Or even better with abstract property setter "x"

abstract getter and setter

import abc

class Base(abc.ABC):
    @property
    @abc.abstractmethod
    def x(self) -> int: ...

    @x.setter
    @abc.abstractmethod
    def x(self, arg: int) -> None: ...

class Child(Base):
    ...

c = Child()  # E: Cannot instantiate abstract class "Child" with abstract attribute "x"

However, this does not fully cover this case:

import abc

class Base(abc.ABC):
    @property
    @abc.abstractmethod
    def x(self) -> int: ...

    @x.setter
    @abc.abstractmethod
    def x(self, arg: int) -> None: ...

class Child(Base):
    @property  # E: Read-only property cannot override read-write property
    def x(self) -> int: ...

Child()  # ok

We can add additional note about x.setter being abstract. Or we can keep this as is.

Related:

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

Beginne damit, die Beispiele für abstrakte Setter und abstrakte Getter/Setter aus dem Issue zu reproduzieren und die Diagnosen von mypy mit dem Laufzeitverhalten von Python zu vergleichen. Verfolge die Behandlung abstrakter Properties und Setter und füge anschließend Abdeckung hinzu, sodass der Fall mit abstraktem Setter die erwartete Diagnose für ein abstraktes Attribut oder einen Property-Setter ausgibt, ohne das bestehende Getter-Verhalten zu beeinträchtigen.

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
38/100

Neue Issues direkt in Ihr Postfach

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