Skip to content

Commit 2dabb87

Browse files
committed
Fix premature unbalanced parentheses check
1 parent 80e6f26 commit 2dabb87

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

jsonpath_rfc9535/lex.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,6 @@ def lex_inside_filter(l: Lexer) -> Optional[StateFn]: # noqa: D103, PLR0915, PL
299299

300300
if c == "]":
301301
l.filter_depth -= 1
302-
if len(l.paren_stack) == 1:
303-
l.error("unbalanced parentheses")
304-
return None
305-
306302
l.backup()
307303
return lex_inside_bracketed_segment
308304

@@ -486,6 +482,9 @@ def tokenize(query: str) -> List[Token]:
486482
lexer, tokens = lex(query)
487483
lexer.run()
488484

485+
if len(lexer.paren_stack) == 1:
486+
raise JSONPathSyntaxError("unbalanced parentheses", token=tokens[-1])
487+
489488
if tokens and tokens[-1].type_ == TokenType.ERROR:
490489
raise JSONPathSyntaxError(tokens[-1].message, token=tokens[-1])
491490

tests/test_errors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class MockEnv(JSONPathEnvironment):
8585
env.find(query, data)
8686

8787

88+
def test_nested_functions_unbalanced_parens(env: JSONPathEnvironment) -> None:
89+
with pytest.raises(JSONPathSyntaxError, match="unbalanced parentheses"):
90+
env.compile("$.values[?match(@.a, value($..['regex'])]")
91+
92+
8893
class FilterLiteralTestCase(NamedTuple):
8994
description: str
9095
query: str

tests/test_issues.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import jsonpath_rfc9535 as jsonpath
2+
3+
4+
def test_issue_13() -> None:
5+
# This was failing with "unbalanced parentheses".
6+
_q = jsonpath.compile("$[? count(@.likes[? @.location]) > 3]")

0 commit comments

Comments
 (0)