Skip to content
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

Raise exception when match failure, to distinguish from “matched, value is None” #113

Closed
benf-wspdigital opened this issue Oct 25, 2016 · 2 comments

Comments

@benf-wspdigital
Copy link

The jmespath.search function returns None in two distinct cases:

>>> import jmespath

>>> foo = {'bar': {'lorem': 13, 'ipsum': None}}
>>> repr(jmespath.search('bar.lorem', foo))
'13'
>>> repr(jmespath.search('bar.ipsum', foo))    # Path matches, value None
'None'
>>> repr(jmespath.search('dolor', foo))    # Path does not match
'None'

It appears the JMESPath search API returns None in these two distinct cases. How can the caller know the difference between them?

I would expect no return in the case of a match failure, and instead an exception (such as KeyError or ValueError).

@benf-wspdigital benf-wspdigital changed the title Distinguish “matched, value is None” versus “failed to match” Raise exception when match failure, to distinguish from “matched, value is None” Oct 25, 2016
@jamesls
Copy link
Member

jamesls commented Dec 7, 2016

If you want to check if a key exists in the hash, you can use the contains function:

>>> import jmespath
>>> foo = {'bar': {'lorem': 13, 'ipsum': None}}
>>> repr(jmespath.search("bar.ipsum", foo))
'None'
>>> repr(jmespath.search("contains(keys(bar), 'ipsum')", foo))
'True'

Let me know if that doesn't work for you.

@jamesls jamesls closed this as completed Dec 7, 2016
@benf-wspdigital
Copy link
Author

benf-wspdigital commented Dec 8, 2016

The JMESPath specification says, about the value returned for a search:

The return value for an identifier is the value associated with the identifier. If the identifier does not exist in the JSON document, than a null value is returned.

So if the value associated with the identifier is null, the specification deliberately conflates that with the “does not exist” case. Hence this library is behaving correctly by the specification.

I maintain the specification is broken by design. The “does not exist” behaviour should, in order to be easily detected, be entirely disjoint from any valid value that could be returned by a match. Raising an exception would be an appropriate implementation.

So that's a bug in the specification. I don't know where to report that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants