This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients AlexWaygood, JelleZijlstra, gvanrossum, kj, serhiy.storchaka, sobolevn
Date 2022-02-05.14:21:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <[email protected]>
In-reply-to
Content
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]: ...
History
Date User Action Args
2022-02-05 14:21:07serhiy.storchakasetrecipients: + serhiy.storchaka, gvanrossum, JelleZijlstra, sobolevn, kj, AlexWaygood
2022-02-05 14:21:07serhiy.storchakasetmessageid: <[email protected]>
2022-02-05 14:21:07serhiy.storchakalinkissue44796 messages
2022-02-05 14:21:07serhiy.storchakacreate