-
Notifications
You must be signed in to change notification settings - Fork 132
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
Intro'd freeze/unfreeze. #130
Conversation
Thank you for your contribution and very clear PR @sumukhbarve! I agree with most of your changes, but in my opinion we should throw What do you think? |
Hi Matts, On second thought, I agree with you. Frozen-ness needn't be the same a I've made the required changes. Frozen |
Great thank you! Would you mind rebasing this on master so that we can run tests here on github actions? |
Done! |
Thanks! |
Could you draft up a simple release note for this @sumukhbarve? |
Based on my reading of #121, it's clear that the need for
.freeze()
arises from the desire forKeyError
s, so that typos can be caught quickly. Keeping this in mind, I've tried to make minimal additions.The Basics
When an
addict.Dict
is frozen, accessing a missing key will raise aKeyError
. This is true for nestedaddict.Dict
instances too.Allowing Key Addition
In some sense, a plain Python
dict
is already frozen, as it always raisesKeyError
s. Note that plaindict
s allow the addition of new keys. Thus, it should make sense to allow the addition to new keys to frozen addicts, but only at the top level. That is:frozenDicty.newKey = 1
should work as expected, butfrozenDicty.missingKey.newKey = 1
should raise aKeyError
.Unfreezing is a Shorthand
As
.freeze()
and.unfreeze()
are very similar, differing only in a boolean flag, it made sense to implement.unfreeze()
as a shorthand for.freeze(False)
. Specifically:.freeze()
is equivalent to.freeze(True)
, and.unfreeze()
is a shorthand for.freeze(False)
.No Frozen Initialization
Thought about adding a
__frozen
param to__init__()
, but decided against it. If required, such functionality can easily be added later. And while a such a parameter is mentioned in #121, in favoring explicit vs implicit, it may be best to require the user to explicitly call.freeze()
.No Return Value
Currently,
.freeze()
(including.unfreeze()
) returnsNone
, not anaddict.Dict
. This is similar to Python's built-indict.update()
. While #121 suggests making.freeze()
return a frozenaddict.Dict
, retaining parity withdict.update()
may be desirable.