Skip to content

bpo-46581: Propagate private vars via _GenericAlias.copy_with #31061

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5793,6 +5793,27 @@ def test_paramspec_in_nested_generics(self):
self.assertEqual(G2[[int, str], float], list[C])
self.assertEqual(G3[[int, str], float], list[C] | int)

def test_paramspec_gets_copied(self):
# bpo-46581
P = ParamSpec('P')
P2 = ParamSpec('P2')
C1 = Callable[P, int]
self.assertEqual(C1.__parameters__, (P,))
self.assertEqual(C1[P2].__parameters__, (P2,))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only this test was failed.

self.assertEqual(C1[str].__parameters__, ())
self.assertEqual(C1[str, T].__parameters__, (T,))
self.assertEqual(C1[Concatenate[str, P2]].__parameters__, (P2,))
self.assertEqual(C1[Concatenate[T, P2]].__parameters__, (T, P2))
self.assertEqual(C1[...].__parameters__, ())

C2 = Callable[Concatenate[str, P], int]
self.assertEqual(C2.__parameters__, (P,))
self.assertEqual(C2[P2].__parameters__, (P2,))
self.assertEqual(C2[str].__parameters__, ())
self.assertEqual(C2[str, T].__parameters__, (T,))
self.assertEqual(C2[Concatenate[str, P2]].__parameters__, (P2,))
self.assertEqual(C2[Concatenate[T, P2]].__parameters__, (T, P2))


class ConcatenateTests(BaseTestCase):
def test_basics(self):
Expand Down
13 changes: 6 additions & 7 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,9 @@ def Concatenate(self, parameters):
"ParamSpec variable.")
msg = "Concatenate[arg, ...]: each arg must be a type."
parameters = (*(_type_check(p, msg) for p in parameters[:-1]), parameters[-1])
return _ConcatenateGenericAlias(self, parameters)
return _ConcatenateGenericAlias(self, parameters,
_typevar_types=(TypeVar, ParamSpec),
_paramspec_tvars=True)


@_SpecialForm
Expand Down Expand Up @@ -1339,7 +1341,9 @@ def _determine_new_args(self, args):
return tuple(new_args)

def copy_with(self, args):
return self.__class__(self.__origin__, args, name=self._name, inst=self._inst)
return self.__class__(self.__origin__, args, name=self._name, inst=self._inst,
_typevar_types=self._typevar_types,
_paramspec_tvars=self._paramspec_tvars)

def __repr__(self):
if self._name:
Expand Down Expand Up @@ -1548,11 +1552,6 @@ def __hash__(self):


class _ConcatenateGenericAlias(_GenericAlias, _root=True):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs,
_typevar_types=(TypeVar, ParamSpec),
_paramspec_tvars=True)

def copy_with(self, params):
if isinstance(params[-1], (list, tuple)):
return (*params[:-1], *params[-1])
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Paul Boddie
Matthew Boedicker
Robin Boerdijk
Andra Bogildea
Matt Bogosian
Nikolay Bogoychev
David Bolen
Wouter Bolsterlee
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Brings :class:`ParamSpec` propagation for :class:`GenericAlias` in line with
:class:`Concatenate` (and others).