-
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
Is it possible to forbid accessing missing keys? #121
Comments
Hi @devforfu, Good suggestion. I'd be open to adding this, sure! Would you like to take a stab at the implementation? What do you think of the following API? from addict import Dict
c = Dict(config)
c.freeze() |
Hi @mewwts, Thank you for the response! Yes, looks good! Probably it is worth also to return a Ok, sure, not a problem. I'll try to cope up with something and open a PR. |
Yes, sure, but then in the example a = Dict(config)
b = a.freeze() I would expect |
Moved discussion to #123 |
Hi there,
I'm using
addict.Dict
instances to store some predefined configuration. The configuration is stored as JSON and loaded intoDict
as follows.It works great. Then I use this configuration to set up some properties of my objects.
The problem with this approach is that if I made a typo in the attribute name, I'll not get an attribute error but an empty dictionary and my class's internal logic fails later on when encounters dictionary type instead of expected value.
I understand that this behavior is expected and required to enable the creation of dynamic nested dictionaries. However, in my case, I use
Dict
as a "frozen" configuration object that is not expected to be modified during the program's lifetime and would like it to fail early in case of typos or missing entries. With the current behavior, the program doesn't fail on an object's creation but raises a (non-obvious) error later on.I wonder is it possible to add a
frozen
attribute to theDict
initializer? Like the following snippet shows.And if a "frozen" object is asked a missing key, the error is raised.
Would be glad to hear your thoughts!
The text was updated successfully, but these errors were encountered: