-
Notifications
You must be signed in to change notification settings - Fork 98
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
Lock ability to create new fields #147
Comments
I was actually thinking of this in a different way earlier this week. From the standpoint of having a Schema that a box had to adhere too. I personally decided against it as the best schema validation tool I know of for this would be jsonschema and it really has such simple operation it makes more sense to use it by itself than incorporate it into box. However that is an interesting way to go about it is just not allowing more fields after initial creation. So the question then would be what about sub boxes, would they by default also be non-mutable, but their values be mutable? and then you could just upright replace a sub-box with a new one with different keys. |
Well, perhaps I'm not thinking about this properly :) I'm inheriting from Box to create a subclass - MLBox - which is of a specific type. So, for the sake of argument, let's say I want to do the following (forgive the hacky code):
Does this make sense? As an aside, is there a blessed way to set all the fields AFTER the box has been instantiated? Or I'm just using |
That sounds like a reasonable way to approach it. It is not something outside the realm of possible, but would probably be a heavy addition / it's own inherited box type (like how ConfigBox or SBox are currently). Will admit not a high priority item myself, but would welcome PRs for it. To achieve a similar thing now, I would still suggest using As far as updating or setting fields the goal of Box is to be a pure |
Sounds good! Do you want me to leave this open as an enhancement? And so, to be clear, is there anything I can do to allow for the freezing just of new field creation? (e.g. is there a method I should subclass in order to do this? or is it effectively impossible)? |
Yes please leave it open as an idea. Easiest way I can think of would be to subclass box and the if self._box_config["__created"] and key in self:
raise BoxError("Cannot create new fields") |
Just saying. Use Pydantic. It accepts dicts as input, parses as your intent and has configuration. I liked the idea of dict accessible with dot notation. It is now try to be pydantic. |
Is it possible to freeze new field creation via dot access without freezing the underlying fields to be immutable?
The text was updated successfully, but these errors were encountered: