Protocol defines a structural interface: any class with matching methods is compatible, no inheritance required
A Protocol from typing describes a set of methods and attributes that a type must provide. Unlike an abstract base class, a class does not need to inherit from the Protocol to be considered compatible. A static type checker will accept any class that structurally matches the Protocol, which brings duck typing into the static type system. This is called structural typing. For example, if you define a Protocol with a close() method, any object with close() satisfies it, whether or not it subclasses the Protocol. You can also use @runtime_checkable to allow isinstance checks against the Protocol at runtime, but that only checks method presence, not signatures. Protocols are ideal for defining interfaces for dependency injection, plugin systems, and callbacks without forcing inheritance. The trade-off is that errors can be less obvious: a missing method is caught by the type checker, not at runtime, unless you use runtime_checkable.
Protocol classes are defined with class MyProto(Protocol): ... and list the required methods and attributes.
A class satisfies the Protocol if it has all the required members with compatible types. No explicit inheritance is needed.
@runtime_checkable allows isinstance and issubclass checks, but only verifies method names, not signatures or types.
Protocols can be generic and can inherit from other Protocols.
Trade-off: structural typing is flexible and decoupled, but it can be harder to discover all implementers. Nominal typing (ABCs) is more explicit.
Common mistake: expecting runtime enforcement from a Protocol. It only enforces at type-check time unless runtime_checkable is used, and even then only superficially.
Common mistake: putting implementation code in a Protocol. It should only declare the interface.
Version note: Protocol was introduced in Python 3.8 via typing.Protocol (PEP 544). It is also available in typing_extensions for older versions.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience