Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

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]: ...

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44796>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to