CKTIS / Python
Type annotations and protocols
An annotation can describe the interface a static checker expects without requiring a shared base class.
Version: Python 3.14; repository image, production runtime not attested
Syntax
class Reader(Protocol):
def read(self) -> str: ...An annotation can describe the interface a static checker expects without requiring a shared base class.
Protocol specifies methods and their signatures structurally. Function calls do not enforce annotations. runtime_checkable permits isinstance checks for member presence without checking parameter or return types.
Message yields READY. NeedsKey passes the read presence check even though calling it without an argument raises TypeError.
from typing import Protocol,runtime_checkable,get_type_hints
@runtime_checkable
class Reader(Protocol):
def read(self)->str: ...
class Bad:
def read(self, extra): return extra
def display(source:Reader): return source.read()
assert get_type_hints(display)['source'] is Reader
assert isinstance(Bad(),Reader)
try: display(Bad())
except TypeError: pass
else: raise AssertionError
Examples
from typing import Protocol, runtime_checkable
@runtime_checkable
class Reader(Protocol):
def read(self) -> str: ...
class Message:
def read(self) -> str:
return "ready"
class NeedsKey:
def read(self, key):
return key
def display(source: Reader):
return source.read().upper()
print(display(Message()), isinstance(NeedsKey(), Reader))
Result
READY True
Message yields READY. NeedsKey passes the read presence check even though calling it without an argument raises TypeError.