Skip to content

Commit 81638ee

Browse files
authored
Refactor classes related to Python AST for clarity and consistency (#1854)
Co-authored-by: Eric Vergnaud <[email protected]>
1 parent 90b0a09 commit 81638ee

File tree

11 files changed

+431
-437
lines changed

11 files changed

+431
-437
lines changed

src/databricks/labs/ucx/source_code/graph.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99

1010
from databricks.labs.ucx.source_code.base import Advisory
1111
from databricks.labs.ucx.source_code.linters.imports import (
12-
ASTLinter,
1312
DbutilsLinter,
1413
ImportSource,
15-
NodeBase,
1614
NotebookRunCall,
1715
SysPathChange,
1816
)
17+
from databricks.labs.ucx.source_code.linters.python_ast import Tree, NodeBase
1918
from databricks.labs.ucx.source_code.path_lookup import PathLookup
2019

2120

@@ -164,10 +163,10 @@ def visit(self, visit_node: Callable[[DependencyGraph], bool | None], visited: s
164163

165164
def build_graph_from_python_source(self, python_code: str) -> list[DependencyProblem]:
166165
problems: list[DependencyProblem] = []
167-
linter = ASTLinter.parse(python_code)
168-
syspath_changes = DbutilsLinter.list_sys_path_changes(linter)
169-
run_calls = DbutilsLinter.list_dbutils_notebook_run_calls(linter)
170-
import_sources, import_problems = DbutilsLinter.list_import_sources(linter, DependencyProblem)
166+
tree = Tree.parse(python_code)
167+
syspath_changes = SysPathChange.extract_from_tree(tree)
168+
run_calls = DbutilsLinter.list_dbutils_notebook_run_calls(tree)
169+
import_sources, import_problems = ImportSource.extract_from_tree(tree, DependencyProblem)
171170
problems.extend(cast(list[DependencyProblem], import_problems))
172171
nodes = syspath_changes + run_calls + import_sources
173172
# need to execute things in intertwined sequence so concat and sort

src/databricks/labs/ucx/source_code/linters/ast_helpers.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/databricks/labs/ucx/source_code/linters/dbfs.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from sqlglot.expressions import Table
66

77
from databricks.labs.ucx.source_code.base import Advice, Linter, Advisory, Deprecation
8-
from databricks.labs.ucx.source_code.linters.imports import Visitor, ASTLinter
8+
from databricks.labs.ucx.source_code.linters.python_ast import Tree, TreeVisitor
99

1010

11-
class DetectDbfsVisitor(Visitor):
11+
class DetectDbfsVisitor(TreeVisitor):
1212
"""
1313
Visitor that detects file system paths in Python code and checks them
1414
against a list of known deprecated paths.
@@ -77,9 +77,9 @@ def lint(self, code: str) -> Iterable[Advice]:
7777
"""
7878
Lints the code looking for file system paths that are deprecated
7979
"""
80-
linter = ASTLinter.parse(code)
80+
tree = Tree.parse(code)
8181
visitor = DetectDbfsVisitor()
82-
visitor.visit(linter.root)
82+
visitor.visit(tree.root)
8383
yield from visitor.get_advices()
8484

8585

0 commit comments

Comments
 (0)