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
Bug or new behavior of generics parameterization? #98852
But after fixing this code I got other test failure. And it seems that we have contradictory tests. Test in test_complex_subclasses expects that we can successfully create a class inheriting from generic base and Generic[].
But tests in test_generic_errors and test_generic_inheritance expect that Generic[] should include all typing variables, otherwise the class creation should fail.
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 Generic[] containing all typing variables or allow it only containing some of them.
I don't see a contradiction. In class Sub(Base, Generic[T]):, Base should be equivalent to Base[Any], which is the general behavior for generics missing a subscription.
Activity
sobolevn commentedon Oct 29, 2022
This is how it used to work in
3.10: https://github.com/python/cpython/blame/85f88f63d96b07208c98a725391af7cb710fe06b/Lib/typing.py#L1060-L1081Clearly looks like a bug to me. I am investigating what is going on :)
Eclips4 commentedon Oct 29, 2022
I know about how it works in 3.10 =)
My simple solution:
... 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.
What you think about this?
sobolevn commentedon Oct 29, 2022
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? :)
gvanrossum commentedon Oct 29, 2022
Could we first find which PR introduced the bug?
Also, @JelleZijlstra ^^
Eclips4 commentedon Oct 30, 2022
It is bpo-44796 (#31143)
serhiy-storchaka commentedon Oct 30, 2022
Indeed, there is a bug in
_collect_parameterswhich causes to skipping generic types.Yet one interesting example:
But after fixing this code I got other test failure. And it seems that we have contradictory tests. Test in
test_complex_subclassesexpects that we can successfully create a class inheriting from generic base andGeneric[].cpython/Lib/test/test_typing.py
Lines 3898 to 3900 in 018b248
But tests in
test_generic_errorsandtest_generic_inheritanceexpect thatGeneric[]should include all typing variables, otherwise the class creation should fail.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
Generic[]containing all typing variables or allow it only containing some of them.See also #94607.
JelleZijlstra commentedon Oct 30, 2022
I don't see a contradiction. In
class Sub(Base, Generic[T]):,Baseshould be equivalent toBase[Any], which is the general behavior for generics missing a subscription.serhiy-storchaka commentedon Oct 31, 2022
It makes sense if you look at it this way. The problem is with distinguishing free and bound variables. Both are represented in
__parameters__.pythongh-98852: Fix subscription of type aliases
serhiy-storchaka commentedon Oct 31, 2022
The issue is more serious and is not completely new.
In the following code:
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.
gh-98852: Fix subscription of type aliases (GH-98920)
pythongh-98852: Fix subscription of type aliases (pythonGH-98920)
gh-98852: Fix subscription of type aliases (GH-98920)
[3.10] pythongh-98852: Fix subscription of types.GenericAlias instanc…
[3.10] gh-98852: Fix subscription of types.GenericAlias instances (GH…
hauntsaninja commentedon Nov 6, 2022
Is there anything left to do here?
serhiy-storchaka commentedon Nov 6, 2022
It is completed.