Message412575
I have created an alternative PR 31143 which adds special method __typing_subst__ in TypeVar and ParamSpec instead of __parameters__ and __getitem__. It has almost the same effect except making TypeVar and ParamSpec and subscriptable. The Python code in typing.py and _collections_abc.py is simplified, but the C code is little changed.
But on other hand, the reasons for PR 27511:
1. We already implemented __getitem__ for a Concatenate alias, although it is never documented. It is not even necessary, it is a pure implementation detail which makes the code for nested variables substitution simpler. Adding __parameters__ and __getitem__ in TypeVar and ParamSpec would have the same effect on simplification of the code.
2. There is a use case for subscription of TypeVar. For example:
if version < 2:
RetType = T
elif version < 3:
RetType = Optional[T]
else:
RetType = Optional[T] | Literal[Unknown]
def get_int(precision: int) -> RetType[int]: ...
def get_str(encoding: str) -> RetType[str]: ...
def get_any(converter: Callable[[str], T]) -> RetType[T]: ... |
|
| Date |
User |
Action |
Args |
| 2022-02-05 14:21:07 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, gvanrossum, JelleZijlstra, sobolevn, kj, AlexWaygood |
| 2022-02-05 14:21:07 | serhiy.storchaka | set | messageid: <[email protected]> |
| 2022-02-05 14:21:07 | serhiy.storchaka | link | issue44796 messages |
| 2022-02-05 14:21:07 | serhiy.storchaka | create | |
|