[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

Serhiy Storchaka report at bugs.python.org
Thu Feb 3 15:23:24 EST 2022


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

It does not have a use case of T[int] in mind. It is an implementation detail which simplifies the code, makes runtime checks more strict and makes types.GenericAlias more similar to typing._GenericAlias.

For example, currently:

>>> A = List[T]
>>> B = list[T]
>>> A[None]
typing.List[NoneType]
>>> B[None]
list[None]
>>> A['X']
typing.List[ForwardRef('X')]
>>> B['X']
list['X']

With the proposed change:

>>> B[None]
list[NoneType]
>>> B['X']
list[ForwardRef('X')]

Meanless expressions like A[42], A[Final], A[Final[int]], A[ClassVar], A[ClassVar[int]], etc which are silently accepted will now be errors.

The code simplification (especially for the C code) is my primary goal, and the rest is a nice side effect. It is possible to add more strict runtime checks and conversions without making TypeVar and ParamSpec subscriptable, but the code will not be so simple.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44796>
_______________________________________


More information about the Python-bugs-list mailing list