CKTIS / Python
typing.Protocol
Define a structural interface for static checking.
Version: Python 3.14; repository image, production runtime not attested
Syntax
class SizedLabel(Protocol): ...Define a structural interface for static checking.
A class with compatible members can satisfy the protocol without explicit inheritance. A plain protocol cannot serve as the target of an isinstance check unless decorated with runtime_checkable.
The example calls the required method on an independent class and catches the unsupported runtime test.
Examples
from typing import Protocol
class Labelled(Protocol):
def label(self) -> str: ...
class Item:
def label(self) -> str:
return "item"
def show(x: Labelled) -> str:
return x.label()
print(show(Item()))
try:
isinstance(Item(), Labelled)
except TypeError:
print("static protocol")
Result
item static protocol
The example calls the required method on an independent class and catches the unsupported runtime test.