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

Add ImmutableConfig #4

Merged
merged 7 commits into from
Apr 25, 2023
Merged

Add ImmutableConfig #4

merged 7 commits into from
Apr 25, 2023

Conversation

joerunde
Copy link
Contributor

Closes #3

This PR adds an ImmutableConfig which looks and feels like a Config, 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-types

Instead we subclassed AttributeAccessDict and removed the setters. Along with updating some static type references in AttributeAccessDict to cls references to respect inheritance, this allows the recursive nesting to still happen but results in immutable dicts.

evaline-ju and others added 4 commits April 24, 2023 14:34
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>
Copy link
Collaborator

@gabe-l-hart gabe-l-hart left a 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>
Copy link
Collaborator

@gabe-l-hart gabe-l-hart left a 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

super().__init__(input_map)

def __setitem__(self, key, value):
raise AttributeError("ImmutableAttributeAccessDict does not support attribute assignment")
Copy link
Collaborator

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

Copy link
Member

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>
@gabe-l-hart gabe-l-hart merged commit ea6566a into IBM:main Apr 25, 2023
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.

Provide immutable config objects
3 participants