-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[components] Add custom scope for custom components (#26594)
## Summary & Motivation Small change, big usability upgrade. This lets users define custom scope for users of their component to take advantage of. This scope can be anything from convenience string constants to complex functions that return raw python types. In the unit test, I have an example showing how this could let someone set up a custom automation condition constructor, but the possibilities here are kinda endless. For example, you could easily imagine creating more complex translator methods without having to fully give up the yaml world using this capability. ## How I Tested These Changes ## Changelog NOCHANGELOG
- Loading branch information
1 parent
234e10c
commit 09333d9
Showing
8 changed files
with
95 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
42 changes: 42 additions & 0 deletions
42
...r-components/dagster_components_tests/rendering_tests/custom_scope_component/component.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Any, Mapping | ||
|
||
from dagster import AssetSpec, AutomationCondition, Definitions | ||
from dagster_components import Component, ComponentLoadContext, component | ||
from pydantic import BaseModel | ||
|
||
|
||
def my_custom_fn(a: str, b: str) -> str: | ||
return a + "|" + b | ||
|
||
|
||
def my_custom_automation_condition(cron_schedule: str) -> AutomationCondition: | ||
return AutomationCondition.cron_tick_passed(cron_schedule) & ~AutomationCondition.in_progress() | ||
|
||
|
||
class CustomScopeParams(BaseModel): | ||
attributes: Mapping[str, Any] | ||
|
||
|
||
@component(name="custom_scope_component") | ||
class HasCustomScope(Component): | ||
params_schema = CustomScopeParams | ||
|
||
@classmethod | ||
def get_rendering_scope(cls) -> Mapping[str, Any]: | ||
return { | ||
"custom_str": "xyz", | ||
"custom_dict": {"a": "b"}, | ||
"custom_fn": my_custom_fn, | ||
"custom_automation_condition": my_custom_automation_condition, | ||
} | ||
|
||
def __init__(self, attributes: Mapping[str, Any]): | ||
self.attributes = attributes | ||
|
||
@classmethod | ||
def load(cls, context: ComponentLoadContext): | ||
loaded_params = context.load_params(cls.params_schema) | ||
return cls(attributes=loaded_params.attributes) | ||
|
||
def build_defs(self, context: ComponentLoadContext): | ||
return Definitions(assets=[AssetSpec(key="key", **self.attributes)]) |
9 changes: 9 additions & 0 deletions
9
...components/dagster_components_tests/rendering_tests/custom_scope_component/component.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type: .custom_scope_component | ||
|
||
params: | ||
attributes: | ||
group_name: "{{ custom_str }}" | ||
tags: "{{ custom_dict }}" | ||
metadata: | ||
prefixed: "prefixed_{{ custom_fn('a', custom_str) }}" | ||
automation_condition: "{{ custom_automation_condition('@daily') }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ibraries/dagster-components/dagster_components_tests/rendering_tests/test_custom_scope.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from pathlib import Path | ||
|
||
from dagster import AssetSpec, AutomationCondition | ||
from dagster_components.core.component_defs_builder import build_defs_from_component_path | ||
|
||
from dagster_components_tests.utils import registry | ||
|
||
|
||
def test_custom_scope() -> None: | ||
defs = build_defs_from_component_path( | ||
path=Path(__file__).parent / "custom_scope_component", | ||
registry=registry(), | ||
resources={}, | ||
) | ||
|
||
assets = list(defs.assets or []) | ||
assert len(assets) == 1 | ||
spec = assets[0] | ||
assert isinstance(spec, AssetSpec) | ||
|
||
assert spec.group_name == "xyz" | ||
assert spec.tags == {"a": "b"} | ||
assert spec.metadata == {"prefixed": "prefixed_a|xyz"} | ||
assert ( | ||
spec.automation_condition | ||
== AutomationCondition.cron_tick_passed("@daily") & ~AutomationCondition.in_progress() | ||
) |