-
Notifications
You must be signed in to change notification settings - Fork 35
fixes issues #44 #45 setDecoder not working, TypeError when reJSON returns None #46
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
Conversation
Kudos, SonarCloud Quality Gate passed! |
rejson/client.py
Outdated
def _decode(self, s, *args, **kwargs): | ||
try: | ||
return self._decoder.decode(s, *args, **kwargs) | ||
except TypeError: | ||
if s is not None: | ||
raise | ||
return None |
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.
def _decode(self, s, *args, **kwargs): | |
try: | |
return self._decoder.decode(s, *args, **kwargs) | |
except TypeError: | |
if s is not None: | |
raise | |
return None | |
def _decode(self, s, *args, **kwargs): | |
try: | |
return self._decoder.decode(s, *args, **kwargs) if s is not None else None | |
except TypeError: | |
raise |
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.
If None
always raises a TypeError
in decode
, can we skip the attempt to decode it?
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 guess we can, I did this to not change the behaviour of the decoder, in the case the user uses a custom decoder and wanted to return a custom value. With this, we override the implementation of the decoder.
But in the general case, this is much better, as we don't even need the try...except
clause. If that is okay to override the decoder implementation, then I prefer it 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.
The default decoder would skip attempting to decode None
.
If, for some reason, users would like to handle None
in the decoder, they can use their own decoder with setDecoder
, so all options are still available.
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.
If they use their own decoder, it would still call the function _decode
, and if the query result was None
, would not attempt to use it, and return None
before. This function is called before the call to the default or custom decoder decode
function. But it only prevents users to handle None
, which can be fine, they can handle it later on.
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.
Right, what I should have written is they could still use set_response_callback
, e.g.,
set_response_callback('json.get', self.myDecoderMethod)
(but that should be done per command)
And then they can still do their own decoding, if they really insist,
But it could also simply be handled later on as you suggested (after the default decoding)
Since this PR is also fixing setDecoder
, do you have a test case also for it?
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.
Perfect, yes if there are very specific user needs, it can always be done.
I believe I often code with the EAFP python way, but the performance in case of a None
value of your solution are 10x better, and about the same if the value isn't None
.
I just pushed the test for setDecoder
and also one more operation on the pipeline test (issue #44). Thanks again for the review!
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.
@bentsku LGTM. Thank you for your contribution and for a fruitful discussion!
Kudos, SonarCloud Quality Gate passed! |
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 👍🏼
Kudos, SonarCloud Quality Gate passed! |
One possibility to fix both #44 and #45. I am not 100% certain on the ramifications, though I doubt it would create any problem in a normal usage of rejson-py. I've tested locally with the
json
module, andujson
.