-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add ImmutableConfig #4
Conversation
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Joe Runde <Joseph.Runde@ibm.com>
Signed-off-by: Joe Runde <Joseph.Runde@ibm.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of small NIT picks on error handling, but no showstoppers. This will be really nice to have!
Co-authored-by: Gabe Goodhart <gabe.l.hart@gmail.com> Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more minor NIT on the errors, but I'll give it the 👍 and you guys can make the call on whether to change it
aconfig/aconfig.py
Outdated
super().__init__(input_map) | ||
|
||
def __setitem__(self, key, value): | ||
raise AttributeError("ImmutableAttributeAccessDict does not support attribute assignment") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, I actually think this should be a TypeError
to keep with what happens if you try to use item assignment on a mappingproxy
:
Foo.__dict__["asdf"] = 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'mappingproxy' object does not support item assignment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah fair
Signed-off-by: Evaline Ju <69598118+evaline-ju@users.noreply.github.com>
Closes #3
This PR adds an
ImmutableConfig
which looks and feels like aConfig
, but has fully nested immutability.We wanted to try using a
mappingproxy
but unfortunately those are final and can't be extended. See related: https://stackoverflow.com/questions/66037137/immutable-frozen-dictionary-subclass-with-fixed-key-value-typesInstead we subclassed
AttributeAccessDict
and removed the setters. Along with updating some static type references inAttributeAccessDict
tocls
references to respect inheritance, this allows the recursive nesting to still happen but results in immutable dicts.