Skip to content

Commit

Permalink
fix: check if whitelisted lib is actually exists in the additional de…
Browse files Browse the repository at this point in the history
…ps (#1499)
  • Loading branch information
ArslanSaleem authored Jan 2, 2025
1 parent 63df017 commit cfeb071
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
27 changes: 13 additions & 14 deletions pandasai/pipelines/chat/code_cleaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,23 +627,22 @@ def _check_imports(self, node: Union[ast.Import, ast.ImportFrom]):
if library == "pandas":
return

if (
library
in WHITELISTED_LIBRARIES + self._config.custom_whitelisted_dependencies
):
for alias in node.names:
self._additional_dependencies.append(
{
"module": module,
"name": alias.name,
"alias": alias.asname or alias.name,
}
)
return
whitelisted_libs = (
WHITELISTED_LIBRARIES + self._config.custom_whitelisted_dependencies
)

if library not in WHITELISTED_LIBRARIES:
if library not in whitelisted_libs:
raise BadImportError(
f"The library '{library}' is not in the list of whitelisted libraries. "
"To learn how to whitelist custom dependencies, visit: "
"https://docs.pandas-ai.com/custom-whitelisted-dependencies#custom-whitelisted-dependencies"
)

for alias in node.names:
self._additional_dependencies.append(
{
"module": module,
"name": alias.name,
"alias": alias.asname or alias.name,
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,11 @@ def test_custom_whitelisted_dependencies(
code_cleaning._config.custom_whitelisted_dependencies = ["my_custom_library"]
output = code_cleaning.execute(code, context=context, logger=logger)

print(code_cleaning._additional_dependencies)
assert output.output == "my_custom_library.do_something()"
assert (
code_cleaning._additional_dependencies[0]["module"] == "my_custom_library"
)
assert isinstance(output, LogicUnitOutput)
assert output.success
assert output.message == "Code Cleaned Successfully"
Expand Down

0 comments on commit cfeb071

Please sign in to comment.