(self, args)
| 1420 | |
| 1421 | @_tp_cache |
| 1422 | def __getitem__(self, args): |
| 1423 | # Parameterizes an already-parameterized object. |
| 1424 | # |
| 1425 | # For example, we arrive here doing something like: |
| 1426 | # T1 = TypeVar('T1') |
| 1427 | # T2 = TypeVar('T2') |
| 1428 | # T3 = TypeVar('T3') |
| 1429 | # class A(Generic[T1]): pass |
| 1430 | # B = A[T2] # B is a _GenericAlias |
| 1431 | # C = B[T3] # Invokes _GenericAlias.__getitem__ |
| 1432 | # |
| 1433 | # We also arrive here when parameterizing a generic `Callable` alias: |
| 1434 | # T = TypeVar('T') |
| 1435 | # C = Callable[[T], None] |
| 1436 | # C[int] # Invokes _GenericAlias.__getitem__ |
| 1437 | |
| 1438 | if self.__origin__ in (Generic, Protocol): |
| 1439 | # Can't subscript Generic[...] or Protocol[...]. |
| 1440 | raise TypeError(f"Cannot subscript already-subscripted {self}") |
| 1441 | if not self.__parameters__: |
| 1442 | raise TypeError(f"{self} is not a generic class") |
| 1443 | |
| 1444 | # Preprocess `args`. |
| 1445 | if not isinstance(args, tuple): |
| 1446 | args = (args,) |
| 1447 | args = _unpack_args(*(_type_convert(p) for p in args)) |
| 1448 | new_args = self._determine_new_args(args) |
| 1449 | r = self.copy_with(new_args) |
| 1450 | return r |
| 1451 | |
| 1452 | def _determine_new_args(self, args): |
| 1453 | # Determines new __args__ for __getitem__. |
nothing calls this directly
no test coverage detected