Exploring Protocols in Python - Real Python
Briefly

Protocols are essential in Python's type hint system, facilitating static type checking through tools like mypy. They address the limitations of nominal subtyping, allowing for structural subtyping that aligns with Python's duck typing philosophy. This change enables the use of objects without regard for their nominal types, ensuring static type checking of objects based on their actual structure. The Protocol class allows developers to create custom protocols, while distinguishing protocols from abstract base classes provides further clarity in type systems.
Protocols allow for structural subtyping in Python, enabling static type checking through tools like mypy, Pyright, and Pyre, overcoming limitations of nominal subtyping.
Before protocols, type checking was limited to nominal subtyping, which only considered inheritance, failing to leverage the flexibility of Python's duck typing system.
Protocols empower developers to create custom type structures with the Protocol class, enhancing the clarity and usability of type hints in Python development.
Protocols differ from abstract base classes by focusing on structural compatibility rather than inheritance hierarchies, fostering a more flexible and dynamic type system.
Read at Realpython
[
|
]