Skip to content

Commit 339fc3c

Browse files
authored
gh-114198: Rename dataclass __replace__ argument to 'self' (gh-114251)
This change renames the dataclass __replace__ method's first argument name from 'obj' to 'self'.
1 parent 9c93350 commit 339fc3c

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

Lib/dataclasses.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1558,14 +1558,14 @@ class C:
15581558
return _replace(obj, **changes)
15591559

15601560

1561-
def _replace(obj, /, **changes):
1561+
def _replace(self, /, **changes):
15621562
# We're going to mutate 'changes', but that's okay because it's a
1563-
# new dict, even if called with 'replace(obj, **my_changes)'.
1563+
# new dict, even if called with 'replace(self, **my_changes)'.
15641564

15651565
# It's an error to have init=False fields in 'changes'.
1566-
# If a field is not in 'changes', read its value from the provided obj.
1566+
# If a field is not in 'changes', read its value from the provided 'self'.
15671567

1568-
for f in getattr(obj, _FIELDS).values():
1568+
for f in getattr(self, _FIELDS).values():
15691569
# Only consider normal fields or InitVars.
15701570
if f._field_type is _FIELD_CLASSVAR:
15711571
continue
@@ -1582,11 +1582,11 @@ def _replace(obj, /, **changes):
15821582
if f._field_type is _FIELD_INITVAR and f.default is MISSING:
15831583
raise TypeError(f"InitVar {f.name!r} "
15841584
f'must be specified with replace()')
1585-
changes[f.name] = getattr(obj, f.name)
1585+
changes[f.name] = getattr(self, f.name)
15861586

15871587
# Create the new object, which calls __init__() and
15881588
# __post_init__() (if defined), using all of the init fields we've
15891589
# added and/or left in 'changes'. If there are values supplied in
15901590
# changes that aren't fields, this will correctly raise a
15911591
# TypeError.
1592-
return obj.__class__(**changes)
1592+
return self.__class__(**changes)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The signature for the ``__replace__`` method on :mod:`dataclasses` now has
2+
the first argument named ``self``, rather than ``obj``.

0 commit comments

Comments
 (0)