[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-03-12 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

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).

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-03-11 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:


New changeset b6a5d8590c4bfe4553d796b36af03bda8c0d5af5 by Serhiy Storchaka in 
branch 'main':
bpo-44796: Unify TypeVar and ParamSpec substitution (GH-31143)
https://github.com/python/cpython/commit/b6a5d8590c4bfe4553d796b36af03bda8c0d5af5


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-02-06 Thread Guido van Rossum


Guido van Rossum  added the comment:

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?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-02-05 Thread Serhiy Storchaka


Serhiy Storchaka  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-02-05 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
pull_requests: +29321
pull_request: https://github.com/python/cpython/pull/31143

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-02-03 Thread Jelle Zijlstra


Jelle Zijlstra  added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-02-03 Thread Alex Waygood


Change by Alex Waygood :


--
nosy: +AlexWaygood, sobolevn

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-02-03 Thread Guido van Rossum


Guido van Rossum  added the comment:

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!

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-02-03 Thread Serhiy Storchaka


Serhiy Storchaka  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2022-01-24 Thread Jelle Zijlstra


Jelle Zijlstra  added the comment:

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.

--
nosy: +Jelle Zijlstra

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2021-07-31 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
Removed message: https://bugs.python.org/msg398655

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2021-07-31 Thread Serhiy Storchaka


Serhiy Storchaka  added the comment:

There is also similar bug in Generic:

>>> from typing import *
>>> T = TypeVar("T")
>>> P = ParamSpec("P")
>>> class X(Generic[T, P]):
... f: Callable[P, int]
... x: T
... 
>>> P_2 = ParamSpec("P_2")
>>> X[int, P_2, str]
__main__.X[int, ~P_2]

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2021-07-31 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
keywords: +patch
pull_requests: +26025
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/27511

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2021-07-31 Thread Serhiy Storchaka


Change by Serhiy Storchaka :


--
dependencies: +Merge tests for typing.Callable and collection.abc.Callable

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44796] Add __parameters__ and __getitem__ in TypeVar and ParamSpec

2021-07-31 Thread Serhiy Storchaka


New submission from 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 issue44098, so the latter changes 
should be reverted.

--
components: Library (Lib)
messages: 398640
nosy: gvanrossum, kj, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Add __parameters__ and __getitem__ in TypeVar and ParamSpec
type: enhancement
versions: Python 3.11

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com