Typing and Generics

typing.Protocol

Defines structural requirements for static typing: an object can satisfy the protocol without explicit inheritance. Runtime instance checks require @runtime_checkable and do not validate signatures.

Formal Syntax & Structure

python syntaxpython
class Name(typing.Protocol): ...

Return Value

protocol class

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import Protocol
class Reader(Protocol):    def read(self) -> str: ...class Memory:    def read(self) -> str: return 'data'def fetch(reader: Reader) -> str: return reader.read()print(fetch(Memory()))
Python Reference