-
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1734 from Giskard-AI/GSK-2468
GSK-2468 Try to automatically add import when adding kwargs tests to giskard Hub
- Loading branch information
Showing
2 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters