[Python-checkins] [3.7] bpo-37163: dataclasses.replace() now supports the field named "obj". (GH-13877) (GH-14405)
Serhiy Storchaka
webhook-mailer at python.org
Wed Jun 26 16:03:15 EDT 2019
https://github.com/python/cpython/commit/6ef103fbdbc05adbc20838c94b1f0c40fa6c159a
commit: 6ef103fbdbc05adbc20838c94b1f0c40fa6c159a
branch: 3.7
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-06-26T23:03:08+03:00
summary:
[3.7] bpo-37163: dataclasses.replace() now supports the field named "obj". (GH-13877) (GH-14405)
(cherry picked from commit f5b89af)
files:
A Misc/NEWS.d/next/Library/2019-06-07-08-18-05.bpo-37163.36JkUh.rst
M Lib/dataclasses.py
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 325b822d9f06..8c3d638e86bc 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -1202,7 +1202,7 @@ class C(Base):
unsafe_hash=unsafe_hash, frozen=frozen)
-def replace(obj, **changes):
+def replace(*args, **changes):
"""Return a new object replacing specified fields with new values.
This is especially useful for frozen classes. Example usage:
@@ -1216,6 +1216,14 @@ class C:
c1 = replace(c, x=3)
assert c1.x == 3 and c1.y == 2
"""
+ if len(args) > 1:
+ raise TypeError(f'replace() takes 1 positional argument but {len(args)} were given')
+ if args:
+ obj, = args
+ elif 'obj' in changes:
+ obj = changes.pop('obj')
+ else:
+ raise TypeError("replace() missing 1 required positional argument: 'obj'")
# We're going to mutate 'changes', but that's okay because it's a
# new dict, even if called with 'replace(obj, **my_changes)'.
diff --git a/Misc/NEWS.d/next/Library/2019-06-07-08-18-05.bpo-37163.36JkUh.rst b/Misc/NEWS.d/next/Library/2019-06-07-08-18-05.bpo-37163.36JkUh.rst
new file mode 100644
index 000000000000..5be989736abe
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-06-07-08-18-05.bpo-37163.36JkUh.rst
@@ -0,0 +1 @@
+:func:`dataclasses.replace` now supports the field named "obj".
More information about the Python-checkins
mailing list