Thu 21 Nov 2019
Protocols
https://www.python.org/dev/peps/pep-0544/
Protocols are also known as structural subtyping. They are a way to keep the static type checker happy.
You determine whether an object supports a protocol by looking at what attributes it has (much like how Go decides if a struct meets an interface).
To define a protocol:
from typing import Protocol class SupportsConfusion(Protocol): whatsthat: str = "oy" def whatsit(self) -> str: return self.whatsthat
Now any classes with compatible methods and variables will automatically be members of that protocol, so you can use that protocol class in your static type hints without actually inheriting from it.
It a method of a protocol doesn't have an implementation, you should
decorate it with @abstractmethod
and raise a NotImplementedError
.