Skip to content

Commit

Permalink
Merge pull request #1073 from guardrails-ai/eval-fix
Browse files Browse the repository at this point in the history
safe eval
  • Loading branch information
dtam authored Sep 17, 2024
2 parents 9ffd452 + 14955e6 commit ab12701
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion guardrails/utils/validator_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ruff: noqa
"""This module contains the constants and utils used by the validator.py."""

from ast import literal_eval
from typing import Any, Dict, List, Optional, Tuple, Type, Union, cast

from guardrails_api_client import ValidatorReference
Expand Down Expand Up @@ -33,7 +34,7 @@ def parse_rail_arguments(arg_tokens: List[str]) -> List[Any]:
# and be responsible for parsing them to the correct types.
# Option 2: We use something like the Validator Manifest that describes the arguments
# to parse the values from the string WITHOUT an eval.
t = eval(t)
t = literal_eval(t)
except (ValueError, SyntaxError, NameError) as e:
raise ValueError(
f"Python expression `{t}` is not valid, "
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, *args, **kwargs):
("test-validator: a", ["a"]),
("test-validator: a b", ["a", "b"]),
(
"test-validator: {list(range(5))} a b",
"test-validator: {[0,1,2,3,4]} a b",
[[0, 1, 2, 3, 4], "a", "b"],
),
("test-validator: {[1, 2, 3]} a b", [[1, 2, 3], "a", "b"]),
Expand All @@ -31,7 +31,7 @@ def __init__(self, *args, **kwargs):
[{"a": 1, "b": 2}, "c", "d"],
),
(
"test-validator: {1 + 2} {{'a': 1, 'b': 2}} c d",
"test-validator: {3} {{'a': 1, 'b': 2}} c d",
[3, {"a": 1, "b": 2}, "c", "d"],
),
],
Expand Down

0 comments on commit ab12701

Please sign in to comment.