-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support values of inner docs given as AttrDict instances #3080
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
Support values of inner docs given as AttrDict instances #3080
Conversation
return data | ||
|
||
return data.to_dict(skip_empty=skip_empty) | ||
try: |
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.
Why not just isinstance(data, AttrDict)
?
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.
Because this would also include support for other types that are not AttrDict. And because it avoids evaluating the conditional for every field that is saved, so it should perform a bit better.
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.
Because this would also include support for other types that are not AttrDict.
Which would probably lead to more crash anyway?
And because it avoids evaluating the conditional for every field that is saved, so it should perform a bit better.
Handling an exception will cost many time what an isinstance
cost. But does a few nano seconds performance of this even matter here 🤔 Just seems an unneeded "complex" structure flow to hide a very simple intent.
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.
The exception would be handled only for the fields that have a wrong type. The conditional would have to be evaluated for every single field.
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.
And to my point, does it even matter in this case? Elasticsearch-dsl serialization/deserialization code is clearly not designed with performance in mind, so I don't really see why this one isinstance
is seen as a potential issue at the cost of readability. But that's just my 2 cents. (and my last comment on this :) )
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.
Actually, fwiw, I suppose your method is consistent with Field._safe_serialize
d09d681
to
d6e025d
Compare
@isra17 Thanks for the discussion and the insights you provided. I'm going to do some additional testing, and if you don't mind, I'm also going to bring the unit test that you created in your PR, which is different than mine. As far as the exception vs. no exception method for preventing this error it's really not a big deal, but I have seen other methods that do an instance check like you suggested, so maybe that wins in terms of consistency. But I'm not sure if that is enough. I don't know yet if we need a similar fix for |
Thanks for looking at the issue! I understand this is a bit of a niche error, so it's nice to see it fixed :)
Of course!
For what it's worth, I think if |
Ha, |
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! LGTM.
5cae556
to
34dc8b7
Compare
* Support values of inner docs given as AttrDict instances * one more unit test (cherry picked from commit f68539e)
* Support values of inner docs given as AttrDict instances * one more unit test (cherry picked from commit f68539e)
* Support values of inner docs given as AttrDict instances * one more unit test (cherry picked from commit f68539e)
* Support values of inner docs given as AttrDict instances * one more unit test (cherry picked from commit f68539e) Co-authored-by: Miguel Grinberg <[email protected]>
* Support values of inner docs given as AttrDict instances * one more unit test (cherry picked from commit f68539e) Co-authored-by: Miguel Grinberg <[email protected]>
* Support values of inner docs given as AttrDict instances * one more unit test (cherry picked from commit f68539e) Co-authored-by: Miguel Grinberg <[email protected]>
Fixes #3075.
It appears that some users pass
AttrDict
instances as values for fields of typeObject
orNested
. If the document is validated (the default whensave()
is called) then these are handled well, but some users also callsave(validate=False)
or justto_dict()
, in which case the AttrDicts cause the error described in #3075. This change adds logic that tolerates AttrDict values, and treats them like regular dicts (which are already tolerated).