-
Notifications
You must be signed in to change notification settings - Fork 87
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
Infer values from child notebook in run cell #2075
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7a3170f
append statements
ericvergnaud e56dda6
reuse python linters across cells
ericvergnaud fa9fb00
resolve cyclic import
ericvergnaud c8d05b0
fix merge issue
ericvergnaud f364b02
Merge branch 'main' into infer-values-across-notebook-cells
ericvergnaud c3cbc7b
successfully infers values from child notebook
ericvergnaud 56e3530
successfully infers values from child notebook in run cell
ericvergnaud d9d3172
formatting
ericvergnaud 1e5feff
formatting
ericvergnaud 257fea6
PythonSequentialLinter is stateful so instantiate it when required
ericvergnaud dacf50a
Merge branch 'main' into infer-values-across-notebook-cells
ericvergnaud 65072d6
Merge branch 'infer-values-across-notebook-cells' into infer-values-f…
ericvergnaud fc209c5
Merge branch 'main' into infer-values-from-child-notebook-in-run-cell
ericvergnaud 76a29a5
address comments
ericvergnaud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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 |
---|---|---|
|
@@ -5,15 +5,24 @@ | |
from collections.abc import Iterable | ||
from functools import cached_property | ||
from pathlib import Path | ||
from typing import cast | ||
|
||
from databricks.sdk.service.workspace import Language | ||
|
||
from databricks.labs.ucx.hive_metastore.migration_status import MigrationIndex | ||
from databricks.labs.ucx.source_code.base import Advice, Failure, Linter | ||
from databricks.labs.ucx.source_code.base import Advice, Failure, Linter, PythonSequentialLinter | ||
|
||
from databricks.labs.ucx.source_code.graph import SourceContainer, DependencyGraph, DependencyProblem | ||
from databricks.labs.ucx.source_code.linters.context import LinterContext | ||
from databricks.labs.ucx.source_code.notebooks.cells import CellLanguage, Cell, CELL_SEPARATOR, NOTEBOOK_HEADER | ||
from databricks.labs.ucx.source_code.notebooks.cells import ( | ||
CellLanguage, | ||
Cell, | ||
CELL_SEPARATOR, | ||
NOTEBOOK_HEADER, | ||
RunCell, | ||
PythonCell, | ||
) | ||
from databricks.labs.ucx.source_code.path_lookup import PathLookup | ||
|
||
|
||
class Notebook(SourceContainer): | ||
|
@@ -84,22 +93,27 @@ class NotebookLinter: | |
to the code cells according to the language of the cell. | ||
""" | ||
|
||
def __init__(self, langs: LinterContext, notebook: Notebook): | ||
self._languages: LinterContext = langs | ||
def __init__(self, context: LinterContext, path_lookup: PathLookup, notebook: Notebook): | ||
self._context: LinterContext = context | ||
self._path_lookup = path_lookup | ||
self._notebook: Notebook = notebook | ||
# reuse Python linter, which accumulates statements for improved inference | ||
self._python_linter = langs.linter(Language.PYTHON) | ||
self._python_linter: PythonSequentialLinter = cast(PythonSequentialLinter, context.linter(Language.PYTHON)) | ||
|
||
@classmethod | ||
def from_source(cls, index: MigrationIndex, source: str, default_language: Language) -> 'NotebookLinter': | ||
def from_source( | ||
cls, index: MigrationIndex, path_lookup: PathLookup, source: str, default_language: Language | ||
) -> NotebookLinter: | ||
ctx = LinterContext(index) | ||
notebook = Notebook.parse(Path(""), source, default_language) | ||
assert notebook is not None | ||
return cls(ctx, notebook) | ||
return cls(ctx, path_lookup, notebook) | ||
|
||
def lint(self) -> Iterable[Advice]: | ||
for cell in self._notebook.cells: | ||
if not self._languages.is_supported(cell.language.language): | ||
if isinstance(cell, RunCell): | ||
self._load_source_from_run_cell(cell) | ||
if not self._context.is_supported(cell.language.language): | ||
continue | ||
linter = self._linter(cell.language.language) | ||
for advice in linter.lint(cell.original_code): | ||
|
@@ -111,7 +125,29 @@ def lint(self) -> Iterable[Advice]: | |
def _linter(self, language: Language) -> Linter: | ||
if language is Language.PYTHON: | ||
return self._python_linter | ||
return self._languages.linter(language) | ||
return self._context.linter(language) | ||
|
||
def _load_source_from_run_cell(self, run_cell: RunCell): | ||
path, _, _ = run_cell.read_notebook_path() | ||
if path is None: | ||
return # malformed run cell already reported | ||
resolved = self._path_lookup.resolve(path) | ||
if resolved is None: | ||
return # already reported during dependency building | ||
# TODO deal with workspace notebooks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noted. |
||
language = SUPPORTED_EXTENSION_LANGUAGES.get(resolved.suffix.lower(), None) | ||
# we only support Python for now | ||
if language is not Language.PYTHON: | ||
return | ||
source = resolved.read_text(_guess_encoding(resolved)) | ||
notebook = Notebook.parse(path, source, language) | ||
for cell in notebook.cells: | ||
if isinstance(cell, RunCell): | ||
self._load_source_from_run_cell(cell) | ||
continue | ||
if not isinstance(cell, PythonCell): | ||
continue | ||
self._python_linter.process_child_cell(cell.original_code) | ||
|
||
@staticmethod | ||
def name() -> str: | ||
|
@@ -124,6 +160,20 @@ def name() -> str: | |
} | ||
|
||
|
||
def _guess_encoding(path: Path): | ||
# some files encode a unicode BOM (byte-order-mark), so let's use that if available | ||
with path.open('rb') as _file: | ||
raw = _file.read(4) | ||
if raw.startswith(codecs.BOM_UTF32_LE) or raw.startswith(codecs.BOM_UTF32_BE): | ||
return 'utf-32' | ||
if raw.startswith(codecs.BOM_UTF16_LE) or raw.startswith(codecs.BOM_UTF16_BE): | ||
return 'utf-16' | ||
if raw.startswith(codecs.BOM_UTF8): | ||
return 'utf-8-sig' | ||
# no BOM, let's use default encoding | ||
return locale.getpreferredencoding(False) | ||
|
||
|
||
class FileLinter: | ||
_NOT_YET_SUPPORTED_SUFFIXES = { | ||
'.scala', | ||
|
@@ -168,30 +218,18 @@ class FileLinter: | |
'zip-safe', | ||
} | ||
|
||
def __init__(self, ctx: LinterContext, path: Path, content: str | None = None): | ||
def __init__(self, ctx: LinterContext, path_lookup: PathLookup, path: Path, content: str | None = None): | ||
self._ctx: LinterContext = ctx | ||
self._path_lookup = path_lookup | ||
self._path: Path = path | ||
self._content = content | ||
|
||
@cached_property | ||
def _source_code(self) -> str: | ||
if self._content is None: | ||
self._content = self._path.read_text(self._guess_encoding()) | ||
self._content = self._path.read_text(_guess_encoding(self._path)) | ||
return self._content | ||
|
||
def _guess_encoding(self): | ||
# some files encode a unicode BOM (byte-order-mark), so let's use that if available | ||
with self._path.open('rb') as _file: | ||
raw = _file.read(4) | ||
if raw.startswith(codecs.BOM_UTF32_LE) or raw.startswith(codecs.BOM_UTF32_BE): | ||
return 'utf-32' | ||
if raw.startswith(codecs.BOM_UTF16_LE) or raw.startswith(codecs.BOM_UTF16_BE): | ||
return 'utf-16' | ||
if raw.startswith(codecs.BOM_UTF8): | ||
return 'utf-8-sig' | ||
# no BOM, let's use default encoding | ||
return locale.getpreferredencoding(False) | ||
|
||
def _file_language(self): | ||
return SUPPORTED_EXTENSION_LANGUAGES.get(self._path.suffix.lower()) | ||
|
||
|
@@ -243,5 +281,5 @@ def _lint_file(self): | |
|
||
def _lint_notebook(self): | ||
notebook = Notebook.parse(self._path, self._source_code, self._file_language()) | ||
notebook_linter = NotebookLinter(self._ctx, notebook) | ||
notebook_linter = NotebookLinter(self._ctx, self._path_lookup, notebook) | ||
yield from notebook_linter.lint() |
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
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,9 @@ | ||
# Databricks notebook source | ||
|
||
# COMMAND ---------- | ||
|
||
# MAGIC %run "./values_across_notebooks_child.py" | ||
|
||
# COMMAND ---------- | ||
|
||
spark.table(f"{a}") |
3 changes: 3 additions & 0 deletions
3
tests/unit/source_code/samples/values_across_notebooks_child.py
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,3 @@ | ||
# Databricks notebook source | ||
|
||
a = 12 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type interface exposes too much unnecessary private information, make this method private and wrap it with public method with only what we need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done