gh-108751: Add copy.replace() function (GH-108752)

It creates a modified copy of an object by calling the object's
__replace__() method.

It is a generalization of dataclasses.replace(), named tuple's _replace()
method and replace() methods in various classes, and supports all these
stdlib classes.
This commit is contained in:
Serhiy Storchaka 2023-09-06 23:55:42 +03:00 committed by GitHub
parent 9f0c0a46f0
commit 6f3c138dfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 311 additions and 68 deletions

View file

@ -2870,6 +2870,8 @@ class Parameter:
return formatted
__replace__ = replace
def __repr__(self):
return '<{} "{}">'.format(self.__class__.__name__, self)
@ -3130,6 +3132,8 @@ class Signature:
return type(self)(parameters,
return_annotation=return_annotation)
__replace__ = replace
def _hash_basis(self):
params = tuple(param for param in self.parameters.values()
if param.kind != _KEYWORD_ONLY)