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

Function _generic_class_getitem

Lib/typing.py:1162–1219  ·  view source on GitHub ↗

Parameterizes a generic class. At least, parameterizing a generic class is the *main* thing this method does. For example, for some generic class `Foo`, this is called when we do `Foo[int]` - there, with `cls=Foo` and `args=int`. However, note that this method is also called when d

(cls, args)

Source from the content-addressed store, hash-verified

1160
1161@_tp_cache
1162def _generic_class_getitem(cls, args):
1163 """Parameterizes a generic class.
1164
1165 At least, parameterizing a generic class is the *main* thing this method
1166 does. For example, for some generic class `Foo`, this is called when we
1167 do `Foo[int]` - there, with `cls=Foo` and `args=int`.
1168
1169 However, note that this method is also called when defining generic
1170 classes in the first place with `class Foo(Generic[T]): ...`.
1171 """
1172 if not isinstance(args, tuple):
1173 args = (args,)
1174
1175 args = tuple(_type_convert(p) for p in args)
1176 is_generic_or_protocol = cls in (Generic, Protocol)
1177
1178 if is_generic_or_protocol:
1179 # Generic and Protocol can only be subscripted with unique type variables.
1180 if not args:
1181 raise TypeError(
1182 f"Parameter list to {cls.__qualname__}[...] cannot be empty"
1183 )
1184 if not all(_is_typevar_like(p) for p in args):
1185 raise TypeError(
1186 f"Parameters to {cls.__name__}[...] must all be type variables "
1187 f"or parameter specification variables.")
1188 if len(set(args)) != len(args):
1189 raise TypeError(
1190 f"Parameters to {cls.__name__}[...] must all be unique")
1191 else:
1192 # Subscripting a regular Generic subclass.
1193 try:
1194 parameters = cls.__parameters__
1195 except AttributeError as e:
1196 init_subclass = getattr(cls, '__init_subclass__', None)
1197 if init_subclass not in {None, Generic.__init_subclass__}:
1198 e.add_note(
1199 f"Note: this exception may have been caused by "
1200 f"{init_subclass.__qualname__!r} (or the "
1201 f"'__init_subclass__' method on a superclass) not "
1202 f"calling 'super().__init_subclass__()'"
1203 )
1204 raise
1205 for param in parameters:
1206 prepare = getattr(param, '__typing_prepare_subst__', None)
1207 if prepare is not None:
1208 args = prepare(cls, args)
1209 _check_generic_specialization(cls, args)
1210
1211 new_args = []
1212 for param, new_arg in zip(parameters, args):
1213 if isinstance(param, TypeVarTuple):
1214 new_args.extend(new_arg)
1215 else:
1216 new_args.append(new_arg)
1217 args = tuple(new_args)
1218
1219 return _GenericAlias(cls, args)

Callers

nothing calls this directly

Calls 8

_type_convertFunction · 0.85
_is_typevar_likeFunction · 0.85
setFunction · 0.85
_GenericAliasClass · 0.85
prepareFunction · 0.50
extendMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…