Skip to content

Commit 608e206

Browse files
[core] Add asset_group_name to IO contexts
1 parent 6886cff commit 608e206

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

python_modules/dagster/dagster/_core/execution/context/input.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
)
1717
from dagster._core.errors import DagsterInvariantViolationError
1818
from dagster._core.instance import DagsterInstance
19+
from dagster._core.definitions.utils import DEFAULT_GROUP_NAME
1920
from dagster._utils.warnings import normalize_renamed_param
2021

2122
if TYPE_CHECKING:
@@ -206,6 +207,29 @@ def upstream_output(self) -> Optional["OutputContext"]:
206207
"""Info about the output that produced the object we're loading."""
207208
return self._upstream_output
208209

210+
@public
211+
@property
212+
def asset_group_name(self) -> str:
213+
"""The group name of the asset being loaded."""
214+
if self._upstream_output is not None and self._upstream_output._asset_spec is not None:
215+
return self._upstream_output.asset_group_name
216+
217+
if self._asset_key is not None and self._step_context is not None:
218+
return self._step_context.job_def.asset_layer.get(self._asset_key).group_name
219+
220+
if self._upstream_output is not None and self._upstream_output.asset_key is not None:
221+
return DEFAULT_GROUP_NAME
222+
223+
if self._asset_key is not None:
224+
raise DagsterInvariantViolationError(
225+
"Attempting to access asset_group_name, but it was not provided when constructing"
226+
" the InputContext"
227+
)
228+
229+
raise DagsterInvariantViolationError(
230+
"Attempting to access asset_group_name, but input does not correspond to an asset"
231+
)
232+
209233
@public
210234
@property
211235
def dagster_type(self) -> "DagsterType":

python_modules/dagster/dagster/_core/execution/context/output.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
time_window_for_partition_key_range,
2626
)
2727
from dagster._core.errors import DagsterInvalidMetadata, DagsterInvariantViolationError
28+
from dagster._core.definitions.utils import DEFAULT_GROUP_NAME
2829
from dagster._core.execution.context.input import KeyRangeNoPartitionsDefPartitionsSubset
2930
from dagster._core.execution.plan.utils import build_resources_for_manager
3031
from dagster._core.instance import DagsterInstance
@@ -396,6 +397,26 @@ def asset_spec(self) -> AssetSpec:
396397
)
397398
return self._asset_spec
398399

400+
@public
401+
@property
402+
def asset_group_name(self) -> str:
403+
"""The group name of the asset that is being stored as an output."""
404+
if self._asset_spec is not None:
405+
return self._asset_spec.group_name or DEFAULT_GROUP_NAME
406+
407+
if self._asset_key is not None and self._step_context is not None:
408+
return self._step_context.job_def.asset_layer.get(self._asset_key).group_name
409+
410+
if self._asset_key is not None:
411+
raise DagsterInvariantViolationError(
412+
"Attempting to access asset_group_name, but it was not provided when constructing"
413+
" the OutputContext"
414+
)
415+
416+
raise DagsterInvariantViolationError(
417+
"Attempting to access asset_group_name, but output does not correspond to an asset"
418+
)
419+
399420
@property
400421
def step_context(self) -> "StepExecutionContext":
401422
if self._warn_on_step_context_use:

python_modules/dagster/dagster_tests/execution_tests/context_tests/test_output.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,40 @@ def load_input(self, context: InputContext) -> Any:
3030
dg.materialize([asset_spec], resources={"io_manager": TestIOManager()})
3131

3232

33+
def test_build_output_context_asset_group_name():
34+
asset_spec = dg.AssetSpec(key="key", group_name="group")
35+
assert (
36+
dg.build_output_context(asset_key="key", asset_spec=asset_spec).asset_group_name == "group"
37+
)
38+
39+
40+
def test_build_output_context_asset_group_name_defaults():
41+
asset_spec = dg.AssetSpec(key="key")
42+
assert (
43+
dg.build_output_context(asset_key="key", asset_spec=asset_spec).asset_group_name
44+
== "default"
45+
)
46+
47+
48+
def test_build_input_context_asset_group_name_from_upstream_output():
49+
upstream_output = dg.build_output_context(
50+
asset_key="key",
51+
asset_spec=dg.AssetSpec(key="key", group_name="group"),
52+
)
53+
assert (
54+
dg.build_input_context(asset_key="key", upstream_output=upstream_output).asset_group_name
55+
== "group"
56+
)
57+
58+
59+
def test_build_input_context_asset_group_name_defaults_from_upstream_output():
60+
upstream_output = dg.build_output_context(asset_key="key", asset_spec=dg.AssetSpec(key="key"))
61+
assert (
62+
dg.build_input_context(asset_key="key", upstream_output=upstream_output).asset_group_name
63+
== "default"
64+
)
65+
66+
3367
def test_build_output_context_with_output_metadata():
3468
expected_metadata = {"foo": "bar"}
3569
output_context = dg.build_output_context(output_metadata=expected_metadata)

0 commit comments

Comments
 (0)