I'm trying to use function annotations in the hope that my editor will be better at refactoring. I however am stumbling over the following problem:
I have an abstract base class Algorithm.
class Algorithm(metaclass=ABCMeta):
     def __init__(self):   
          self.foo = 'bar'
I also have a function that uses instances of subclasses of Algorithm
def get_foo(foo_algorithm):
    return foo_algoritm.foo
the input foo_algorithm can be an instance of any of the subclasses of Algorithm. How do I sensibly annotate this input? I'm looking for something along the lines off:
def get_foo(foo_algorithm: subclassof(Algorithm)):
    return foo_algoritm.foo
But I couldn't find the right way to do this.
Just use Algorithm directly:
def get_foo(foo_algorithm: Algorithm):
    return foo_algoritm.foo
and automatically any instance of a subclass will be acceptable (isinstance(foo_algorithm, Algorithm) must be true, which applies to all subclasses of a baseclass).
If you can only accept classes, then use Type[Algorithm] as the type hint:
def get_foo(foo_algorithm: Type[Algorithm]):
    return foo_algoritm().foo
See the The type of class objects section of PEP 484 -- Type Hints:
Sometimes you want to talk about class objects, in particular class objects that inherit from a given class. This can be spelled as
Type[C]whereCis a class. To clarify: whileC(when used as an annotation) refers to instances of classC,Type[C]refers to subclasses ofC.
Here I called the class object, since .foo is an instance attribute according to your code example; a class derived from Algorithm would not have such an attribute itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With