zeroSteiner / rule-engine

A lightweight, optionally typed expression language with a custom grammar for matching arbitrary Python objects.
https://zerosteiner.github.io/rule-engine/
BSD 3-Clause "New" or "Revised" License
445 stars 54 forks source link

TypeError: can't compare offset-naive and offset-aware datetimes #22

Closed priyankmav closed 3 years ago

priyankmav commented 3 years ago

Hello Spencer,

Seems like the below exception is currently not being handled by the rule-engine. We are currently handling the TypeError at our end. Thought of reporting it. Rule Syntax --> facts['effectiveTMS'] > d'2021-08-12' The effectiveTMS is stored in mongo as "2021-01-01T00:00:00.000+00:00" (Date), retrieved from mongo as 'effectiveTMS': datetime.datetime(2021, 1, 1, 0, 0).

File "/loc/lib/python3.9/site-packages/rule_engine/engine.py", line 620, in matches return bool(self.evaluate(thing)) File "/loc/lib/python3.9/site-packages/rule_engine/engine.py", line 609, in evaluate return self.statement.evaluate(thing) File "/loc/lib/python3.9/site-packages/rule_engine/ast.py", line 944, in evaluate return self.expression.evaluate(thing) File "/loc/lib/python3.9/site-packages/rule_engine/ast.py", line 348, in evaluate return self._evaluator(thing) File "/loc/lib/python3.9/site-packages/rule_engine/ast.py", line 478, in __op_arithmetic return self.__op_arithmetic_values(op, left_value, right_value) File "/loc/lib/python3.9/site-packages/rule_engine/ast.py", line 495, in __op_arithmetic_values return op(left_value, right_value) TypeError: can't compare offset-naive and offset-aware datetimes

Should this be a candidate for DatetimeSyntaxError?

Thanks Priyank Mavani

zeroSteiner commented 3 years ago

There shouldn't be an exception at all. If there's no timezone specified, then the Context's default_timezone should be used and applied to it. Thanks for reporting this, I'll see if I can reproduce it and figure something out.

zeroSteiner commented 3 years ago

Alright yeah this is definitely a bug and I was able to reproduce it. Now I'll work on a fix.

To reproduce it I created this repl setup file:

import datetime
from rule_engine import *

thing = {
  'record': {
    'timestamp': datetime.datetime(2021, 1, 1)
  }
}

Loaded it, and evaluated the expression record['timestamp'] > $now.

zeroSteiner commented 3 years ago

Should be fixed in 683cbea00ad5fb6950a769e48ccf2a86d3570bc0 and v3.3.1 which is now on PyPi. Thanks again for reporting this.

priyankmav commented 3 years ago

Thank you! I updated the package, and I don't see the issue occur.