From 6f91ddae81c8acb9e9552a5e2c37ce9736e25fef Mon Sep 17 00:00:00 2001 From: Gleb Kanterov Date: Thu, 24 Apr 2025 13:25:25 +0200 Subject: [PATCH 1/3] [Python] Fix "Bundle resource explorer" in VSCode --- .../python/databricks/bundles/core/_load.py | 2 +- .../databricks/bundles/core/_location.py | 15 +++++++++++++++ .../python/databricks_tests/core/test_load.py | 2 +- .../databricks_tests/core/test_location.py | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 experimental/python/databricks_tests/core/test_location.py diff --git a/experimental/python/databricks/bundles/core/_load.py b/experimental/python/databricks/bundles/core/_load.py index e643dc4b1b..65d328da4b 100644 --- a/experimental/python/databricks/bundles/core/_load.py +++ b/experimental/python/databricks/bundles/core/_load.py @@ -161,7 +161,7 @@ def _parse_locations(module: ModuleType) -> dict[str, Location]: locations[var_name] = Location( line=stmt.lineno, - column=stmt.col_offset, + column=stmt.col_offset + 1, file=file, ) diff --git a/experimental/python/databricks/bundles/core/_location.py b/experimental/python/databricks/bundles/core/_location.py index 809c3da1d7..7e7d47a652 100644 --- a/experimental/python/databricks/bundles/core/_location.py +++ b/experimental/python/databricks/bundles/core/_location.py @@ -11,8 +11,23 @@ @dataclass(kw_only=True, frozen=True) class Location: file: str + line: Optional[int] = None + """ + Line number in the file. Line numbers are 1-based and should be greater than 0. + """ + column: Optional[int] = None + """ + Column number in the line. Column numbers are 1-based and should be greater than 0. + """ + + def __post_init__(self): + if self.line is not None and self.line < 1: + raise ValueError(f"Line number must be greater than 0, got {self.line}") + + if self.column is not None and self.column < 1: + raise ValueError(f"Column number must be greater than 0, got {self.column}") @staticmethod def from_callable(fn: Callable) -> Optional["Location"]: diff --git a/experimental/python/databricks_tests/core/test_load.py b/experimental/python/databricks_tests/core/test_load.py index 93ff3144fc..c45050a618 100644 --- a/experimental/python/databricks_tests/core/test_load.py +++ b/experimental/python/databricks_tests/core/test_load.py @@ -66,7 +66,7 @@ def test_parse_locations(): assert locations == { "my_job": Location( line=3, - column=0, + column=1, file="databricks_tests/fixtures/dummy_module.py", ), } diff --git a/experimental/python/databricks_tests/core/test_location.py b/experimental/python/databricks_tests/core/test_location.py new file mode 100644 index 0000000000..17598ad4c8 --- /dev/null +++ b/experimental/python/databricks_tests/core/test_location.py @@ -0,0 +1,19 @@ +import pytest + +from databricks.bundles.core._location import Location + + +def test_line_number_positive(): + """ + Line numbers are 1-based and should be greater than 0. + """ + with pytest.raises(ValueError, match="Line number must be greater than 0"): + Location(file="test.py", line=0) + + +def test_column_number_positive(): + """ + Column numbers are 1-based and should be greater than 0. + """ + with pytest.raises(ValueError, match="Column number must be greater than 0"): + Location(file="test.py", line=1, column=0) From f974136e39661c18273f1b252399d596dc850339 Mon Sep 17 00:00:00 2001 From: Gleb Kanterov Date: Thu, 24 Apr 2025 14:59:18 +0200 Subject: [PATCH 2/3] Add comment --- experimental/python/databricks/bundles/core/_load.py | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/python/databricks/bundles/core/_load.py b/experimental/python/databricks/bundles/core/_load.py index 65d328da4b..cf5cb30355 100644 --- a/experimental/python/databricks/bundles/core/_load.py +++ b/experimental/python/databricks/bundles/core/_load.py @@ -161,6 +161,7 @@ def _parse_locations(module: ModuleType) -> dict[str, Location]: locations[var_name] = Location( line=stmt.lineno, + # do conversion: col_offset is 0-based, and column is 1-based column=stmt.col_offset + 1, file=file, ) From f01ef2d8414212f2bcb26fbc075c681c2ebc1c50 Mon Sep 17 00:00:00 2001 From: Gleb Kanterov Date: Thu, 24 Apr 2025 15:03:56 +0200 Subject: [PATCH 3/3] Add changelog --- NEXT_CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index d8a52f1572..b264083f8a 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -9,5 +9,6 @@ ### CLI ### Bundles +* Fixed issue with jobs and pipelines declared in Python not showing in "Bundle resource explorer" in VSCode ([#2764](https://github.com/databricks/cli/pull/2764)) ### API Changes