Is there such a thing as an AbstractSubClass in python? - python

The background
In python, if you were defining an Abstract Base Class which requires that its methods be overwritten, you'd do:
from abc import ABC, abstractmethod
class MyAbstractClass(ABC):
#abstractmethod
def my_method(self):
pass
The following code would then fail because it doesn't implement my_method.
class MyConcreteClass(MyAbstractClass):
pass
But what if I want to define the method requirements of a mixin class?
class MyMixin:
def my_mixin_method(self):
self.a_required_method()
The following code is then valid:
class MyBase:
def a_required_method(self):
pass
class MyFull(MyMixin, MyBase):
pass
The following code is also valid...
class MyDubious(MyMixin):
pass
But exposes an error at runtime:
MyFull().my_mixin_method() # Works
MyDubious().my_mixin_method() # Runtime error
The Question
Is there something like AbstractBaseClass which can be added to Mixin classes, to ensure that a derived class can't be instantiated unless it inherits correctly?
I'm thinking a nice API would look like:
from asc import ASC, requiredmethod
class MyRobustMixin(ASC):
#requiredmethod
def a_required_method(self):
pass
def my_mixin_method(self):
self.a_required_method()

Related

mypy error: Cannot instantiate abstract class with abstract attributes when adding types to an abstract method

I have a class I would like to add typehints to that looks as following:
import yaml
class TestClass(dict):
#classmethod
def load(cls, fname) -> "TestClass":
return cls(yaml.safe_load(""))
#property
#abc.abstractmethod
def test(self):
raise
when I run mypy on a module containing only this class I get the following error message:
error: Cannot instantiate abstract class 'TestClass' with abstract attribute 'test'
From what I have understood from other posts this has something to do with when the 'test' method is instantiated when executing the load method. Is there a way to fix this issue using typehints alone or would I need to adjust my code here?
What mypy is telling you is that TestClass.load(...) will fail, because it will try to create an instance of the abstract class TestClass.
We can fix this by requiring that cls can be called with whatever yaml.safe_load returns (I'm assuming dict here), and returns an instance of TestClass:
from typing import Callable
import abc
import yaml
class TestClass(dict):
#classmethod
def load(cls: Callable[[dict], TestClass], fname) -> "TestClass":
return cls(yaml.safe_load(""))
#property
#abc.abstractmethod
def test(self):
raise
Note that TestClass.load("foo") will now also pass type checking. This is fair to some extent, because it's also fine at runtime, until you call test() on the created instance. I think this might be a limitation of how mypy implements protocols.

Test abstract class calls parent method in Python

I'm currently refactoring features and I ended up with this abstractions
I have this classes
class AbstractClassA(SomeOtherAbstractClass):
#abstractmethod
def some_abstract_method(self):
pass
def my_method(self)):
service.some_method
class AbstractClassB(AbstractClassA):
#abstractmethod
def another_abstract_method(self):
pass
def some_abstract_method(self):
some_implementation
def my_method(self):
super().my_method()
do_any_other_stuff
And I need to test if the AbstractClassB.my_method calls super().my_method().
I've tried to test this by creating some ImplementationClass that inherits from AbstractClassB and then mocking the AbstractClassA.my_method and checking if it was called but it didn't work...
class AbstractClassBImplementation(AbstractClassB):
def some_abstract_method(self):
calls_service()
class TestAbstractClassB(TestCase):
#patch('module.submodule.AbstractClassA.my_method')
def test_class_b_calls_class_a_my_method(self, my_method_mock):
instance = AbstractClassBImplementation()
instance.my_method()
self.assertTrue(my_method_mock.called)
Someone know how to test this?

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 :)

AttributeError not generated in case of passing method reference

I have one simple doubt with respect to python 2.7:
I have created an abstract base class and a child class:
from abc import ABCMeta, abstractmethod
class Base:
"""
Abstract base class for all entities.
"""
__metaclass__ = ABCMeta
def __init__(self, name):
self.name = name
def send_data(self):
self.send_data()
class Child (Base):
def __init__(self, name):
super(Child, self).__init__(name=name)
When the object for the child class is created and the send_method is called I get the following error which is the expected behavior:
sample = Child('test')
sample.send_data()
# …
RuntimeError: maximum recursion depth exceeded
But when the send_method reference is passed in the base class and call is made to send_method by creating the child class object I think the expected behavior is to receive AttributeError but I am surprised to see no error is generated. Please explain.
from abc import ABCMeta, abstractmethod
class Base:
"""
Abstract base class for all entities.
"""
__metaclass__ = ABCMeta
def __init__(self, name, parent):
self.name = name
self.parent = parent
def send_data(self):
self.send_data
sample = Child('test')
sample.send_data()
# No error
In your first example you simply created a recursive function:
def send_data(self):
self.send_data()
This calls itself, without end, and that's why you end up with a recursion depth exception.
Your second example doesn't actually call the method:
def send_data(self):
self.send_data
The only difference here is that you forgot to use ().
None of this has anything to do with abstract base classes or inheritance. You didn't mark the send_data function as abstract, and even if you did, all that using abstractmethod does is make it impossible to create an instance of a class without a concrete implementation to replace it.
You won't get an AttributeError just because you defined a method on an ABCMeta class. And note that methods are just attributes on a class; they don't live a separate namespace. self.send_data references the bound method, not some other attribute that is separate. Referencing the method without calling it does nothing otherwise.

Redefine abstract method as static method and call it from base class

Suppose I need to implement an abstract Python interface which then will have many derived classes (each named equally but written in different modules), and in base class I heed to have a common method which will use a particular imported derived class' static method.
So my toy modules look like this:
abstract_class.py
from abc import ABCMeta, abstractmethod
from derived_class import Derived
class Abstract:
__metaclass__ = ABCMeta
#abstractmethod
def foo(self):
pass
def bar(self):
Derived.foo()
derived_class.py
from abstract_class import Abstract
class Derived(Abstract):
#staticmethod
def foo():
print 'Good news everyone!'
if __name__ == '__main__':
derived_object = Derived()
derived_object.bar()
Then of course when I'm trying to run derived_class.py, I get the Abstract name import error.
How do I properly organize this?
On the other hand, if you absolutely needed to do this without an object instance, it's possible to do with classmethods rather than staticmethods.
from abc import ABC, abstractmethod
class MyAbstractClass(ABC):
#staticmethod
#abstractmethod
def foo(label: str):
raise NotImplementedError()
#classmethod
def foo_agnostic(cls, label: str):
"""
NOTE: Here, this method doesn't have a reference to an instance of the class.
Instead, it only has a reference to the class itself; but that is enough
to call the abstract static foo() method.
"""
cls.foo(label)
class MyDerivedClass(MyAbstractClass):
#staticmethod
def foo(label: str):
print(label)
if __name__ == "__main__":
instance = MyDerivedClass()
instance.foo("Test 1") # Outputs "Test 1"
instance.foo_agnostic("Test 2") # Outputs "Test 2"
MyDerivedClass.foo_agnostic("Test 3") # Outputs "Test 3"
... in base class I heed to have a common method which will use a
particular imported derived class' static method
If I understand your question correctly, I'd say that this functionality is available out of the box with one small exception: Don't use a static method; just use a regular instance method.
Defining an abstract method in the base class will ensure that derived classes contain an implementation for that method. And, out of the box, the method defined in the derived class will get called when you call derived_object.bar().

Categories

Resources