Protocols Check Names Not Signatures
BadWriter.write takes no arguments besides self, which does not match the Writer protocol's write(self, data: str) -> None signature:
from typing import Protocol, runtime_checkable
@runtime_checkable
class Writer(Protocol):
def write(self, data: str) -> None: ...
class BadWriter:
def write(self) -> int:
return 42
def send(w: Writer, payload: str) -> None:
w.write(payload)
bw = BadWriter()
print(isinstance(bw, Writer))
send(bw, "hello")
Neither the w: Writer annotation nor payload: str is checked by the interpreter. What happens when this runs?
Sign in to answer questions and track your progress
Sign In