Abstract settable properties are not fully supported
Dieses Issue hat noch niemand übernommen.
- 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
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 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