Fix accessing builtin dict's attrs for a missing field #1557
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I've faced this issue, when a schema has a field with name same as builtin dict's has attribute:
Fails with this:
It happens this way:
marshmallow.utils._get_value_for_key
update
field in data on the lineobj[key]
theKeyError
exception is raisedgetattr(obj, key, default)
is returnedIf the
key
is not found here, it returns default, so if my field is'update_'
or'qwerty'
, it works as expected -- the result is returned and it's{'data': {}}
But the
'update'
matches the builtin dict's attribute (method) so it's returned. And that's not expected at all.Of course, I could override the
get_attribute
method inMyNestedSchema
this way:But I think, that needs to be fixed.
I thought about checking if the
getattr(obj, key, default)
expression returns a method of a builtin's object, or checking a key to be one of the builtin's attrs, for example:I'm not sure if that's a proper way to do this check. Maybe we need to check even for
isinstance
instead of the object's type, but here's my current solution.