Skip to content

Commit

Permalink
Make it possible to opt-out from loose is/is-not operations
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Dec 29, 2023
1 parent 6afc33e commit f536f23
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion leval/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(
max_time: Optional[float] = None,
allowed_constant_types: Optional[Iterable[type]] = None,
allowed_container_types: Optional[Iterable[type]] = None,
loose_is_operator: bool = True,
):
"""
Initialize an evaluator with access to the given evaluation universe.
Expand All @@ -62,6 +63,7 @@ def __init__(
self.universe = universe
self.max_depth = _default_if_none(max_depth, self.default_max_depth)
self.max_time = float(max_time or 0)
self.loose_is_operator = bool(loose_is_operator)
self.allowed_constant_types = frozenset(
_default_if_none(
allowed_constant_types,
Expand Down Expand Up @@ -123,7 +125,7 @@ def visit_Compare(self, node): # noqa: D102
if len(node.ops) != 1:
raise InvalidOperation("Only simple comparisons are supported", node=node)
op = node.ops[0]
if isinstance(op, (ast.Is, ast.IsNot)):
if self.loose_is_operator and isinstance(op, (ast.Is, ast.IsNot)):
left = self._visit_or_none(node.left)
right = self._visit_or_none(node.comparators[0])
else:
Expand Down

0 comments on commit f536f23

Please sign in to comment.