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

gh-141510: Fix copy.deepcopy() for recursive frozendict by vstinner · Pull Request #145027 · python/cpython · GitHub

/ cpython Public

gh-141510: Fix copy.deepcopy() for recursive frozendict - #145027

Merged
vstinner merged 4 commits into
python:mainfrom
vstinner:frozendict_deepcopy
Feb 21, 2026
Merged

gh-141510: Fix copy.deepcopy() for recursive frozendict#145027
vstinner merged 4 commits into
python:mainfrom
vstinner:frozendict_deepcopy

Conversation

vstinner commented Feb 19, 2026
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

eendebakpt left a comment

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

Good catch!

serhiy-storchaka self-requested a review February 20, 2026 09:46
Comment thread Lib/test/test_copy.py
Comment thread Lib/test/test_copy.py

Copy link
Copy Markdown
Member

After adding the pickle support we may be able to remove the special deepcopy code.

Copy link
Copy Markdown
Member Author

I added the tests requested by @serhiy-storchaka.

Copy link
Copy Markdown
Member Author

After adding the pickle support we may be able to remove the special deepcopy code.

I propose merging #144967 PR first (once it will be approved), and then rework this PR. See also @serhiy-storchaka's comment on the pickle PR:

If things are pickleable, they should also be deepcopyable. It is worth to have explicit deepcopy tests, because they can preserve additional invariants. For example, it should be possible to deepcopy a frozendict containing lambdas or modules.

We can add more tests in this deepcopy PR.

Copy link
Copy Markdown
Member Author

After adding the pickle support we may be able to remove the special deepcopy code.

I merged main into this branch to retrieve the pickle change. If I remove _deepcopy_frozendict(), test_copy.test_deepcopy_frozendict() fails:

FAIL: test_deepcopy_frozendict (test.test_copy.TestCopy.test_deepcopy_frozendict)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/vstinner/python/main/Lib/test/test_copy.py", line 442, in test_deepcopy_frozendict
    self.assertIs(y['foo'][0], y)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
AssertionError: frozendict({'foo': [frozendict({...})]}) is not frozendict({'foo': [frozendict({'foo': [...]})]})

Copy link
Copy Markdown
Member

If I remove _deepcopy_frozendict(), test_copy.test_deepcopy_frozendict() fails:

Hmm, I thought it is just an optimization. I wonder if deepcopy() can be generalized to handle this. But this is a different issue.

serhiy-storchaka left a comment

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

LGTM. 👍 (But maybe add few more assertions?)

Comment thread Lib/test/test_copy.py
x['foo'].append(x)
x = x['foo']
y = copy.deepcopy(x)
self.assertIs(y[0]['foo'], y)

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

Add also other assertions similar to the above ones:

        self.assertEqual(y, x)
        self.assertIsNot(x, y)
        self.assertIsNot(x[0], y[0])

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

self.assertEqual(y, x) fails with RecursionError. I added the two other tests.

vstinner enabled auto-merge (squash) February 21, 2026 15:05
vstinner merged commit 646bd86 into python:main Feb 21, 2026
45 checks passed
vstinner deleted the frozendict_deepcopy branch February 21, 2026 15:30

Copy link
Copy Markdown
Member Author

Merged, thanks for the exhaustive review.

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.

3 participants


Back | FazBrowse Home | New Git URL