pydoc > typing.Protocol

Help on class Protocol in typing:

typing.Protocol = class Protocol(Generic)
 |  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[T](Protocol):
 |          def meth(self) -> T:
 |              ...
 |
 |  Method resolution order:
 |      Protocol
 |      Generic
 |      builtins.object
 |
 |  Class methods defined here:
 |
 |  __init_subclass__(*args, **kwargs)
 |      Function to initialize subclasses.
 |
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |
 |  __abstractmethods__ = frozenset()
 |
 |  __parameters__ = ()
 |
 |  __protocol_attrs__ = set()
 |
 |  ----------------------------------------------------------------------
 |  Class methods inherited from Generic:
 |
 |  __class_getitem__(...)
 |      Parameterizes a generic class.
 |
 |      At least, parameterizing a generic class is the *main* thing this
 |      method does. For example, for some generic class `Foo`, this is called
 |      when we do `Foo[int]` - there, with `cls=Foo` and `params=int`.
 |
 |      However, note that this method is also called when defining generic
 |      classes in the first place with `class Foo[T]: ...`.

Generated by phpman v4.12.29 · Markdown · JSON Author: Che Dong Under GNU General Public License
2026-12-04 12:23 @216.73.216.5
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!