[Python] Fix issues with multiple resource loaders (#2511)

## Changes
Fix issues with multiple resource loaders. 

Previously, we discarded outputs of all loaders except the last one.
That worked well if there is only a single resource loader.

## Tests
- Unit tests
- Acceptance tests in https://github.com/databricks/cli/pull/2493
This commit is contained in:
Gleb Kanterov 2025-03-18 12:56:36 +01:00 committed by GitHub
parent d52a06f864
commit d2ec690585
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 2 deletions

View File

@ -241,9 +241,11 @@ def _load_resources(
for function in functions:
try:
resources, diagnostics = diagnostics.extend_tuple(
function_resources, diagnostics = diagnostics.extend_tuple(
_load_resources_from_function(bundle, function)
)
resources.add_resources(function_resources)
except Exception as exc:
diagnostics = diagnostics.extend(
Diagnostics.from_exception(
@ -342,7 +344,7 @@ def _explain_common_import_error(exc: Exception) -> Diagnostics:
# a common case when default name of the module is not found
# we can give a hint to the user how to fix it
explanation = """Make sure to create a new Python file at resources/__init__.py with contents:
from databricks.bundles.core import load_resources_from_current_package_module

View File

@ -7,6 +7,7 @@ from databricks.bundles.build import (
_Args,
_Conf,
_load_object,
_load_resources,
_parse_args,
_parse_bundle_info,
_relativize_location,
@ -296,3 +297,51 @@ def test_conf_from_dict():
],
venv_path="venv",
)
def test_load_resources():
bundle = Bundle(target="default")
def load_resources_1() -> Resources:
resources = Resources()
resources.add_job(
resource_name="my_job_1",
job={"name": "Job 1"},
location=Location(file="my_job_1.py", line=42, column=1),
)
return resources
def load_resources_2() -> Resources:
resources = Resources()
resources.add_job(
resource_name="my_job_2",
job={"name": "Job 2"},
location=Location(file="my_job_2.py", line=42, column=1),
)
return resources
resources, diagnostics = _load_resources(
bundle=bundle,
functions=[
load_resources_1,
load_resources_2,
],
)
assert not diagnostics.has_error()
assert resources.jobs == {
"my_job_1": Job(name="Job 1"),
"my_job_2": Job(name="Job 2"),
}
assert resources._locations == {
("resources", "jobs", "my_job_1"): Location(
file="my_job_1.py", line=42, column=1
),
("resources", "jobs", "my_job_2"): Location(
file="my_job_2.py", line=42, column=1
),
}