]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.7] bpo-37163: dataclasses.replace() now supports the field named "obj". (GH-13877...
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 26 Jun 2019 20:03:08 +0000 (23:03 +0300)
committerGitHub <noreply@github.com>
Wed, 26 Jun 2019 20:03:08 +0000 (23:03 +0300)
(cherry picked from commit f5b89af)

Lib/dataclasses.py
Misc/NEWS.d/next/Library/2019-06-07-08-18-05.bpo-37163.36JkUh.rst [new file with mode: 0644]

index 325b822d9f06f110127e252015d6cc80c95899a0..8c3d638e86bc58e316480ce39b2fb764385635be 100644 (file)
@@ -1202,7 +1202,7 @@ def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
                      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 @@ def replace(obj, **changes):
       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 (file)
index 0000000..5be9897
--- /dev/null
@@ -0,0 +1 @@
+:func:`dataclasses.replace` now supports the field named "obj".