pydoc > typing.Protocol

📘 NAME

typing.Protocol — class Protocol(Generic)

🚀 Quick Reference

Use CaseCommand / CodeDescription
🏗️ Define a protocolclass Proto(Protocol):Base class for protocol classes.
🦆 Structural subtypingclass C: def meth(self) -> int: ...Static duck-typing recognized by type checkers.
✅ Runtime checkable@typing.runtime_checkableSimple-minded runtime protocol checking attribute presence.
🧬 Generic protocolclass GenProto(Protocol[T]):Protocols can be generic over types.

📖 Description

Base class for protocol classes.

Protocol classes are defined as:

class Proto(Protocol):
    def meth(self) -> int:
        ...

Such classes are primarily used with static type checkers that recognize structural subtyping (static duck-typing), for example:

class C:
    def meth(self) -> int:
        return 0

def func(x: Proto) -> int:
    return x.meth()

func(C())  # Passes static type check

See PEP 544 for details. Protocol classes decorated with @typing.runtime_checkable act as simple-minded runtime protocols that check only the presence of given attributes, ignoring their type signatures. Protocol classes can be generic, they are defined as:

class GenProto(Protocol[T]):
    def meth(self) -> T:
        ...

🧬 Method Resolution Order

  1. Protocol
  2. Generic
  3. builtins.object

⚙️ Class Methods Defined Here

🗄️ Data and Other Attributes

🔗 Class Methods Inherited from Generic

typing.Protocol
📘 NAME 🚀 Quick Reference 📖 Description 🧬 Method Resolution Order ⚙️ Class Methods Defined Here 🗄️ Data and Other Attributes 🔗 Class Methods Inherited from Generic

Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-02 19:13 @216.73.216.239
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!