Meaning
This import statement brings the Protocol class from the typing module into the current namespace. It allows developers to define structural interfaces that classes can implicitly satisfy by matching the required methods and attributes. This is useful when you want to enforce a contract without explicit inheritance.
Primary Function
Interface definition
Communicative Purpose
Enables structural subtyping by defining protocol interfaces for duck typing.
Pattern
from typing import Protocol
Core Structure
from typing import Protocol
Função primária
Interface definition
Propósito comunicativo
Enables structural subtyping by defining protocol interfaces for duck typing.
Situações de gatilho
Python: defining a service interface for dependency injection without abstract base classes Python: specifying expected object shape for callback functions in async frameworks Python: creating pluggable parsers where any object with a parse method is accepted
Contextos
Python standard library typing module, API design, dependency injection frameworks, test mocking, structural typing patterns.
Padrão
from typing import Protocol
Estrutura central
from typing import Protocol
Colocados típicos
- Often used with @runtime_checkable for isinstance checks
- in abstract base class alternatives
- and with generic TypeVars for flexible protocols.
Substituições comuns
- Using ABC (abstract base classes) for explicit inheritance — more verbose but enforces at instantiation
- Using duck typing without protocols — flexible but lacks static checking.
Erros comuns
Forgetting to import Protocol from typing — results in NameError Using Protocol as a base class without inheriting — leads to runtime errors assuming structural subtyping Mixing Protocol with concrete method implementations — may cause confusion about interface vs implementation
Similar / contraste
ABC (abstract base classes): enforces inheritance and runtime checks; Structural typing with duck typing: no explicit interface declaration; Typing.Protocol: static structural typing without inheritance.
Interferências
Coming from Java: may expect nominal subtyping with interfaces — Python's Protocol uses structural subtyping, so explicit implementation is not required.
Família do chunk
- from typing import Protocol
- from typing import runtime_checkable
- from typing import TypeVar
Nuance
Avoid using Protocol when you need explicit inheritance semantics or when targeting Python versions <3.8 without typing_extensions; No runtime overhead as Protocol is purely a static typing construct; Boundary: Protocol members must be callable or have attributes accessible at runtime for isinstance checks with @runtime_checkable.
Efeito pragmático
Enables static type checkers to validate duck-typed interfaces, reducing runtime errors and improving IDE autocomplete.
Dica de memória
Protocol is like a duck test: if it walks like a duck and quacks like a duck, the type checker treats it as a duck.
Upgrade path
Using @runtime_checkable to enable isinstance checks with Protocol or defining generic protocols with TypeVar.
Log in to save chunks.