How to enforce setter implementation in python when using ABC - python

I have a class with the following implementation:
class Device(AbstractDevice):
#property
def value(self):
return 1
it uses this ABC:
from abc import ABC
from abc import abstractmethod
class AbstractDevice(ABC):
#property
#abstractmethod
def value(self):
pass
#value.setter
#abstractmethod
def value(self):
pass
The problem is, python doesn't seem to enforce the setter part. Am I missing some decorator or is that by design?
What I would like to happen is if a user implements a class using my AbstractDevice the way I've done here, they'll get an error saying they need to implement the value.setter. Is that at all possible?

Related

python lint error if not implementing interface

I know python likes to play it nice and loose with types, but sometimes you want a plugin type interface, and want to discover before production that someone has missed something. I found abcmeta - so did the following:
class Abstract_Base(metaclass=abc.ABCMeta):
#abc.abstractmethod
def a():
pass
#abc.abstractmethod
def b():
pass
class Inheritor_One(Abstract_Base):
def a():
pass
but when I do python -m flake8.... it has no problem with that. Is there any way of writing it such that someone not overriding an abstract method will go bang at linting time?
Pylint raises abstract-method for your example:
W0223: Method 'b' is abstract in class 'Abstract_Base' but is not overridden (abstract-method)
If you actually want Inheritor_One to be an abstract class you can disable the warning locally in this class and still have the warning when you use the abstract class later on:
import abc
class Abstract_Base(metaclass=abc.ABCMeta):
#abc.abstractmethod
def a(self):
pass
#abc.abstractmethod
def b(self):
pass
class Inheritor_One(Abstract_Base):
# pylint: disable=abstract-method
def a(self):
pass

OOP in Python beginner issues, finding all methods of the same starting name

I would like to create an abstract method in parent class which would be overridden in subclasses. This method would print all methods in the given subclass which start with 'on_'.
from abc import ABC, abstractmethod
class abstract_class(ABC):
#abstractmethod
def get_all_on_methods(self):
pass
class sub(abstract_class):
an_object = sub()
def get_all_on_methods(self):
for attribute in dir(self):
if attribute.startswith("on_"):
print(attribute)
def nothin(self):
print("nothin")
def on_fallback(self):
raise NotImplementedError()
def on_no(self):
raise NotImplementedError()
sub.get_all_on_methods()
I have two problems. First, I have:
Unresolved reference 'sub'
Second, I don't know whether my approach as actually all that good.

Closest thing to a virtual call in python

Python does not provide some built-in inheritance mechanism to call implementations of base virtual or abstract methods in the derived class from the base methods
I am wondering what is the closest thing in python that would provide the following structure:
class Base(?):
def some_abstract_interface(self, **params):
raise Unimplemented()
def some_base_impl(self):
self.some_abstract_interface(self, a=4, b=3, c=2)
class Derived(Base):
#neat_override_decorator_here?
def some_abstract_interface(self, **params):
print("actual logic here {0}".format(params))
d = Derived()
d.some_base_impl()
>>>output: actual logic here a=4, b=3, c=2
You can already do that without any neat decorator:
class Base:
def some_abstract_interface(self):
raise NotImplemented
def some_base_impl(self):
self.some_abstract_interface()
class Derived(Base):
def some_abstract_interface(self):
print('actual logic here')
Derived().some_base_impl()
This outputs:
actual logic here
If you want to enforce that Base is an abstract class and cannot be used to instantiate an object directly, and that some_abstract_interface is meant to be an abstract method and always has to be overridden by an implementation of the method from a child class, you can make the base class inherit from the ABC class of the abc module and decorate abstract methods with abc.abstractmethod like this:
import abc
class Base(abc.ABC):
#abc.abstractmethod
def some_abstract_interface(self):
raise NotImplemented
def some_base_impl(self):
self.some_abstract_interface()
class Derived(Base):
def some_abstract_interface(self):
print('actual logic here')
Derived().some_base_impl()
You simply make the call yourself. That's not going to be any heavier, syntactically, then the decorator you posit.
class Derived(Base):
def some_abstract_interface(self, **params):
self.some_base_impl()
print('actual logic her {0}.format(params))
In fact, you don't even need to separate some_base_impl and some_abstract_interace; an abstract method can have an implementation but still require overriding.
from abc import ABC, abstractmethod
class Base(ABC):
#abstractmethod
def some_abstract_interface(self, **params):
pass # Put base implementation here
class Derived(Base):
def some_abstract_interface(self, **params):
super().some_abstract_interface(**params)
print("actual logic here {0}".format(params))

Proper way to implement ABC SubClass

I have an Interface class which defines the requirements to an active "in-use" class:
class Portfolio(ABC):
#abstractmethod
def update_portfolio(self):
raise NotImplementedError
#abstractmethod
def update_from_fill(self):
raise NotImplementedError
#abstractmethod
def check_signal(self, signal_event):
raise NotImplementedError
The methods update_portfolio and update_from_fill are both methods which will be the same in 99% of the required cases. Only the check_signal method will vary. Therefore, to avoid having to write the same code again and again, I have defined a base class with default methods for update_portfolio and update_from_fill:
class BaseBacktestPortfolio(Portfolio):
def __init__(self, ...):
...
def update_portfolio(self, ...):
...
def update_from_fill(self, ...):
...
Then, finally, I have a class inheriting from the BacktestPortfolio class which specifies the correct implementation of the check_signal method:
class USBacktestPortfolio(BaseBacktestPortfolio):
def check_signal(self, ...):
...
Now, the problem is that my editor complains about the BacktestPortfolio classing not having all the required abstract methods. I could ignore this, of course, but the perfect scenario would be if I could make sure that it is not possible to instantiate an object form the BacktestPortfolio class.
Is this possible? And/or is there a more correct way to implement a structure like this?
I could ignore this, of course, but the perfect scenario would be if I could make sure that it is not possible to instantiate an object from the BacktestPortfolio class.
That is the case in your example already:
>>> BaseBacktestPortfolio.mro()
[__main__.BaseBacktestPortfolio, __main__.Portfolio, abc.ABC, object]
>>> BaseBacktestPortfolio()
TypeError: Can't instantiate abstract class BaseBacktestPortfolio with abstract methods check_signal
Since ABC and ABCMeta are just regular types, their features are inherited. This includes their guards against instantiating incomplete classes. Your BaseBacktestPortfolio already is an abstract class.
The warning from your IDE/linter/... exists specifically to warn you that instantiating BaseBacktestPortfolio is not possible.
You can make the BaseBacktestPortfolio also as Abstract class.
from abc import ABC, abstractmethod
class Portfolio(ABC):
#abstractmethod
def update_portfolio(self):
pass
#abstractmethod
def update_from_fill(self):
pass
#abstractmethod
def check_signal(self, signal_event):
pass
class BaseBacktestPortfolio(Portfolio, ABC):
def update_portfolio(self):
print("updated portfolio")
def update_from_fill(self):
print("update from fill")
#abstractmethod
def check_signal(self):
pass
class USBacktestPortfolio(BaseBacktestPortfolio):
def check_signal(self):
print("checked signal")
Also notice that you don't need raise NotImplementedError inside abstract method. You can just pass. Its more Pythonic :)

Is it a good idea to define properties in Python interfaces?

Is it a good practice to define properties in an interface like this?
class MyInterface(object):
def required_method(self):
raise NotImplementedError
#property
def required_property(self):
raise NotImplementedError
I'd use a ABC class for that, but yes; you can even use a #abstractproperty for that very use-case.
from abc import ABCMeta, abstractproperty, abstractmethod
class MyInterface(object):
__metaclass__ = ABCMeta
#abstractmethod
def required_method(self):
pass
#abstractproperty
def required_property(self):
pass
Subclasses of the ABC are still free to implement required_property as an attribute instead; the ABC will only verify the existence of required_property, not what type it is.

Categories

Resources