Skip to content

Commit 6691822

Browse files
authored
Fix JSONPath operator precedence. (#42)
1 parent 602b34b commit 6691822

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Python JSONPath Change Log
22

3+
## Version 0.10.2 (unreleased)
4+
5+
**Fixes**
6+
7+
- Fixed precedence of the logical not operator in JSONPath filter expressions. Previously, logical _or_ and logical _and_ had priority over _not_. See [#41](https://github.com/jg-rp/python-jsonpath/issues/41).
8+
39
## Version 0.10.1
410

511
**Hot fix**

jsonpath/parse.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Parser:
158158
TOKEN_LG: PRECEDENCE_RELATIONAL,
159159
TOKEN_LT: PRECEDENCE_RELATIONAL,
160160
TOKEN_NE: PRECEDENCE_RELATIONAL,
161-
TOKEN_NOT: PRECEDENCE_LOGICALRIGHT,
161+
TOKEN_NOT: PRECEDENCE_PREFIX,
162162
TOKEN_OR: PRECEDENCE_LOGICAL,
163163
TOKEN_RE: PRECEDENCE_RELATIONAL,
164164
TOKEN_RPAREN: PRECEDENCE_LOWEST,
@@ -499,9 +499,7 @@ def parse_prefix_expression(self, stream: TokenStream) -> FilterExpression:
499499
assert tok.kind == TOKEN_NOT
500500
return PrefixExpression(
501501
operator="!",
502-
right=self.parse_filter_selector(
503-
stream, precedence=self.PRECEDENCE_LOGICALRIGHT
504-
),
502+
right=self.parse_filter_selector(stream, precedence=self.PRECEDENCE_PREFIX),
505503
)
506504

507505
def parse_infix_expression(

tests/test_parse.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,21 @@ class Case:
180180
path='$[[email protected] == "ba\\"r"]',
181181
want='$[?@[\'foo\'] == "ba\\"r"]',
182182
),
183+
Case(
184+
description="not binds more tightly than or",
185+
186+
want="$[?(!@['a'] || !@['b'])]",
187+
),
188+
Case(
189+
description="not binds more tightly than and",
190+
191+
want="$[?(!@['a'] && !@['b'])]",
192+
),
193+
Case(
194+
description="control precedence with parens",
195+
path="$[?!(@.a && [email protected])]",
196+
want="$[?!(@['a'] && !@['b'])]",
197+
),
183198
]
184199

185200

0 commit comments

Comments
 (0)