Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ef103f

Browse files
[3.7] bpo-37163: dataclasses.replace() now supports the field named "obj". (GH-13877) (GH-14405)
(cherry picked from commit f5b89af)
1 parent 814c7ae commit 6ef103f

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Lib/dataclasses.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ class C(Base):
12021202
unsafe_hash=unsafe_hash, frozen=frozen)
12031203

12041204

1205-
def replace(obj, **changes):
1205+
def replace(*args, **changes):
12061206
"""Return a new object replacing specified fields with new values.
12071207
12081208
This is especially useful for frozen classes. Example usage:
@@ -1216,6 +1216,14 @@ class C:
12161216
c1 = replace(c, x=3)
12171217
assert c1.x == 3 and c1.y == 2
12181218
"""
1219+
if len(args) > 1:
1220+
raise TypeError(f'replace() takes 1 positional argument but {len(args)} were given')
1221+
if args:
1222+
obj, = args
1223+
elif 'obj' in changes:
1224+
obj = changes.pop('obj')
1225+
else:
1226+
raise TypeError("replace() missing 1 required positional argument: 'obj'")
12191227

12201228
# We're going to mutate 'changes', but that's okay because it's a
12211229
# new dict, even if called with 'replace(obj, **my_changes)'.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`dataclasses.replace` now supports the field named "obj".

0 commit comments

Comments
 (0)