-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Bug or new behavior of generics parameterization? #98852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is how it used to work in >>> from typing import TypeVar, Generic, Callable
>>>
>>>
>>> T = TypeVar("T")
>>> P = TypeVar("P")
>>>
>>> class Test(Generic[T]): ...
...
>>>
>>> S = Callable[[Test], P]
>>>
>>> def test(arg: S[T]): ...
...
>>> test.__annotations__
{'arg': typing.Callable[[__main__.Test], ~T]} Clearly looks like a bug to me. I am investigating what is going on :) |
I know about how it works in 3.10 =) ...
if substfunc:
new_arg = substfunc(new_arg_by_param[old_arg])
elif not isinstance(old_arg, (_GenericAlias, GenericAlias, types.UnionType)):
new_arg = old_arg
else:
... With that, code above works fine. >>> test.__annotations__
{'arg': typing.Callable[[__main__.Test], ~T]} What you think about this? |
Yes, looks like it might work. Previously the same logic was guarded by this check: https://github.com/python/cpython/blame/85f88f63d96b07208c98a725391af7cb710fe06b/Lib/typing.py#L1071 Would you like to send a PR with this proposal? :) |
Could we first find which PR introduced the bug? Also, @JelleZijlstra ^^ |
Indeed, there is a bug in Yet one interesting example: >>> class Test(Generic[T]): ...
...
>>> Test.__parameters__
(~T,)
>>> class Test2(Test): ...
...
>>> Test2.__parameters__
() But after fixing this code I got other test failure. And it seems that we have contradictory tests. Test in cpython/Lib/test/test_typing.py Lines 3898 to 3900 in 018b248
But tests in cpython/Lib/test/test_typing.py Lines 3047 to 3048 in 018b248
cpython/Lib/test/test_typing.py Lines 6492 to 6494 in 018b248
The only difference between these examples is that in the former case the generic base is a generic class, and in the latter cases it is a generic alias. I do not think that there should be difference between them in this context. We should either require See also #94607. |
I don't see a contradiction. In |
It makes sense if you look at it this way. The problem is with distinguishing free and bound variables. Both are represented in |
Fix subscription of type aliases containing bare generic types or types like TypeVar: for example tuple[A, T][int] and tuple[TypeVar, T][int], where A is a generic type, and T is a type variable.
The issue is more serious and is not completely new. In the following code: from typing import *
T = TypeVar("T")
T2 = TypeVar("T2")
class A(Generic[T2]): ...
Tuple[A, T][int]
tuple[A, T][int]
Tuple[TypeVar, T][int]
tuple[TypeVar, T][int] 3.11 raises exception for the 1st and 3rd examples, produces incorrect result for the 2nd example, and crashes on the last example. 3.10 raises exception for the 2nd examples. |
Fix subscription of type aliases containing bare generic types or types like TypeVar: for example tuple[A, T][int] and tuple[TypeVar, T][int], where A is a generic type, and T is a type variable.
Fix subscription of type aliases containing bare generic types or types like TypeVar: for example tuple[A, T][int] and tuple[TypeVar, T][int], where A is a generic type, and T is a type variable. (cherry picked from commit 0e15c31) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Fix subscription of type aliases containing bare generic types or types like TypeVar: for example tuple[A, T][int] and tuple[TypeVar, T][int], where A is a generic type, and T is a type variable. (cherry picked from commit 0e15c31) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
…es (pythonGH-98920) Fix subscription of types.GenericAlias instances containing bare generic types: for example tuple[A, T][int], where A is a generic type, and T is a type variable.
Is there anything left to do here? |
It is completed. |
On python3.11 i can't run this code. Code falls on an attempt to parameterize
S
Traceback:
How i understand, in 3.11, an attempt is being made to parameterize
Test
inS
, but in 3.10 - notIs it new behavior or a bug?
The text was updated successfully, but these errors were encountered: