Skip to content

Commit

Permalink
Raise FloatSyntaxError now as appropriate
Browse files Browse the repository at this point in the history
Since strings can now be parsed with the new function, it should raise a
more specific error class.
  • Loading branch information
zeroSteiner committed Jul 9, 2023
1 parent 16b5736 commit 010aa5f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
5 changes: 4 additions & 1 deletion docs/source/change_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Version 4.0.0
* **Breaking:** Changed ``STRING.to_ary`` to return an array of characters instead of splitting the string
* Use the new builtin ``$split`` function to split a string on whitespace into an array of words
* **Breaking:** Changed :py:class:`~rule_engine.engine.Context` to use keyword-only arguments
* **Breaking:** Dropped support for Python versions 3.4 and 3.5
* **Breaking:** Invalid floating point literals now raise :py:exc:`~.errors.FloatSyntaxError` instead of
:py:exc:`~.errors.RuleSyntaxError`

Version 3.x.x
-------------
Expand All @@ -24,7 +27,7 @@ Version 3.6.0

Released :release:`3.6.0` on June 16th, 2023

* Removed testing for Python 3.4 and 3.5 on GitHub Actions
* Removed testing for Python versions 3.4 and 3.5 on GitHub Actions
* Add regex error details to the debug REPL
* Add support for Python-style comments

Expand Down
6 changes: 6 additions & 0 deletions docs/source/rule_engine/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Exceptions
:show-inheritance:
:special-members: __init__

.. autoexception:: FloatSyntaxError
:members:
:show-inheritance:
:special-members: __init__

.. autoexception:: EngineError
:members:
:show-inheritance:
Expand Down Expand Up @@ -96,6 +101,7 @@ The class hierarchy for Rule Engine exceptions is:
+-- SymbolTypeError
+-- SyntaxError
+-- DatetimeSyntaxError
+-- FloatSyntaxError
+-- RegexSyntaxError
+-- RuleSyntaxError
+-- TimedeltaSyntaxError
4 changes: 2 additions & 2 deletions lib/rule_engine/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def parse_datetime(string, default_timezone):

def parse_float(string):
if re.match('^0[0-9]', string):
raise errors.RuleSyntaxError('invalid floating point literal: ' + string + ' (leading zeros in decimal literals are not permitted)')
raise errors.FloatSyntaxError('invalid floating point literal (leading zeros in decimal literals are not permitted)', string)
try:
if re.match('^0[box]', string):
val = decimal.Decimal(pyast.literal_eval(string))
else:
val = decimal.Decimal(string)
except Exception:
raise errors.RuleSyntaxError('invalid floating point literal: ' + string) from None
raise errors.FloatSyntaxError('invalid floating point literal', string) from None
return val

def parse_timedelta(periodstring):
Expand Down
15 changes: 15 additions & 0 deletions lib/rule_engine/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def __init__(self, message, value):
self.value = value
"""The datetime value which contains the syntax error which caused this exception to be raised."""

class FloatSyntaxError(SyntaxError):
"""
An error raised for issues regarding the use of improperly formatted float expressions.
.. versionadded:: 4.0.0
"""
def __init__(self, message, value):
"""
:param str message: A text description of what error occurred.
:param str value: The float value which contains the syntax error which caused this exception to be raised.
"""
super(FloatSyntaxError, self).__init__(message)
self.value = value
"""The float value which contains the syntax error which caused this exception to be raised."""

class TimedeltaSyntaxError(SyntaxError):
"""
An error raised for issues regarding the use of improperly formatted timedelta expressions.
Expand Down
2 changes: 1 addition & 1 deletion tests/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_engine_builtins_function_parse_float(self):
self.assertBuiltinFunction('parse_float', 1e1, '1e1')
self.assertBuiltinFunction('parse_float', float('inf'), 'inf')
self.assertBuiltinFunction('parse_float', -1, '-1')
with self.assertRaises(errors.RuleSyntaxError):
with self.assertRaises(errors.FloatSyntaxError):
self.assertBuiltinFunction('parse_float', 1, 'f00d')

def test_engine_builtins_function_parse_timedelta(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ def test_number_54(self):
"test=='NOTTEST' and count==01 and other=='other'"
)
for rule in rules:
with self.assertRaises(errors.RuleSyntaxError):
with self.assertRaises(errors.FloatSyntaxError):
engine.Rule(rule)
2 changes: 1 addition & 1 deletion tests/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def test_parse_float_base_8(self):
def test_parse_float_base_10(self):
self.assertLiteralStatementEqual('0', ast.FloatExpression, decimal.Decimal('0'))
self.assertLiteralStatementEqual('99', ast.FloatExpression, decimal.Decimal('99'))
with self.assertRaises(errors.RuleSyntaxError):
with self.assertRaises(errors.FloatSyntaxError):
self._parse('00', self.context)

def test_parse_float_base_16(self):
Expand Down

0 comments on commit 010aa5f

Please sign in to comment.