File tree Expand file tree Collapse file tree
dagster_tests/execution_tests/context_tests
dagster/_core/execution/context Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1616)
1717from dagster ._core .errors import DagsterInvariantViolationError
1818from dagster ._core .instance import DagsterInstance
19+ from dagster ._core .definitions .utils import DEFAULT_GROUP_NAME
1920from dagster ._utils .warnings import normalize_renamed_param
2021
2122if 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" :
Original file line number Diff line number Diff line change 2525 time_window_for_partition_key_range ,
2626)
2727from dagster ._core .errors import DagsterInvalidMetadata , DagsterInvariantViolationError
28+ from dagster ._core .definitions .utils import DEFAULT_GROUP_NAME
2829from dagster ._core .execution .context .input import KeyRangeNoPartitionsDefPartitionsSubset
2930from dagster ._core .execution .plan .utils import build_resources_for_manager
3031from 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 :
Original file line number Diff line number Diff 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+
3367def test_build_output_context_with_output_metadata ():
3468 expected_metadata = {"foo" : "bar" }
3569 output_context = dg .build_output_context (output_metadata = expected_metadata )
You can’t perform that action at this time.
0 commit comments