Typing and Generics

typing.runtime_checkable

Enables shallow runtime instance checks on a protocol, using attribute presence rather than type signatures. Data protocols do not generally support issubclass; member names are frozen at protocol creation in 3.12+.

Formal Syntax & Structure

python syntaxpython
@typing.runtime_checkable

Parameters & Attributes

NameTypeDescription
targetclassObject to decorate.

Return Value

protocol class

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import Protocol, runtime_checkable
@runtime_checkableclass Reader(Protocol):    def read(self) -> str: ...class Memory:    def read(self) -> str: return 'data'print((isinstance(Memory(), Reader), isinstance(object(), Reader)))
Python Reference