-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
gh-99344, gh-99379, gh-99382: Fix issues in substitution of ParamSpec and TypeVarTuple #99412
gh-99344, gh-99379, gh-99382: Fix issues in substitution of ParamSpec and TypeVarTuple #99412
Conversation
serhiy-storchaka
commented
Nov 12, 2022
•
edited
Loading
edited
- Fix substitution of TypeVarTuple and ParamSpec together in user generics.
- Fix substitution of ParamSpec followed by TypeVarTuple in generic aliases.
- Check the number of arguments in substitution in user generics containing a TypeVarTuple and one or more TypeVar.
- Issue: TypeVarTuple and ParamSpec don't work together #99344
- Issue: Type substitution in generic aliases does not work if ParamSpec is followed by TypeVarTuple #99379
- Issue: User generic with TypeVarTuple does not check for minimal type of arguments #99382
…ution of ParamSpec and TypeVarTuple * Fix substitution of TypeVarTuple and ParamSpec together in user generics. * Fix substitution of ParamSpec followed by TypeVarTuple in generic aliases. * Check the number of arguments in substitution in user generics containing a TypeVarTuple and one or more TypeVar.
I'm not sure I'm the one to review this. Maybe @mrahtz can follow this? |
self.assertEqual(G3.__args__, ((bytes,),)) | ||
G4 = X[[]] | ||
self.assertEqual(G4.__args__, ((),)) | ||
with self.assertRaises(TypeError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add tests for X[int]
and Y[int]
? (I guess those should raise TypeError.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, unfortunately it does not raise TypeError. The type check for ParamSpec and TypeVar substitution are too lenient and accept any nonsense.
>>> from typing import *
>>> T = TypeVar('T')
>>> P = ParamSpec('P')
>>> class A(Generic[P, T]): pass
...
>>> A[int, [str]]
__main__.A[int, [<class 'str'>]]
>>> A[1, 2]
__main__.A[1, 2]
More strict type checks can be introduced in a separate issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't follow everything going on in typing.py
, but I'm sure Serhiy knows what he's doing, and the tests look good.
Thanks, Serhiy!
for C in Callable, collections.abc.Callable: | ||
with self.subTest(generic=C): | ||
A = C[P, Tuple[*Ts]] | ||
B = A[[int, str], bytes, float] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, this is an interesting case. Would it be worth checking C[Tuple[*Ts], P]
too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C
is Callable
. Callable[Tuple[*Ts], P]
does not make sense. It is an error for collections.abc.Callable
, and accepted for typing.Callable
(producing a meaningless result), but it is a different issue.
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11. |
GH-99866 is a backport of this pull request to the 3.11 branch. |
…ution of ParamSpec and TypeVarTuple (pythonGH-99412) * Fix substitution of TypeVarTuple and ParamSpec together in user generics. * Fix substitution of ParamSpec followed by TypeVarTuple in generic aliases. * Check the number of arguments in substitution in user generics containing a TypeVarTuple and one or more TypeVar. (cherry picked from commit 8f2fb7d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11. |
…ution of ParamSpec and TypeVarTuple (pythonGH-99412) * Fix substitution of TypeVarTuple and ParamSpec together in user generics. * Fix substitution of ParamSpec followed by TypeVarTuple in generic aliases. * Check the number of arguments in substitution in user generics containing a TypeVarTuple and one or more TypeVar. (cherry picked from commit 8f2fb7d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
… and TypeVarTuple (GH-99412) * Fix substitution of TypeVarTuple and ParamSpec together in user generics. * Fix substitution of ParamSpec followed by TypeVarTuple in generic aliases. * Check the number of arguments in substitution in user generics containing a TypeVarTuple and one or more TypeVar. (cherry picked from commit 8f2fb7d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>