-
Notifications
You must be signed in to change notification settings - Fork 78
Default values for frozen attrs classes #263
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
Comments
User have full control over user classes so the obvious and preferred way to provide defaults is in the class constructor. For dynamically created textX classes we don't have other options but to use object processors. |
Sounds reasonable. The issue is that currently the defaults provided by the class constructor are not used because TextX sets missing properties explicitly to |
Yeah, it would be a change to the way it worked before. I guess that documenting the preferred way of providing the defaults might be a better approach. Something like: class MyUserClass:
def __init__(self, first_param, ...):
self.fist_param = <default value> if first_param is None else first_param
.... This is a good practice anyway as the usage of default values directly in the parameter list can lead to bugs if mutable values are used. |
I think best practice is to use a custom sentinel value as default rather than I discovered this on my own when I was bitten by some subtle bugs during a project at work, but I have since then discovered that attrs uses the same pattern with It would be great if there was a way to ask textx to not set missing values to |
Sounds like a good idea. I think it would be relatively straightforward to implement it as an optional parameter to |
OTOH, textX will only set value to |
Uh oh!
There was an error while loading. Please reload this page.
Base type attribute are initialized as
None
with the comment that default values can be specified via object processors.textX/textx/metamodel.py
Lines 460 to 464 in c8a49a2
However for frozen attrs classes, object processors are run too late to actually change the value of the attribute, so there is no obvious way to provide a default value for such classes.
One way to solve this would be to filter out any
None
values when constructing the dictionary passed to__init__
and use the standard mechanisms for default values.textX/textx/model.py
Lines 836 to 840 in c8a49a2
would become
The text was updated successfully, but these errors were encountered: