Skip to content
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

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Nov 12, 2022

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

…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.
@gvanrossum
Copy link
Member

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):
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Contributor

@mrahtz mrahtz left a 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]
Copy link
Contributor

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?

Copy link
Member Author

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.

@serhiy-storchaka serhiy-storchaka merged commit 8f2fb7d into python:main Nov 29, 2022
@miss-islington
Copy link
Contributor

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@serhiy-storchaka serhiy-storchaka deleted the typing-paramspec-subst branch November 29, 2022 07:46
@bedevere-bot
Copy link

GH-99866 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Nov 29, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 29, 2022
…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>
@AlexWaygood AlexWaygood added the needs backport to 3.11 only security fixes label Nov 29, 2022
@miss-islington
Copy link
Contributor

Thanks @serhiy-storchaka for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 29, 2022
…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>
@AlexWaygood AlexWaygood removed the needs backport to 3.11 only security fixes label Nov 29, 2022
miss-islington added a commit that referenced this pull request Nov 29, 2022
… 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants