Question about missing keys behavior in rule evaluation #112
-
|
Hi, I’ve noticed something inconsistent when evaluating rules with missing keys in the input dictionary. For example: person = {'name': 'Lebron'} rule = rule_engine.Rule( If I apply this rule to person, the result is simply False. However, if the rule is numeric: rule = rule_engine.Rule( then I get an error: Why is this happening? Is it the expected behavior or could it be a bug? Extra info: I’m using a custom context with context = rule_engine.Context(default_value=None). Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
The extra info is super relevant here. So since you defined the >>> surname = None
>>> age = None
>>> surname == 'James'
False
>>> age > 10
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
age > 10
TypeError: '>' not supported between instances of 'NoneType' and 'int'
>>> Rule engine raises the data type mismatch instead of the type error for the same reason as Python, you can't compare None and int using that operator. If you don't set the |
Beta Was this translation helpful? Give feedback.
The extra info is super relevant here. So since you defined the
default_valueasNone, that will effectively treat missing values asNone, allowing you to avoid undefined symbol errors being raised, thus making rule evaluation a bit more permissive. As for why the one rule works and the other doesn't, it's the same behavior you'd see in Python. It's safe to compareNoneto a string via the equality operator but you can't compareNoneto an integer with the greater-than operator.