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

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

Merged
Merged
8 changes: 8 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5793,6 +5793,14 @@ 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')
original = Callable[P, int]
self.assertEqual(original.__parameters__, (P,))
copied = original[P]
self.assertEqual(original.__parameters__, copied.__parameters__)
Copy link
Member

Choose a reason for hiding this comment

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

Could you add some more complicated test cases? Ideas:

  • The original has a Concatenate[int, P] in it (original = Callable[Concatenate[int, P], int]; original[P].__paramaters__ == (P,))
  • Instead of substituting a P in, substitute in a Concatenate or something like [int, T], and then the T should be in __parameters__.

serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved


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,
posita marked this conversation as resolved.
Show resolved Hide resolved
_typevar_types=self._typevar_types,
_paramspec_tvars=self._paramspec_tvars)
posita marked this conversation as resolved.
Show resolved Hide resolved
posita marked this conversation as resolved.
Show resolved Hide resolved

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