Skip to content

Commit

Permalink
Merge pull request #1734 from Giskard-AI/GSK-2468
Browse files Browse the repository at this point in the history
GSK-2468 Try to automatically add import when adding kwargs tests to giskard Hub
  • Loading branch information
andreybavt authored Jan 19, 2024
2 parents 4ab196b + 126cd5f commit 3941812
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 30 additions & 0 deletions giskard/core/kwargs_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Dict, List, Optional, Set, Tuple, Union

import inspect
from collections import defaultdict


def get_imports_code(obj):
imports = _get_imports(obj)
return "\n".join([f'from {module} import {", ".join(classes)}' for module, classes in imports.items()])


def _get_imports(obj, imports: Optional[defaultdict] = None):
imports = imports or defaultdict(set)
module = inspect.getmodule(obj)

if isinstance(obj, Dict):
for i in obj.values():
imports = _get_imports(i, imports)
elif isinstance(obj, Union[List, Set, Tuple]):
for i in obj:
print(i)
imports = _get_imports(i, imports)
elif hasattr(obj, "__dict__"):
imports = _get_imports(obj.__dict__.values(), imports)

if module is None:
return imports

imports[module.__name__].add(obj.__class__.__name__)
return imports
3 changes: 2 additions & 1 deletion giskard/core/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ..client.python_utils import warning
from ..utils.analytics_collector import analytics
from ..utils.artifacts import serialize_parameter
from .kwargs_utils import get_imports_code

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -514,7 +515,7 @@ def to_dto(self, client: GiskardClient, project_key: str, uploaded_uuid_status:
)

kwargs_params = [
f"kwargs[{repr(pname)}] = {repr(value)}"
f"{get_imports_code(value)}\nkwargs[{repr(pname)}] = {repr(value)}"
for pname, value in t.provided_inputs.items()
if pname not in t.giskard_test.meta.args
]
Expand Down

0 comments on commit 3941812

Please sign in to comment.