Search code examples
pythoninheritanceabstract-classextendstyping

Specify type that is a subtype


We are specifying an abstract class as well as implementation like this:

from abc import ABC
class Base(ABC):
    ...

class Type1(Base):
    def __init__(self, var1: str):
        ...

then we are trying to use it in this way

from typing import Dict
constructors: Dict[str, Base]= {'type1': Type1}

constructors['type1']("")  # Error here

But we are getting an error in our IDE that says Base is not callable -- which is true. How do we specify that the our dictionary values are descendants of the Base class which are callable?


Solution

  • The annotation Dict[str,Base] indicates a dict that maps str keys to values that are instances of Base (or its subclasses). You intend the values to be the class Base (or one of its subclasses) itself, so you need to use Type[Base] instead. (Just like Base et al. are instances of the (meta)class type.)

    constructors: Dict[str, Type[Base]] = {'type1': Type1}
    constructors['type1']("")
    

    Whether your IDE is smart enough tell if constructors['type1'] is abstract or not is another matter, though.