FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-108751: Add copy.replace() function by serhiy-storchaka · Pull Request #108752 · python/cpython · GitHub

/ cpython Public

gh-108751: Add copy.replace() function - #108752

Merged
serhiy-storchaka merged 2 commits into
python:mainfrom
serhiy-storchaka:copy-replace
Sep 6, 2023
Merged

gh-108751: Add copy.replace() function#108752
serhiy-storchaka merged 2 commits into
python:mainfrom
serhiy-storchaka:copy-replace

Conversation

serhiy-storchaka commented Sep 1, 2023
edited by github-actions Bot
Loading

Copy link
Copy Markdown
Member

It creates a modified copy of an object by calling the object's __replace__() method.

It is a generalization of dataclasses.replace(), named tuple's _replace() method and replace() methods in various classes, and supports all these stdlib classes.


📚 Documentation preview 📚: https://cpython-previews--108752.org.readthedocs.build/

It creates a modified copy of an object by calling the object's
__replace__() method.

It is a generalization of dataclasses.replace(), named tuple's _replace()
method and replace() methods in various classes, and supports all these
stdlib classes.
Comment thread Doc/library/copy.rst Outdated
single: __replace__() (replace protocol)

Function :func:`replace` is more limited than :func:`copy` and :func:`deepcopy`,
and only supports named tuples, dataclasses, and other classes which

merwok Sep 1, 2023
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
and only supports named tuples, dataclasses, and other classes which
and only supports :term:`named tuples <named tuple>`, :mod:`dataclasses`, and other classes which

(I haven’t double checked if named tuple or namedtuple is the right key)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Actually, it does not support named tuples in general. It currently only supports named tuples created by collections.namedtuple().

Comment thread Lib/copy.py
func = getattr(cls, '__replace__', None)
if func is None:
raise TypeError(f"replace() does not support {cls.__name__} objects")
return func(obj, **changes)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is a cool feature for very little code!

serhiy-storchaka merged commit 6f3c138 into python:main Sep 6, 2023
serhiy-storchaka deleted the copy-replace branch September 6, 2023 20:55

Copy link
Copy Markdown

There's a new commit after the PR has been approved.

@rhettinger: please review the changes made to this pull request.

vstinner commented Sep 7, 2023

Copy link
Copy Markdown
Member

This change introduced a regression: see issue #109052.

With this change, test_sys_settrace started to crash. I don't know if the bug already existed before and was hidden, or if the Objects/codeobject.c change really introduced a regression.

Comment thread Doc/whatsnew/3.13.rst
----

* Add :func:`copy.replace` function which allows to create a modified copy of
an object, which is especially usefule for immutable objects.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

usefule -> useful

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks. Do you mind to create a PR to fix this typo?

Copy link
Copy Markdown
Member

Would it make sense to add support for dict?

>>> d={'x': 1}
>>> copy.replace(d, x=2)
TypeError: replace() does not support dict objects

It would be an alternative to:

>>> d={'x': 1}
>>> dict(d, x=2)
{'x': 2}
>>> {**d, 'x': 2}
{'x': 2}

Copy link
Copy Markdown
Member

Nice feature.

Copy link
Copy Markdown
Member Author

Would it make sense to add support for dict?

Perhaps no, because it erodes the difference between an object and a dict (as in JavaScript).

And you already have other ways to do this with dicts.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants


Back | FazBrowse Home | New Git URL