Skip to content

gh-141510, PEP 814: Add frozendict support to pickle - #144967

Merged
vstinner merged 10 commits into
python:mainfrom
vstinner:frozendict_pickle2
Feb 21, 2026
Merged

gh-141510, PEP 814: Add frozendict support to pickle#144967
vstinner merged 10 commits into
python:mainfrom
vstinner:frozendict_pickle2

Conversation

@vstinner

@vstinnervstinner commented Feb 18, 2026

Copy link
Copy Markdown
Member

Add frozendict.__getnewargs__() method.

Add frozendict.__getnewargs__() method.
Comment threadLib/test/picklecommon.py Outdated
Comment threadLib/test/pickletester.py Outdated
# make sure that floats are formatted locale independent with proto 0
self.assertEqual(self.dumps(1.2, 0)[0:3], b'F1.')

def test_frozendict(self):

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.

Would not be better to add this test in test_frozendict.py? Together with tests for copy() and deepcopy()?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Ok, I moved this test to test_pickle.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Together with tests for copy() and deepcopy()?

Commit dd64e42, which adds frozendict support to the copy module, added frozendict tests to test_copy.

@vstinner

Copy link
Copy Markdown
MemberAuthor

@serhiy-storchaka: I addressed your review. Please review the updated PR.

@serhiy-storchakaserhiy-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.

What about frozendict views and iterators? Are they copyable/pickleable?

Are there tests for deepcopying?

I am surprised that there is no separate test_frozendict.py.

Comment threadLib/test/test_dict.py
Comment threadLib/test/test_dict.py Outdated
@serhiy-storchaka

Copy link
Copy Markdown
Member

Oh, I don't think deepcopy of frozendict is correct. It does not work for recursive frozendict.

Comment threadLib/test/pickletester.py
@vstinner

Copy link
Copy Markdown
MemberAuthor

I will try to update my PR later to address other comments.

What about frozendict views and iterators? Are they copyable/pickleable?

keys, values and items views cannot be copied nor serialized by pickle.

Ah, it seems like it's possible to serialize a frozendict iterator.

Are there tests for deepcopying?

test_copy.test_deepcopy_frozendict() tests frozendict deepcopy.

I am surprised that there is no separate test_frozendict.py.

Ah. It was simple to add frozendict tests to existing test_dict. Maybe we can create test_frozendict later once we will add more tests.

Comment threadLib/test/test_dict.py
@vstinner

Copy link
Copy Markdown
MemberAuthor

Oh, I don't think deepcopy of frozendict is correct. It does not work for recursive frozendict.

You're right, the current copy.deepcopy() implementation doesn't work for recursive frozendict.

@vstinner

Copy link
Copy Markdown
MemberAuthor

I completed the PR to add requested tests.

@vstinner

Copy link
Copy Markdown
MemberAuthor

You're right, the current copy.deepcopy() implementation doesn't work for recursive frozendict.

I created #145027 to fix copy.deepcopy() for recursive frozendict.

Comment threadLib/test/test_dict.py Outdated
pickle.dumps(fd, proto)

def test_pickle_iter(self):
it = iter(frozendict(x=1, y=2))

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.

What about value iterator and item iterator?

Consume one item from the iterator, to ensure that it correctly restores its state. See pickling tests in test_ordered_dict for example.

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.

Comment threadLib/test/pickletester.py Outdated
Comment threadLib/test/pickletester.py Outdated
Comment threadLib/test/pickletester.py Outdated
@vstinner

Copy link
Copy Markdown
MemberAuthor

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.

I propose merging this PR first (once it will be approved), and then adding such tests to #145027 PR.

@serhiy-storchaka

Copy link
Copy Markdown
Member

Did you forget to push changes for comments which you marked resolved?

@vstinner

Copy link
Copy Markdown
MemberAuthor

Ooops, you're correct. I forgot to push my changes, but I also removed my local branch... I had to rewrite my changes. I just pushed them. I should be ok now.

@serhiy-storchakaserhiy-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. 👍

@vstinner
vstinner merged commit 20b1535 into python:mainFeb 21, 2026
49 checks passed
@vstinner
vstinner deleted the frozendict_pickle2 branch February 21, 2026 10:07
@vstinner

Copy link
Copy Markdown
MemberAuthor

Merged. Thanks for your great review @serhiy-storchaka, it was very useful!

@StanFromIreland

Copy link
Copy Markdown
Member

This broke Oddballs (i.e. text_xpickle): https://buildbot.python.org/#/builders/1868/builds/5

@vstinner

Copy link
Copy Markdown
MemberAuthor

This broke Oddballs (i.e. text_xpickle): https://buildbot.python.org/#/builders/1868/builds/5

Oh, I'm not used to test_xpickle yet, I forgot about it. I wrote #145069 to fix test_xpickle.

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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@vstinner@serhiy-storchaka@StanFromIreland