Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic import issue #3053

Merged
merged 7 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/databricks/labs/ucx/source_code/linters/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from astroid import ( # type: ignore
Attribute,
Call,
Const,
InferenceError,
Import,
ImportFrom,
Expand Down Expand Up @@ -71,9 +70,18 @@ def _make_sources_for_import_call_nodes(
problems: list[T],
) -> Iterable[ImportSource]:
for node in nodes:
arg = node.args[0]
if isinstance(arg, Const):
yield ImportSource(node, arg.value)
yield from cls._make_sources_for_import_call_node(node, problem_factory, problems)

@classmethod
def _make_sources_for_import_call_node(
cls,
node: Call,
problem_factory: ProblemFactory,
problems: list[T],
) -> Iterable[ImportSource]:
for inferred in InferredValue.infer_from_node(node.args[0]):
nfx marked this conversation as resolved.
Show resolved Hide resolved
if inferred.is_inferred():
yield ImportSource(node, inferred.as_string())
continue
problem = problem_factory(
'dependency-not-constant', "Can't check dependency not provided as a constant", node
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/source_code/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,22 @@ def module_compatibility(self, name: str) -> Compatibility:
# visit the graph without a 'visited' set
roots = graph.root_dependencies
assert roots


def test_graph_imports_dynamic_import():
allow_list = KnownList()
library_resolver = PythonLibraryResolver(allow_list)
notebook_resolver = NotebookResolver(NotebookLoader())
import_resolver = ImportFileResolver(FileLoader(), allow_list)
path_lookup = PathLookup.from_sys_path(Path(__file__).parent)
dependency_resolver = DependencyResolver(
library_resolver, notebook_resolver, import_resolver, import_resolver, path_lookup
)
root_path = Path(__file__).parent.parent.parent / "unit" / "source_code" / "samples" / "import-module.py"
assert root_path.is_file()
maybe = dependency_resolver.resolve_file(path_lookup, root_path)
assert maybe.dependency
graph = DependencyGraph(maybe.dependency, None, dependency_resolver, path_lookup, CurrentSessionState())
container = maybe.dependency.load(path_lookup)
problems = container.build_dependency_graph(graph)
assert not problems
4 changes: 4 additions & 0 deletions tests/unit/source_code/samples/import-module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import importlib

module_name = "astroid"
module = importlib.import_module(module_name)