Skip to content

gh-114198: Rename dataclass __replace__ argument to 'self' #114251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,14 +1558,14 @@ class C:
return _replace(obj, **changes)


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

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

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

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