Python Glossary Русский

Python 3.13+typingfunction

typing.is_protocol

introspection

Check whether a class is a Protocol; True for typing.Protocol subclasses (3.13+).

Syntax

typing.is_protocol(cls)

It checks declaration, not conformance: it returns True only when the class itself inherits typing.Protocol, not for an ordinary class that merely fits the protocol structurally — to test whether an object satisfies a protocol you need @runtime_checkable plus isinstance. Added in 3.13; on older versions you fall back to the private _is_protocol attribute, as the example does.

Examples

import typing
from typing import Protocol
class P(Protocol):
    def f(self) -> int: ...
check = getattr(typing, 'is_protocol', lambda c: getattr(c, '_is_protocol', False))
print(check(P))   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy