Complete worked example
python
from typing import Protocol
class Sink(Protocol):
def write(self, text: str) -> int: ...
class Buffer:
def __init__(self) -> None:
self.parts: list[str] = []
def write(self, text: str) -> int:
self.parts.append(text)
return len(text)
def send(sink: Sink, text: str) -> int:
return sink.write(text)
def inspect_example(text):
buffer = Buffer()
count = send(buffer, text)
return {'count': count, 'text': ''.join(buffer.parts)}- Reasoning
- Sink declares a write operation accepting str and returning int. Buffer appends the received string and returns len(text); send forwards exactly one call through that interface. The report returns both the count and the joined stored text.