You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add __parameters__ and __getitem__ in TypeVar and ParamSpec #88959
bpo-44794: Merge tests for typing.Callable and collection.abc.Callable
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-07-31.13:31:29.771>labels= ['type-feature', 'library', '3.11']
title='Add __parameters__ and __getitem__ in TypeVar and ParamSpec'updated_at=<Date2022-03-12.13:54:12.028>user='https://github.com/serhiy-storchaka'
Adding __parameters__ and __getitem__ in TypeVar and ParamSpec allows to generalize and simplify the code (especially the C code) and allows to add more runtime checks. It may open ways for further simplification.
Unfortunately it is not compatible with bpo-44098, so the latter changes should be reverted.
The linked PR makes T = TypeVar("T"); T[int] work. That's not currently meaningful syntax to the type checker, but we may want it in the future for higher-kinded types. I would rather not make this change without a clear static typing use case in mind.
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']
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.
I'd also be wary about making changes that introduce additional runtime checks. As we've seen with the PEP-612 and 646 implementations, those can impede future iteration on typing.
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.
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.
There is a use case for subscription of TypeVar. For example:
Super subtle stuff. Tonight I do not have time to really dive into this, but I think Serhiy is on to something. KJ or Jelle, do you have the guts to dive in here?
Activity
serhiy-storchaka commentedon Jul 31, 2021
Adding __parameters__ and __getitem__ in TypeVar and ParamSpec allows to generalize and simplify the code (especially the C code) and allows to add more runtime checks. It may open ways for further simplification.
Unfortunately it is not compatible with bpo-44098, so the latter changes should be reverted.
JelleZijlstra commentedon Jan 25, 2022
The linked PR makes
T = TypeVar("T"); T[int]work. That's not currently meaningful syntax to the type checker, but we may want it in the future for higher-kinded types. I would rather not make this change without a clear static typing use case in mind.serhiy-storchaka commentedon Feb 3, 2022
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:
With the proposed change:
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.
gvanrossum commentedon Feb 3, 2022
I don't think that changing list[None] to list[NoneType] in the output is an improvement. I do appreciate the reduction in C code though!
JelleZijlstra commentedon Feb 3, 2022
I'd also be wary about making changes that introduce additional runtime checks. As we've seen with the PEP-612 and 646 implementations, those can impede future iteration on typing.
serhiy-storchaka commentedon Feb 5, 2022
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:
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.
There is a use case for subscription of TypeVar. For example:
gvanrossum commentedon Feb 7, 2022
Super subtle stuff. Tonight I do not have time to really dive into this, but I think Serhiy is on to something. KJ or Jelle, do you have the guts to dive in here?
serhiy-storchaka commentedon Mar 11, 2022
New changeset b6a5d85 by Serhiy Storchaka in branch 'main':
bpo-44796: Unify TypeVar and ParamSpec substitution (GH-31143)
b6a5d85
serhiy-storchaka commentedon Mar 12, 2022
PR 31143 simplified some code without changing the common API:
Objects/genericaliasobject.c | 46 ++++++++++++++--------------------------------
1 file changed, 14 insertions(+), 32 deletions(-)
Lib/_collections_abc.py | 63 +++++++++------------------------------------------------------
1 file changed, 9 insertions(+), 54 deletions(-)
PR 27511 will simplify it even more:
Objects/genericaliasobject.c | 60 ++++++++++++++++++------------------------------------------
1 file changed, 18 insertions(+), 42 deletions(-)
(Lib/typing.py will be simplified too, even if this does not reduce the number of lines).