Skip to content

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

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

vstinner merged 4 commits into
python:mainfrom
vstinner:frozendict_deepcopy

Conversation

@vstinner

@vstinner vstinner commented Feb 19, 2026 •

Copy link
Copy Markdown
Member

@eendebakpt 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.

Good catch!

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

Copy link
Copy Markdown
Member

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

@vstinner

Copy link
Copy Markdown
Member Author

I added the tests requested by @serhiy-storchaka.

@vstinner

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.

@vstinner

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': [...]})]})
@serhiy-storchaka

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 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.

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.

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.

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

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

Copy link
Copy Markdown
Member Author

Merged, thanks for the exhaustive review.

brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants