Skip to content

Commit 55e5282

Browse files
authored
Rename file language helper function (#3661)
## Changes Rename and document getting supported file language helper function to make more clear what it does. IMO it was not clear that this function contains essential decision power as it is the filter for passing files to the linter only if they are supported (in contrast with inferring any language - also the non-supported ones). For example, before #3655, the `FileLinter` contains filters for extensions, however this redundant as that is already handled by this helper function. The rename makes it more explicit what this function does. The documentation makes it more clear how to use the function ### Functionality - [x] modified linter related code.
1 parent 8de37e1 commit 55e5282

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,14 @@ def parse_security_mode(mode_str: str | None) -> compute.DataSecurityMode | None
320320
}
321321

322322

323-
def file_language(path: Path) -> Language | None:
323+
def infer_file_language_if_supported(path: Path) -> Language | None:
324+
"""Infer the file language if it is supported by UCX's linter module.
325+
326+
This function returns the language of the file based on the file
327+
extension. If the file extension is not supported, it returns None.
328+
329+
Use this function to filter paths before passing it to the linters.
330+
"""
324331
return SUPPORTED_EXTENSION_LANGUAGES.get(path.suffix.lower())
325332

326333

@@ -415,7 +422,7 @@ def is_a_notebook(path: Path, content: str | None = None) -> bool:
415422
return path.is_notebook()
416423
if not path.is_file():
417424
return False
418-
language = file_language(path)
425+
language = infer_file_language_if_supported(path)
419426
if not language:
420427
return False
421428
magic_header = f"{LANGUAGE_COMMENT_PREFIXES.get(language)} {NOTEBOOK_HEADER}"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from databricks.sdk.service.workspace import Language
88

9-
from databricks.labs.ucx.source_code.base import file_language, safe_read_text
9+
from databricks.labs.ucx.source_code.base import infer_file_language_if_supported, safe_read_text
1010
from databricks.labs.ucx.source_code.graph import (
1111
SourceContainer,
1212
DependencyGraph,
@@ -95,7 +95,7 @@ def load_dependency(self, path_lookup: PathLookup, dependency: Dependency) -> Lo
9595
# Paths are excluded from further processing by loading them as stub container.
9696
# Note we don't return `None`, as the path is found.
9797
return StubContainer(resolved_path)
98-
language = file_language(resolved_path)
98+
language = infer_file_language_if_supported(resolved_path)
9999
if not language:
100100
return StubContainer(resolved_path)
101101
content = safe_read_text(resolved_path)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from databricks.sdk.service.workspace import Language
1010

1111
from databricks.labs.ucx.source_code.base import (
12-
file_language,
1312
Advice,
1413
Failure,
14+
infer_file_language_if_supported,
1515
safe_read_text,
1616
)
1717
from databricks.labs.ucx.source_code.files import LocalFile
@@ -201,9 +201,9 @@ def _resolve_and_parse_notebook_path(self, path: Path | None) -> Notebook | None
201201
if resolved is None:
202202
return None # already reported during dependency building
203203
# TODO deal with workspace notebooks
204-
language = file_language(resolved)
204+
language = infer_file_language_if_supported(resolved)
205205
# we only support Python notebooks for now
206-
if language is not Language.PYTHON:
206+
if language != Language.PYTHON:
207207
logger.warning(f"Unsupported notebook language: {language}")
208208
return None
209209
source = safe_read_text(resolved)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
LocatedAdvice,
1919
UsedTable,
2020
SourceInfo,
21-
file_language,
21+
infer_file_language_if_supported,
2222
is_a_notebook,
2323
safe_read_text,
2424
)
@@ -149,9 +149,9 @@ def _process_dependency(
149149
path_lookup: PathLookup,
150150
inherited_tree: Tree | None,
151151
) -> Iterable[S]:
152-
language = file_language(dependency.path)
152+
language = infer_file_language_if_supported(dependency.path)
153153
if not language:
154-
logger.warning(f"Unknown language for {dependency.path}")
154+
logger.warning(f"Unsupported language for {dependency.path}")
155155
return
156156
cell_language = CellLanguage.of_language(language)
157157
source = safe_read_text(dependency.path)

src/databricks/labs/ucx/source_code/notebooks/loaders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from databricks.sdk.service.workspace import Language
88

9-
from databricks.labs.ucx.source_code.base import is_a_notebook, file_language, safe_read_text
9+
from databricks.labs.ucx.source_code.base import infer_file_language_if_supported, is_a_notebook, safe_read_text
1010
from databricks.labs.ucx.source_code.graph import (
1111
BaseNotebookResolver,
1212
Dependency,
@@ -102,7 +102,7 @@ def load_dependency(self, path_lookup: PathLookup, dependency: Dependency) -> No
102102

103103
@staticmethod
104104
def _detect_language(path: Path, content: str) -> Language | None:
105-
language = file_language(path)
105+
language = infer_file_language_if_supported(path)
106106
if language:
107107
return language
108108
for cell_language in CellLanguage:

0 commit comments

Comments
 (0)