Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Enable freezing collections.defaultdict objects #281

Merged
merged 5 commits into from
Oct 16, 2023
Merged

Fix: Enable freezing collections.defaultdict objects #281

merged 5 commits into from
Oct 16, 2023

Conversation

arshad-ml
Copy link
Contributor

@arshad-ml arshad-ml commented Oct 14, 2023

Fixes #280

Solution:
Changing the condition from typ is dict to isinstance(o, dict) enables the freeze function to convert objects of collections.defaultdict type to Pmap.

Copy link
Owner

@tobgu tobgu left a comment

Choose a reason for hiding this comment

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

Thanks! Please see my consideration.

@@ -31,7 +31,7 @@ def freeze(o, strict=True):
(1, pvector([]))
"""
typ = type(o)
if typ is dict or (strict and isinstance(o, PMap)):
if isinstance(o, dict) or (strict and isinstance(o, PMap)):
Copy link
Owner

Choose a reason for hiding this comment

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

While this certainly works for what you want to achieve I'm sligthly worried of the unintended effects it may have in clients code bases. Things that may not previously have been froozen (such as OrderedDicts , Counters, etc.) now start becoming frozen which leads to data loss and change of existing functionality.

If we want freeze to work also for defaultdicts I would prefer adding it as a distinct type to check against (using is) given the above considerations. Although not as nice/general I think it would be a safer move with a smaller blast radius. It's still a backwards imcompatible change, but a smaller one.

Also, please add a test for your new case while you are at it. :-)

Copy link
Contributor Author

@arshad-ml arshad-ml Oct 16, 2023

Choose a reason for hiding this comment

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

Makes sense! Thank you for the review and suggestions.
I have made the changes accordingly.

Also, I've added tests for defaultdict. :-)

@arshad-ml arshad-ml requested a review from tobgu October 16, 2023 06:26
@tobgu
Copy link
Owner

tobgu commented Oct 16, 2023

Thanks!

@tobgu tobgu merged commit ae193f2 into tobgu:master Oct 16, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

freeze() does not work on collections.defaultdict
2 participants