On this page
  1. typing.Protocol
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

typing.Protocol

Prev: typing.ParamSpecKwargs | Up: typing | Next: typing.SupportsAbs
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.Protocol
Tags: #public #python3 #stdlib #typing


Import

import typing
from typing import Protocol

Signature & Overview

typing.Protocol()

Base class for protocol classes.

Usage

from typing import Protocol, runtime_checkable

@runtime_checkable
class Closable(Protocol):
    def close(self) -> None: ...

class Conn:
    def close(self) -> None: pass

assert isinstance(Conn(), Closable)

Gotchas

REPL Inspect

from typing import Protocol
help(Protocol)

Linked from

Related notes