Skip to content

Commit 90bdf0d

Browse files
committed
bpo-46581: Propagate private vars via _GenericAlias.copy_with
pythonGH-26091 added the _typevar_types and _paramspec_tvars instance variables to _GenericAlias. However, they were not propagated consistently. This commit addresses the most prominent deficiency identified in bpo-46581 (namely their absence from _GenericAlias.copy_with), but there could be others.
1 parent e801e88 commit 90bdf0d

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

Lib/test/test_genericalias.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,14 @@ def __deepcopy__(self, memo):
305305
self.assertEqual(copied.__args__, alias.__args__)
306306
self.assertEqual(copied.__parameters__, alias.__parameters__)
307307

308+
def test_copy_with(self):
309+
# bpo-46581
310+
from typing import Callable, ParamSpec
311+
P = ParamSpec('P')
312+
original = Callable[P, int]
313+
copied = original.copy_with((P, int))
314+
self.assertEqual(original.__parameters__, copied.__parameters__)
315+
308316
def test_union(self):
309317
a = typing.Union[list[int], list[str]]
310318
self.assertEqual(a.__args__, (list[int], list[str]))

Lib/typing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,9 @@ def _determine_new_args(self, args):
13391339
return tuple(new_args)
13401340

13411341
def copy_with(self, args):
1342-
return self.__class__(self.__origin__, args, name=self._name, inst=self._inst)
1342+
return self.__class__(self.__origin__, args, name=self._name, inst=self._inst,
1343+
_typevar_types=self._typevar_types,
1344+
_paramspec_tvars=self._paramspec_tvars)
13431345

13441346
def __repr__(self):
13451347
if self._name:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Paul Boddie
190190
Matthew Boedicker
191191
Robin Boerdijk
192192
Andra Bogildea
193+
Matt Bogosian
193194
Nikolay Bogoychev
194195
David Bolen
195196
Wouter Bolsterlee
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Propagates :attribute:`~typing._GenericAlias._typevar_types` and
2+
:attribute:`~typing._GenericAlias._paramspec_tvars` via
3+
:method:`~typing._GenericAlias.copy_with`.

0 commit comments

Comments
 (0)