Skip to content

Commit cad5978

Browse files
committed
Raise IncompleteExpressionError where appropriate
There were a few cases where ParseError was being raised instead of a more specific IncompleteExpressionError including: * Missing closing function parens * Missing closing rbracket for multiselect lists
1 parent 732518a commit cad5978

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

jmespath/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def __str__(self):
2222
# self.lex_position +1 to account for the starting double quote char.
2323
underline = ' ' * (self.lex_position + 1) + '^'
2424
return (
25-
'%s: Parse error at column %s near '
26-
'token "%s" (%s) for expression:\n"%s"\n%s' % (
25+
'%s: Parse error at column %s, '
26+
'token "%s" (%s), for expression:\n"%s"\n%s' % (
2727
self.msg, self.lex_position, self.token_value, self.token_type,
2828
self.expression, underline))
2929

jmespath/parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ def _assert_not_token(self, *token_types):
427427
"Token %s not allowed to be: %s" % (actual_type, token_types))
428428

429429
def _error_nud_token(self, token):
430+
if token['type'] == 'eof':
431+
raise exceptions.IncompleteExpressionError(
432+
token['start'], token['value'], token['type'])
430433
raise exceptions.ParseError(token['start'], token['value'],
431434
token['type'], 'Invalid token.')
432435

tests/test_parser.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ def test_bad_parse(self):
121121

122122
def test_bad_parse_error_message(self):
123123
error_message = (
124-
'Unexpected token: ]: Parse error at column 3 '
125-
'near token "]" (RBRACKET) for expression:\n'
124+
'Unexpected token: ]: Parse error at column 3, '
125+
'token "]" (RBRACKET), for expression:\n'
126126
'"foo]baz"\n'
127127
' ^')
128128
self.assert_error_message('foo]baz', error_message)
@@ -134,6 +134,13 @@ def test_bad_parse_error_message_with_multiselect(self):
134134
' ^')
135135
self.assert_error_message('foo.{bar: baz,bar: bar', error_message)
136136

137+
def test_incomplete_expression_with_missing_paren(self):
138+
error_message = (
139+
'Invalid jmespath expression: Incomplete expression:\n'
140+
'"length(@,"\n'
141+
' ^')
142+
self.assert_error_message('length(@,', error_message)
143+
137144
def test_bad_lexer_values(self):
138145
error_message = (
139146
'Bad jmespath expression: '

0 commit comments

Comments
 (0)