Fellow pythonistas,
- We adopted Python type hints in a new project.
- The project itself is composed of couple of modules that don't share a common repository, but they do have interdependencies. Since it is a pure algorithmic research project, we decided to use decopuled components that program through an interface, so we have an abstract class that all those that want to substitute current module for another one, have to comply with this interface. Say,
class ClusteringInterface(ABC):
def __init__(self, n_groups: int, data_generator: Generator[Dict, None, None]):
self.n_groups = n_groups
self.data_generator = data_generator
...
@abstractmethod
def foo(self):
pass
Now, I do want to accept anything that complies with ClusteringInterface, so it implements foo methods and complies with the init function. Can I somehow define a custom type so that mypy will indeed throw an error when something pretends to be ClusteringInterface, but is really something completely different?
I have seen Raymond Hettinger lecture about Abstract classes and I understand that python itself cannot guarantee that.
So the more general question would be: What is the recommended solution so that I actually can enforce compliance with an interface? I realize that the question is a little bit too broad, but I don't really know where to look for more information.