Skip to content

Comments

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

Open
vstinner wants to merge 10 commits intopython:mainfrom
vstinner:frozendict_pickle2
Open

gh-141510, PEP 814: Add frozendict support to pickle#144967
vstinner wants to merge 10 commits intopython:mainfrom
vstinner:frozendict_pickle2

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Feb 18, 2026

Add frozendict.__getnewargs__() method.

Add frozendict.__getnewargs__() method.
# 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
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
Member Author

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
Member Author

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
Member Author

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

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

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.

@serhiy-storchaka
Copy link
Member

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

@vstinner
Copy link
Member Author

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.

@vstinner
Copy link
Member Author

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
Member Author

I completed the PR to add requested tests.

@vstinner
Copy link
Member Author

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

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

pickle.dumps(fd, proto)

def test_pickle_iter(self):
it = iter(frozendict(x=1, y=2))
Copy link
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.

@vstinner
Copy link
Member Author

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
Member

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

@vstinner
Copy link
Member Author

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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

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.

2 participants