-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
[python] Add option to return None instead of raising exception when accessing unset attribute #7784
[python] Add option to return None instead of raising exception when accessing unset attribute #7784
Conversation
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
@keymone can this be implemented with a
Though we will have a name collision with get when one has a property named get, one can do: The reasons that I prefer a get method are:
|
@spacether I understand the motivation behind But why not have both? Compared to current implementation both Yes, model instances can be treated as dictionaries and dictionaries throw a Also, dictionaries don't pretend to be objects and don't expose direct |
This gets into very murky territory
Because it is confusing to have multiple ways to do the same thing. Which is the preferred method? Do they both work the same way? How do you feel about chatting about this over Slack to get on the same page? We both definitely want to make using these models easier for users and to allow them to get back a default value rather than a key error. I am in our channel at https://join.slack.com/t/openapi-generator/shared_invite/enQtNzAyNDMyOTU0OTE1LTY5ZDBiNDI5NzI5ZjQ1Y2E5OWVjMjZkYzY1ZGM2MWQ4YWFjMzcyNDY5MGI4NjQxNDBiMTlmZTc5NjY2ZTQ5MGM |
@@ -1,4 +1,4 @@ | |||
def __setattr__(self, name, value): | |||
def __setitem__(self, name, value): | |||
"""this allows us to set a value with instance.field_name = val""" |
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.
How about updating the docstring here?
we use this to set values for instance.field_name = val and instance["field_name"] = val
raise ApiAttributeError( | ||
"{0} has no attribute '{1}'".format( | ||
type(self).__name__, name), | ||
path_to_item | ||
[e for e in [self._path_to_item, name] if e] |
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.
How about just using name here? We know that name was not found.
- this same comment applies on line 70 too
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.
i just compacted the previous functionality to find path_to_item
, if you think just name
is enough instead of the old path code, i'm fine with that
) | ||
|
||
def __getattr__(self, name): | ||
__unset_attribute_value__ = object() |
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.
How about unset_attribute_value = object
Then we always point to a singleton and don't need to instantiate additional empty objects
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.
well, somebody could concievably set an attribute to object
value, __unset_attribute_value__
will generate unique object only once per class, so i'm not worried about performance here
…, use __dict__ to avoid recursion issues
a626d27
to
d69a598
Compare
This looks great, thanks you @keymone! Can you add a python test in one of our manual tests that accesses a type:object's model properties with:
That will verify that this is working and throw a test error if this feature is accidentally changed/broken in the future. |
@spacether i think all those cases are covered except |
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.
Thanks for adding those tests. Your CI error is unrelated so I will merge this.
Thank you for the your PR!
See #7781
Add option to return None instead of raising exception when accessing unset attribute.
PR checklist
./bin/generate-samples.sh
to update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*
. For Windows users, please run the script in Git BASH.master
cc @spacether