summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-09-06 20:55:42 (GMT)
committerGitHub <noreply@github.com>2023-09-06 20:55:42 (GMT)
commit6f3c138dfa868b32d3288898923bbfa388f2fa5d (patch)
tree98098db4d0d2f256f2a17d515c1750a28424166c /Doc
parent9f0c0a46f00d687e921990ee83894b2f4ce8a6e7 (diff)
downloadcpython-6f3c138dfa868b32d3288898923bbfa388f2fa5d.zip
cpython-6f3c138dfa868b32d3288898923bbfa388f2fa5d.tar.gz
cpython-6f3c138dfa868b32d3288898923bbfa388f2fa5d.tar.bz2
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.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/collections.rst2
-rw-r--r--Doc/library/copy.rst30
-rw-r--r--Doc/library/dataclasses.rst2
-rw-r--r--Doc/library/datetime.rst9
-rw-r--r--Doc/library/inspect.rst11
-rw-r--r--Doc/library/types.rst2
-rw-r--r--Doc/whatsnew/3.13.rst12
7 files changed, 61 insertions, 7 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index b8b231b..03cb1dc 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -979,6 +979,8 @@ field names, the method and attribute names start with an underscore.
>>> for partnum, record in inventory.items():
... inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now())
+ Named tuples are also supported by generic function :func:`copy.replace`.
+
.. attribute:: somenamedtuple._fields
Tuple of strings listing the field names. Useful for introspection
diff --git a/Doc/library/copy.rst b/Doc/library/copy.rst
index 8f32477..cc4ca03 100644
--- a/Doc/library/copy.rst
+++ b/Doc/library/copy.rst
@@ -17,14 +17,22 @@ operations (explained below).
Interface summary:
-.. function:: copy(x)
+.. function:: copy(obj)
- Return a shallow copy of *x*.
+ Return a shallow copy of *obj*.
-.. function:: deepcopy(x[, memo])
+.. function:: deepcopy(obj[, memo])
- Return a deep copy of *x*.
+ Return a deep copy of *obj*.
+
+
+.. function:: replace(obj, /, **changes)
+
+ Creates a new object of the same type as *obj*, replacing fields with values
+ from *changes*.
+
+ .. versionadded:: 3.13
.. exception:: Error
@@ -89,6 +97,20 @@ with the component as first argument and the memo dictionary as second argument.
The memo dictionary should be treated as an opaque object.
+.. index::
+ single: __replace__() (replace protocol)
+
+Function :func:`replace` is more limited than :func:`copy` and :func:`deepcopy`,
+and only supports named tuples created by :func:`~collections.namedtuple`,
+:mod:`dataclasses`, and other classes which define method :meth:`!__replace__`.
+
+ .. method:: __replace__(self, /, **changes)
+ :noindex:
+
+:meth:`!__replace__` should create a new object of the same type,
+replacing fields with values from *changes*.
+
+
.. seealso::
Module :mod:`pickle`
diff --git a/Doc/library/dataclasses.rst b/Doc/library/dataclasses.rst
index d687487..d78a607 100644
--- a/Doc/library/dataclasses.rst
+++ b/Doc/library/dataclasses.rst
@@ -456,6 +456,8 @@ Module contents
``replace()`` (or similarly named) method which handles instance
copying.
+ Dataclass instances are also supported by generic function :func:`copy.replace`.
+
.. function:: is_dataclass(obj)
Return ``True`` if its parameter is a dataclass or an instance of one,
diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
index 04cc755..0b9d42f 100644
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -652,6 +652,9 @@ Instance methods:
>>> d.replace(day=26)
datetime.date(2002, 12, 26)
+ :class:`date` objects are also supported by generic function
+ :func:`copy.replace`.
+
.. method:: date.timetuple()
@@ -1251,6 +1254,9 @@ Instance methods:
``tzinfo=None`` can be specified to create a naive datetime from an aware
datetime with no conversion of date and time data.
+ :class:`datetime` objects are also supported by generic function
+ :func:`copy.replace`.
+
.. versionadded:: 3.6
Added the ``fold`` argument.
@@ -1827,6 +1833,9 @@ Instance methods:
``tzinfo=None`` can be specified to create a naive :class:`.time` from an
aware :class:`.time`, without conversion of the time data.
+ :class:`time` objects are also supported by generic function
+ :func:`copy.replace`.
+
.. versionadded:: 3.6
Added the ``fold`` argument.
diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst
index 603ac32..fe0ed13 100644
--- a/Doc/library/inspect.rst
+++ b/Doc/library/inspect.rst
@@ -689,8 +689,8 @@ function.
The optional *return_annotation* argument, can be an arbitrary Python object,
is the "return" annotation of the callable.
- Signature objects are *immutable*. Use :meth:`Signature.replace` to make a
- modified copy.
+ Signature objects are *immutable*. Use :meth:`Signature.replace` or
+ :func:`copy.replace` to make a modified copy.
.. versionchanged:: 3.5
Signature objects are picklable and :term:`hashable`.
@@ -746,6 +746,9 @@ function.
>>> str(new_sig)
"(a, b) -> 'new return anno'"
+ Signature objects are also supported by generic function
+ :func:`copy.replace`.
+
.. classmethod:: Signature.from_callable(obj, *, follow_wrapped=True, globalns=None, localns=None)
Return a :class:`Signature` (or its subclass) object for a given callable
@@ -769,7 +772,7 @@ function.
.. class:: Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)
Parameter objects are *immutable*. Instead of modifying a Parameter object,
- you can use :meth:`Parameter.replace` to create a modified copy.
+ you can use :meth:`Parameter.replace` or :func:`copy.replace` to create a modified copy.
.. versionchanged:: 3.5
Parameter objects are picklable and :term:`hashable`.
@@ -892,6 +895,8 @@ function.
>>> str(param.replace(default=Parameter.empty, annotation='spam'))
"foo:'spam'"
+ Parameter objects are also supported by generic function :func:`copy.replace`.
+
.. versionchanged:: 3.4
In Python 3.3 Parameter objects were allowed to have ``name`` set
to ``None`` if their ``kind`` was set to ``POSITIONAL_ONLY``.
diff --git a/Doc/library/types.rst b/Doc/library/types.rst
index 8cbe17d..82300af 100644
--- a/Doc/library/types.rst
+++ b/Doc/library/types.rst
@@ -200,6 +200,8 @@ Standard names are defined for the following types:
Return a copy of the code object with new values for the specified fields.
+ Code objects are also supported by generic function :func:`copy.replace`.
+
.. versionadded:: 3.8
.. data:: CellType
diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index de23172..8c64675 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -115,6 +115,18 @@ array
It can be used instead of ``'u'`` type code, which is deprecated.
(Contributed by Inada Naoki in :gh:`80480`.)
+copy
+----
+
+* Add :func:`copy.replace` function which allows to create a modified copy of
+ an object, which is especially usefule for immutable objects.
+ It supports named tuples created with the factory function
+ :func:`collections.namedtuple`, :class:`~dataclasses.dataclass` instances,
+ various :mod:`datetime` objects, :class:`~inspect.Signature` objects,
+ :class:`~inspect.Parameter` objects, :ref:`code object <code-objects>`, and
+ any user classes which define the :meth:`!__replace__` method.
+ (Contributed by Serhiy Storchaka in :gh:`108751`.)
+
dbm
---