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

Function _generic_init_subclass

Lib/typing.py:1222–1259  ·  view source on GitHub ↗
(cls, *args, **kwargs)

Source from the content-addressed store, hash-verified

1220
1221
1222def _generic_init_subclass(cls, *args, **kwargs):
1223 super(Generic, cls).__init_subclass__(*args, **kwargs)
1224 tvars = []
1225 if '__orig_bases__' in cls.__dict__:
1226 error = Generic in cls.__orig_bases__
1227 else:
1228 error = (Generic in cls.__bases__ and
1229 cls.__name__ != 'Protocol' and
1230 type(cls) != _TypedDictMeta)
1231 if error:
1232 raise TypeError("Cannot inherit from plain Generic")
1233 if '__orig_bases__' in cls.__dict__:
1234 tvars = _collect_type_parameters(cls.__orig_bases__, validate_all=True)
1235 # Look for Generic[T1, ..., Tn].
1236 # If found, tvars must be a subset of it.
1237 # If not found, tvars is it.
1238 # Also check for and reject plain Generic,
1239 # and reject multiple Generic[...].
1240 gvars = None
1241 basename = None
1242 for base in cls.__orig_bases__:
1243 if (isinstance(base, _GenericAlias) and
1244 base.__origin__ in (Generic, Protocol)):
1245 if gvars is not None:
1246 raise TypeError(
1247 "Cannot inherit from Generic[...] multiple times.")
1248 gvars = base.__parameters__
1249 basename = base.__origin__.__name__
1250 if gvars is not None:
1251 tvarset = set(tvars)
1252 gvarset = set(gvars)
1253 if not tvarset <= gvarset:
1254 s_vars = ', '.join(str(t) for t in tvars if t not in gvarset)
1255 s_args = ', '.join(str(g) for g in gvars)
1256 raise TypeError(f"Some type variables ({s_vars}) are"
1257 f" not listed in {basename}[{s_args}]")
1258 tvars = gvars
1259 cls.__parameters__ = tuple(tvars)
1260
1261
1262def _is_dunder(attr):

Callers

nothing calls this directly

Calls 6

superClass · 0.85
_collect_type_parametersFunction · 0.85
setFunction · 0.85
strFunction · 0.85
__init_subclass__Method · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…