MCPcopy Index your code
hub / github.com/python/cpython / __instancecheck__

Method __instancecheck__

Lib/typing.py:2076–2119  ·  view source on GitHub ↗
(cls, instance)

Source from the content-addressed store, hash-verified

2074 return _abc_subclasscheck(cls, other)
2075
2076 def __instancecheck__(cls, instance):
2077 # We need this method for situations where attributes are
2078 # assigned in __init__.
2079 if cls is Protocol:
2080 return type.__instancecheck__(cls, instance)
2081 if not getattr(cls, "_is_protocol", False):
2082 # i.e., it's a concrete subclass of a protocol
2083 return _abc_instancecheck(cls, instance)
2084
2085 if (
2086 not getattr(cls, '_is_runtime_protocol', False) and
2087 not _allow_reckless_class_checks()
2088 ):
2089 raise TypeError("Instance and class checks can only be used with"
2090 " @runtime_checkable protocols")
2091
2092 if getattr(cls, '__typing_is_deprecated_inherited_runtime_protocol__', False):
2093 # See GH-132604.
2094 import warnings
2095
2096 depr_message = (
2097 f"{cls!r} isn't explicitly decorated with @runtime_checkable but "
2098 "it is used in issubclass() or isinstance(). Instance and class "
2099 "checks can only be used with @runtime_checkable protocols. "
2100 "This will raise a TypeError in Python 3.20."
2101 )
2102 warnings.warn(depr_message, category=DeprecationWarning, stacklevel=2)
2103
2104 if _abc_instancecheck(cls, instance):
2105 return True
2106
2107 getattr_static = _lazy_load_getattr_static()
2108 for attr in cls.__protocol_attrs__:
2109 try:
2110 val = getattr_static(instance, attr)
2111 except AttributeError:
2112 break
2113 # this attribute is set by @runtime_checkable:
2114 if val is None and attr not in cls.__non_callable_proto_members__:
2115 break
2116 else:
2117 return True
2118
2119 return False
2120
2121
2122@classmethod

Callers

nothing calls this directly

Calls 5

getattr_staticFunction · 0.90
__instancecheck__Method · 0.45
warnMethod · 0.45

Tested by

no test coverage detected