According to documentation (https://docs.python.org/3/library/abc.html), abstract setter (and getter) should be defined as:
class C(ABC):
@property
@abstractmethod
def my_abstract_property(self):
...
@my_abstract_property.setter
@abstractmethod
def my_abstract_property(self, val):
...
I have created an example:
from abc import ABC, abstractmethod
class C(ABC):
@property
@abstractmethod
def my_abstract_property(self):
pass
@my_abstract_property.setter
@abstractmethod
def my_abstract_property(self, val):
pass
class D(C):
my_abstract_property = 1
if __name__ == '__main__':
x = C()
If I run it, I get an error: "TypeError: Can't instantiate abstract class C with abstract methods my_abstract_property
" which is OK.
if I change if __name__ == "__main__"
part of code to:
if __name__ == '__main__':
x = D()
print(x.my_abstract_property)
x.my_abstract_property = 5
print(x.my_abstract_property)
Output is:
1
5
Process finished with exit code 0
As you can see in class D() I am not forced to implement setter at all.
Furthermore, while using Pycharm for impolementation of class D(), if I press alt+enter, implement abstract methods, I get only one option: my_abstract_property(self: C)
. After choosing this single option, only getter is generated by Pycharm:
@property
def my_abstract_property(self):
pass
So, my question is how to force subclass to implement setter (and getter) for an abstract property (in Pycharm)?