[BugFix] Remove unnecessary eval() calls in script_parser.py (#7403)

* [BugFix] Remove unnecessary eval() calls in script_parser.py

Replace eval() with direct dict access and slice() construction.
Follows the same pattern as #7390 which removed eval() from utils.py.
No behavior change - all replacements are equivalent to the eval'd
expressions.

* style: format script_parser with black

---------

Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
This commit is contained in:
Andrew Barnes
2026-03-13 00:58:52 -04:00
committed by GitHub
parent f7d01ba031
commit 1788e77fe1

View File

@@ -9,7 +9,7 @@ from openbb_cli.session import Session
session = Session()
# pylint: disable=too-many-statements,eval-used,consider-iterating-dictionary
# pylint: disable=too-many-statements,consider-iterating-dictionary
# pylint: disable=too-many-branches,too-many-return-statements
# Necessary for OpenBB keywords
@@ -248,13 +248,11 @@ def parse_openbb_script( # noqa: PLR0911,PLR0912
# in python will only take the first '2'
if VAR_SLICE == "0":
if VAR_NAME in ROUTINE_VARS:
values = eval( # noqa: S307
f'ROUTINE_VARS["{VAR_NAME}"]'
)
values = ROUTINE_VARS[VAR_NAME]
if isinstance(values, list):
templine = templine.replace(
match[0],
eval(f"values[{VAR_SLICE}]"), # noqa: S307
values[int(VAR_SLICE)],
)
else:
templine = templine.replace(match[0], values)
@@ -266,9 +264,7 @@ def parse_openbb_script( # noqa: PLR0911,PLR0912
# Only enters here when any other index from 0 is used
elif VAR_NAME in ROUTINE_VARS:
variable = eval( # noqa: S307
f'ROUTINE_VARS["{VAR_NAME}"]'
)
variable = ROUTINE_VARS[VAR_NAME]
length_variable = (
len(variable) if isinstance(variable, list) else 1
)
@@ -300,23 +296,20 @@ def parse_openbb_script( # noqa: PLR0911,PLR0912
or VAR_SLICE.split(":")[1].isdigit()
)
):
slicing_tuple = "slice("
slicing_tuple += (
VAR_SLICE.split(":")[0]
if VAR_SLICE.split(":")[0].isdigit()
else "None"
parts = VAR_SLICE.split(":")
start = (
int(parts[0])
if parts[0] and parts[0].lstrip("-").isdigit()
else None
)
slicing_tuple += ","
slicing_tuple += (
VAR_SLICE.split(":")[1]
if VAR_SLICE.split(":")[1].isdigit()
else "None"
)
slicing_tuple += ")"
vars_to_loop = eval( # noqa: S307
f'ROUTINE_VARS["{VAR_NAME}"][{slicing_tuple}]'
stop = (
int(parts[1])
if len(parts) > 1
and parts[1]
and parts[1].lstrip("-").isdigit()
else None
)
vars_to_loop = ROUTINE_VARS[VAR_NAME][slice(start, stop)]
# Check whether the slicing was successful or not
if vars_to_loop:
@@ -352,9 +345,7 @@ def parse_openbb_script( # noqa: PLR0911,PLR0912
)
if VAR_NAME in ROUTINE_VARS:
value = eval( # noqa: S307
f'ROUTINE_VARS["{VAR_NAME}"]'
)
value = ROUTINE_VARS[VAR_NAME]
# If the value is a list, we want to replace it with the whole list
if isinstance(value, list):