I want to create a class that has some nested class that defines some contract in Python. A tenable example is a typed config object. My attempt at this is below:
from typing import Mapping
from abc import ABCMeta, abstractmethod
class BaseClass(metaclass=ABCMeta):
# If you want to implement BaseClass, you must also implement BaseConfig
class BaseConfig(metaclass=ABCMeta):
@abstractmethod
def to_dict(self) -> Mapping:
"""Converts the config to a dictionary"""
But unfortunately I can instantiate a subclass of BaseClass
without implementing BaseConfig
:
class Foo(BaseClass):
pass
if __name__ == "__main__":
foo = Foo()
Is there some way to enforce that a subclass must implement an inner class, too?