diff --git a/python_modules/libraries/dagster-components/dagster_components/lib/definitions_component/component.py b/python_modules/libraries/dagster-components/dagster_components/lib/definitions_component/component.py index 40853348a24c4..a5ba0e3ab841d 100644 --- a/python_modules/libraries/dagster-components/dagster_components/lib/definitions_component/component.py +++ b/python_modules/libraries/dagster-components/dagster_components/lib/definitions_component/component.py @@ -1,10 +1,11 @@ +import glob import importlib from pathlib import Path from typing import Optional from dagster._core.definitions.definitions_class import Definitions from dagster._core.definitions.module_loaders.load_defs_from_module import ( - load_definitions_from_module, + load_definitions_from_modules, ) from dagster._utils import pushd from pydantic import Field @@ -49,12 +50,17 @@ def build_defs(self, context: ComponentLoadContext) -> Definitions: ] component_module_name = ".".join([context.module_name, *component_module_relative_path]) - defs_file_path = ( - Path(self.definitions_path) if self.definitions_path else Path("definitions.py") - ).absolute() - if defs_file_path.name != "__init__.py": - component_module_name = f"{component_module_name}.{defs_file_path.stem}" + defs_file_glob = self.definitions_path if self.definitions_path else "definitions.py" - module = importlib.import_module(component_module_name) + files = glob.glob(defs_file_glob) - return load_definitions_from_module(module) + module_names = [] + for file in files: + module_names.append(f"{component_module_name}.{Path(file).stem}") + + modules = [] + for module_name in module_names: + module = importlib.import_module(module_name) + modules.append(module) + + return load_definitions_from_modules(modules) diff --git a/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/components/definitions/explicit_glob/component.yaml b/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/components/definitions/explicit_glob/component.yaml new file mode 100644 index 0000000000000..ed9b2066d7605 --- /dev/null +++ b/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/components/definitions/explicit_glob/component.yaml @@ -0,0 +1,4 @@ +type: definitions@dagster_components + +params: + definitions_path: "*.py" \ No newline at end of file diff --git a/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/components/definitions/explicit_glob/file_one.py b/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/components/definitions/explicit_glob/file_one.py new file mode 100644 index 0000000000000..840718f97572f --- /dev/null +++ b/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/components/definitions/explicit_glob/file_one.py @@ -0,0 +1,5 @@ +from dagster import asset + + +@asset +def file_one_asset() -> None: ... diff --git a/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/components/definitions/explicit_glob/file_two.py b/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/components/definitions/explicit_glob/file_two.py new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/test_definitions_component.py b/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/test_definitions_component.py index 288f33f1dfe6a..0d4d6072f8824 100644 --- a/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/test_definitions_component.py +++ b/python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/test_definitions_component.py @@ -55,3 +55,10 @@ def test_definitions_component_validation_error() -> None: load_test_component_defs("definitions/validation_error_file") assert "component.yaml:4" in str(e.value) + + +def test_definitions_explicit_glob() -> None: + defs = load_test_component_defs("definitions/explicit_glob") + assert {spec.key for spec in defs.get_all_asset_specs()} == { + AssetKey("file_one_asset"), + }