Python
typing.python.org › en › latest › spec › protocol.html
Protocols — typing documentation
However, it should be possible for protocol types to implement custom instance and class checks when this makes sense, similar to how Iterable and other ABCs in collections.abc and typing already do it, but this is limited to non-generic and unsubscripted generic protocols (Iterable is statically equivalent to Iterable[Any]). The typing module will define a special @runtime_checkable class decorator that provides the same semantics for class and instance checks as for collections.abc classes, essentially making them “runtime protocols”:
Is there a downside to `typing.runtime_checkable`?
I can’t find one, and I’m curious why the capability wasn’t just added to the Protocol type by default! from typing import Protocol, runtime_checkable @runtime_checkable class Runnable(Protocol): """This is nothing new!""" def __run__(self) -> None: """Run something.""" class RoadRunner: ... More on discuss.python.org
`isinstance` on `runtime_checkable` `Protocol` has side-effects for `@property` methods
from typing import Protocol, runtime_checkable @runtime_checkable class X(Protocol): @property def myproperty(self): ... More on github.com
isinstance check of runtime_checkable Protocol returns true for data-protocol if explicitly subclassed without non-method member
Asked this question on stackoverflow but I am copying here as this seems to be an untended results based on PEP 544. If a protocol is implemented with a requried attribute i.e. name @runtime_checka... More on github.com
Instance checking with data Protocols?
Hi! I’m having a little trouble understanding what I can do with the new runtime checkable protocols. The PEP says that data protocols (those with typed attributes) can be checked with isinstance, so I’ve tried this: from typing import Protocol, runtime_checkable @runtime_checkable class ... More on discuss.python.org
Videos
10:10
You NEED to know about Python protocols - YouTube
15:31
Protocols vs ABCs in Python - When to Use Which One? - YouTube
28:40
Protocols in Python: Why You Need Them - presented by Rogier van ...
23:45
Protocol Or ABC In Python - When to Use Which One? - YouTube
05:29
typing: Protocol + @runtime_checkable (intermediate) anthony explains ...
09:03
structural subtyping in python with Protocol! (intermediate) anthony ...
Mypy
mypy.readthedocs.io › en › stable › protocols.html
Protocols and structural subtyping - mypy 1.19.1 documentation
You can use a protocol class with isinstance() if you decorate it with the @runtime_checkable class decorator.
Python.org
discuss.python.org › python help
Is there a downside to `typing.runtime_checkable`? - Python Help - Discussions on Python.org
November 4, 2022 - Is there a downside to adding the runtime_checkable decorator to a Protocol definition? I can’t find one, and I’m curious why the capability wasn’t just added to the Protocol type by default! from typing import Protocol, runtime_checkable @runtime_checkable class Runnable(Protocol): """This is nothing new!""" def __run__(self) -> None: """Run something.""" class RoadRunner: """A concrete implementation.""" def __run__(self) -> None: """Run!""" bird = RoadRunner() assert isin...
GitHub
github.com › python › cpython › issues › 102433
`isinstance` on `runtime_checkable` `Protocol` has side-effects for `@property` methods · Issue #102433 · python/cpython
March 5, 2023 - For example: from typing import Protocol, runtime_checkable @runtime_checkable class X(Protocol): @property def myproperty(self): ... class Y: @property def myproperty(self): raise RuntimeError("hallo") isinstance(Y(), X) will raise the ...
Author chrisjsewell
Python
typing.python.org › en › latest › reference › protocols.html
Protocols and structural subtyping — typing documentation
from typing import Protocol, runtime_checkable @runtime_checkable class Portable(Protocol): handles: int class Mug: def __init__(self) -> None: self.handles = 1 def use(handles: int) -> None: ... mug = Mug() if isinstance(mug, Portable): # Works at runtime!
GitHub
github.com › python › typing › issues › 800
isinstance check of runtime_checkable Protocol returns true for data-protocol if explicitly subclassed without non-method member · Issue #800 · python/typing
January 4, 2021 - If a protocol is implemented with a requried attribute i.e. name · @runtime_checkable class DuckProtocol(Protocol): """Protocol for a duck""" name: str @abstractmethod def quack(self): raise NotImplementedError
Author mwjohnson56
Python
peps.python.org › pep-0544
PEP 544 – Protocols: Structural subtyping (static duck typing) | peps.python.org
March 5, 2017 - Add a class attribute _is_protocol = True if that is the case. Verify that a protocol class only has protocol base classes in the MRO (except for object). Implement @runtime_checkable that allows __subclasshook__() performing structural instance and subclass checks as in collections.abc classes.
Python.org
discuss.python.org › python help
Instance checking with data Protocols? - Python Help - Discussions on Python.org
November 28, 2019 - Hi! I’m having a little trouble understanding what I can do with the new runtime checkable protocols. The PEP says that data protocols (those with typed attributes) can be checked with isinstance, so I’ve tried this: from typing import Protocol, runtime_checkable @runtime_checkable class Foo(Protocol): x: int class Bar(object): def __init__(self, x): self.x = x assert isinstance(Bar(10), Foo) assert not isinstance(Bar(10.), Foo) # This fails, which I didn't expect I feel li...
Top answer 1 of 2
16
The selected answer, although not wrong, doesn't reflect the actual use of Protocol, which is providing structural subtyping. Nominal subtyping was always available to Python 3.
from typing import Protocol, runtime_checkable
@runtime_checkable
class Closable(Protocol):
def close(self):
...
class AnyClass:
def close(self):
...
issubclass(AnyClass, Closable)
#> True
Also, runtime_checkable is only needed in the base class, even if the class being checked is a Protocol subclass.
class ExtendedProtocol(Closable, Protocol):
...
class AnotherProtocol(Protocol):
def close(self):
...
class NotAProtocol(Closable):
# just inherits the protocol's default implementation
...
issubclass(ExtendedProtocol, Closable)
#> True
issubclass(AnotherProtocol, Closable)
#> True
issubclass(NotAProtocol, Closable)
#> True
2 of 2
5
For more on the topic of python Protocols, see
- https://mypy.readthedocs.io/en/latest/protocols.html#using-isinstance-with-protocols
In Python 3.6.7, one way to solve this is to use the @runtime_checkable decorator:
>>> from typing_extensions import Protocol
>>> from typing_extensions import runtime_checkable
>>> @runtime_checkable
... class CustomProtocol(Protocol):
... def custom(self):
... ...
...
>>> @runtime_checkable
... class ExtendedCustomProtocol(CustomProtocol, Protocol):
... def extended(self):
... ...
...
>>> issubclass(ExtendedCustomProtocol, CustomProtocol)
True
Python.org
discuss.python.org › ideas
Optional strict mode for @runtime_checkable - Ideas - Discussions on Python.org
April 15, 2025 - Idea Extend @typing.runtime_checkable with optional parameters: def runtime_checkable(*, values: bool = False, signatures: bool = False, return_annotations: bool | None = None, ) values – check runtime types of attribute values in isinstance(obj, Proto) for data (non-callable) members, signatures – check member function signatures (both parameters and return annotation) in isinstance(obj, Proto) and issubclass(Cls, Proto), return_annotations – adjust checking of return annotation ...
Stack Overflow
stackoverflow.com › questions › 77712392 › type-alias-for-a-union-of-two-runtime-checkable-protocols-triggers-mypys-cann
python - Type alias for a union of two @runtime_checkable Protocols triggers mypy's "Cannot use parameterized generics in instance checks" - Stack Overflow
from typing import Protocol, runtime_checkable, SupportsIndex, SupportsInt @runtime_checkable class SupportsTrunc(Protocol): def __trunc__(self) -> int: ...