Skip to content

Commit 1788e77

Browse files
[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>
1 parent f7d01ba commit 1788e77

1 file changed

Lines changed: 17 additions & 26 deletions

File tree

cli/openbb_cli/controllers/script_parser.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
session = Session()
1111

12-
# pylint: disable=too-many-statements,eval-used,consider-iterating-dictionary
12+
# pylint: disable=too-many-statements,consider-iterating-dictionary
1313
# pylint: disable=too-many-branches,too-many-return-statements
1414

1515
# Necessary for OpenBB keywords
@@ -248,13 +248,11 @@ def parse_openbb_script( # noqa: PLR0911,PLR0912
248248
# in python will only take the first '2'
249249
if VAR_SLICE == "0":
250250
if VAR_NAME in ROUTINE_VARS:
251-
values = eval( # noqa: S307
252-
f'ROUTINE_VARS["{VAR_NAME}"]'
253-
)
251+
values = ROUTINE_VARS[VAR_NAME]
254252
if isinstance(values, list):
255253
templine = templine.replace(
256254
match[0],
257-
eval(f"values[{VAR_SLICE}]"), # noqa: S307
255+
values[int(VAR_SLICE)],
258256
)
259257
else:
260258
templine = templine.replace(match[0], values)
@@ -266,9 +264,7 @@ def parse_openbb_script( # noqa: PLR0911,PLR0912
266264

267265
# Only enters here when any other index from 0 is used
268266
elif VAR_NAME in ROUTINE_VARS:
269-
variable = eval( # noqa: S307
270-
f'ROUTINE_VARS["{VAR_NAME}"]'
271-
)
267+
variable = ROUTINE_VARS[VAR_NAME]
272268
length_variable = (
273269
len(variable) if isinstance(variable, list) else 1
274270
)
@@ -300,23 +296,20 @@ def parse_openbb_script( # noqa: PLR0911,PLR0912
300296
or VAR_SLICE.split(":")[1].isdigit()
301297
)
302298
):
303-
slicing_tuple = "slice("
304-
slicing_tuple += (
305-
VAR_SLICE.split(":")[0]
306-
if VAR_SLICE.split(":")[0].isdigit()
307-
else "None"
299+
parts = VAR_SLICE.split(":")
300+
start = (
301+
int(parts[0])
302+
if parts[0] and parts[0].lstrip("-").isdigit()
303+
else None
308304
)
309-
slicing_tuple += ","
310-
slicing_tuple += (
311-
VAR_SLICE.split(":")[1]
312-
if VAR_SLICE.split(":")[1].isdigit()
313-
else "None"
314-
)
315-
slicing_tuple += ")"
316-
317-
vars_to_loop = eval( # noqa: S307
318-
f'ROUTINE_VARS["{VAR_NAME}"][{slicing_tuple}]'
305+
stop = (
306+
int(parts[1])
307+
if len(parts) > 1
308+
and parts[1]
309+
and parts[1].lstrip("-").isdigit()
310+
else None
319311
)
312+
vars_to_loop = ROUTINE_VARS[VAR_NAME][slice(start, stop)]
320313

321314
# Check whether the slicing was successful or not
322315
if vars_to_loop:
@@ -352,9 +345,7 @@ def parse_openbb_script( # noqa: PLR0911,PLR0912
352345
)
353346

354347
if VAR_NAME in ROUTINE_VARS:
355-
value = eval( # noqa: S307
356-
f'ROUTINE_VARS["{VAR_NAME}"]'
357-
)
348+
value = ROUTINE_VARS[VAR_NAME]
358349

359350
# If the value is a list, we want to replace it with the whole list
360351
if isinstance(value, list):

0 commit comments

Comments
 (0)