Skip to content

Commit 0d96ec8

Browse files
committed
[components] Explicit glob in definitions
1 parent 47b6f0a commit 0d96ec8

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

Diff for: python_modules/libraries/dagster-components/dagster_components/lib/definitions_component/component.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import glob
12
import importlib
23
from pathlib import Path
34
from typing import Optional
45

56
from dagster._core.definitions.definitions_class import Definitions
67
from dagster._core.definitions.module_loaders.load_defs_from_module import (
7-
load_definitions_from_module,
8+
load_definitions_from_modules,
89
)
910
from dagster._utils import pushd
1011
from pydantic import Field
@@ -49,12 +50,17 @@ def build_defs(self, context: ComponentLoadContext) -> Definitions:
4950
]
5051
component_module_name = ".".join([context.module_name, *component_module_relative_path])
5152

52-
defs_file_path = (
53-
Path(self.definitions_path) if self.definitions_path else Path("definitions.py")
54-
).absolute()
55-
if defs_file_path.name != "__init__.py":
56-
component_module_name = f"{component_module_name}.{defs_file_path.stem}"
53+
defs_file_glob = self.definitions_path if self.definitions_path else "definitions.py"
5754

58-
module = importlib.import_module(component_module_name)
55+
files = glob.glob(defs_file_glob)
5956

60-
return load_definitions_from_module(module)
57+
module_names = []
58+
for file in files:
59+
module_names.append(f"{component_module_name}.{Path(file).stem}")
60+
61+
modules = []
62+
for module_name in module_names:
63+
module = importlib.import_module(module_name)
64+
modules.append(module)
65+
66+
return load_definitions_from_modules(modules)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type: definitions@dagster_components
2+
3+
params:
4+
definitions_path: "*.py"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from dagster import asset
2+
3+
4+
@asset
5+
def file_one_asset() -> None: ...

Diff for: python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/components/definitions/explicit_glob/file_two.py

Whitespace-only changes.

Diff for: python_modules/libraries/dagster-components/dagster_components_tests/integration_tests/test_definitions_component.py

+7
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ def test_definitions_component_validation_error() -> None:
5555
load_test_component_defs("definitions/validation_error_file")
5656

5757
assert "component.yaml:4" in str(e.value)
58+
59+
60+
def test_definitions_explicit_glob() -> None:
61+
defs = load_test_component_defs("definitions/explicit_glob")
62+
assert {spec.key for spec in defs.get_all_asset_specs()} == {
63+
AssetKey("file_one_asset"),
64+
}

0 commit comments

Comments
 (0)