(self, args)
| 1369 | |
| 1370 | @_tp_cache |
| 1371 | def __getitem__(self, args): |
| 1372 | # Parameterizes an already-parameterized object. |
| 1373 | # |
| 1374 | # For example, we arrive here doing something like: |
| 1375 | # T1 = TypeVar('T1') |
| 1376 | # T2 = TypeVar('T2') |
| 1377 | # T3 = TypeVar('T3') |
| 1378 | # class A(Generic[T1]): pass |
| 1379 | # B = A[T2] # B is a _GenericAlias |
| 1380 | # C = B[T3] # Invokes _GenericAlias.__getitem__ |
| 1381 | # |
| 1382 | # We also arrive here when parameterizing a generic `Callable` alias: |
| 1383 | # T = TypeVar('T') |
| 1384 | # C = Callable[[T], None] |
| 1385 | # C[int] # Invokes _GenericAlias.__getitem__ |
| 1386 | |
| 1387 | if self.__origin__ in (Generic, Protocol): |
| 1388 | # Can't subscript Generic[...] or Protocol[...]. |
| 1389 | raise TypeError(f"Cannot subscript already-subscripted {self}") |
| 1390 | if not self.__parameters__: |
| 1391 | raise TypeError(f"{self} is not a generic class") |
| 1392 | |
| 1393 | # Preprocess `args`. |
| 1394 | if not isinstance(args, tuple): |
| 1395 | args = (args,) |
| 1396 | args = _unpack_args(*(_type_convert(p) for p in args)) |
| 1397 | new_args = self._determine_new_args(args) |
| 1398 | r = self.copy_with(new_args) |
| 1399 | return r |
| 1400 | |
| 1401 | def _determine_new_args(self, args): |
| 1402 | # Determines new __args__ for __getitem__. |
nothing calls this directly
no test coverage detected