Skip to content

Commit

Permalink
Pass run cell's tree as parent to the notebook it is running
Browse files Browse the repository at this point in the history
  • Loading branch information
JCZuurmond committed Jan 17, 2025
1 parent 5c28fc9 commit 1b8f4b5
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/databricks/labs/ucx/source_code/notebooks/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def lint(self) -> Iterable[Advice]:
return

def _load_tree_from_notebook(
self, notebook: Notebook, register_trees: bool, *, parent_tree: Tree | None = None
self, notebook: Notebook, register_trees: bool, *, parent_tree: Tree | None
) -> Failure | None:
for cell in notebook.cells:
failure = None
Expand All @@ -198,7 +198,7 @@ def _load_tree_from_notebook(
return None

def _load_tree_from_python_cell(
self, python_cell: PythonCell, register_trees: bool, parent_tree: Tree | None = None
self, python_cell: PythonCell, register_trees: bool, *, parent_tree: Tree | None
) -> Failure | None:
maybe_tree = Tree.maybe_normalized_parse(python_cell.original_code)
if maybe_tree.failure:
Expand All @@ -224,7 +224,7 @@ def _load_children_from_tree(self, tree: Tree) -> Failure | None:
# need to execute things in intertwined sequence so concat and sort them
nodes = list(cast(Module, tree.node).body)
base_nodes = sorted(base_nodes, key=lambda node: (node.node.lineno, node.node.col_offset))
return self._load_children_with_base_nodes(nodes, base_nodes)
return self._load_children_with_base_nodes(nodes, base_nodes, parent_tree=tree)

@staticmethod
def _list_run_magic_lines(tree: Tree) -> Iterable[MagicLine]:
Expand All @@ -237,24 +237,28 @@ def _ignore_problem(_code: str, _message: str, _node: NodeNG) -> None:
if isinstance(command.as_magic(), RunCommand):
yield command

def _load_children_with_base_nodes(self, nodes: list[NodeNG], base_nodes: list[NodeBase]) -> Failure | None:
def _load_children_with_base_nodes(
self, nodes: list[NodeNG], base_nodes: list[NodeBase], *, parent_tree: Tree | None
) -> Failure | None:
for base_node in base_nodes:
failure = self._load_children_with_base_node(nodes, base_node)
failure = self._load_children_with_base_node(nodes, base_node, parent_tree=parent_tree)
if failure:
return failure
return None

def _load_children_with_base_node(self, nodes: list[NodeNG], base_node: NodeBase) -> Failure | None:
def _load_children_with_base_node(
self, nodes: list[NodeNG], base_node: NodeBase, *, parent_tree: Tree | None
) -> Failure | None:
while len(nodes) > 0:
node = nodes.pop(0)
if node.lineno < base_node.node.lineno:
continue
failure = self._load_children_from_base_node(base_node)
failure = self._load_children_from_base_node(base_node, parent_tree=parent_tree)
if failure:
return failure
return None

def _load_children_from_base_node(self, base_node: NodeBase) -> Failure | None:
def _load_children_from_base_node(self, base_node: NodeBase, *, parent_tree: Tree | None) -> Failure | None:
if isinstance(base_node, SysPathChange):
failure = self._mutate_path_lookup(base_node)
if failure:
Expand All @@ -270,7 +274,7 @@ def _load_children_from_base_node(self, base_node: NodeBase) -> Failure | None:
node=base_node.node,
)
return failure
return self._load_tree_from_notebook(notebook, False)
return self._load_tree_from_notebook(notebook, False, parent_tree=parent_tree)
return None

def _mutate_path_lookup(self, change: SysPathChange) -> Failure | None:
Expand Down

0 comments on commit 1b8f4b5

Please sign in to comment.