-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make upstream asset materialization events available on the context #18971
Changes from all commits
82c2cc7
40b06c8
d207cc3
47f4487
11ec018
8ced250
9b663de
d572c35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
AssetKey, | ||
AssetMaterialization, | ||
AssetObservation, | ||
CoercibleToAssetKey, | ||
ExpectationResult, | ||
UserEvent, | ||
) | ||
|
@@ -1478,6 +1479,29 @@ def job_def(self) -> JobDefinition: | |
""" | ||
return self.op_execution_context.job_def | ||
|
||
@public | ||
def latest_materialization_for_upstream_asset( | ||
self, key: CoercibleToAssetKey | ||
) -> Optional[AssetMaterialization]: | ||
"""Get the most recent AssetMaterialization event for the key. The key must be an upstream | ||
asset for the currently materializing asset. Information like metadata and tags can be found | ||
on the AssetMaterialization. If the key is not an upstream asset of the currently | ||
materializing asset, an error will be raised. If no AssetMaterialization exists for key, None | ||
will be returned. | ||
|
||
Returns: Optional[AssetMaterialization] | ||
""" | ||
materialization_events = ( | ||
self.op_execution_context._step_execution_context.upstream_asset_materialization_events # noqa: SLF001 | ||
) | ||
if AssetKey.from_coercible(key) in materialization_events.keys(): | ||
return materialization_events.get(AssetKey.from_coercible(key)) | ||
|
||
raise DagsterInvariantViolationError( | ||
f"Cannot fetch AssetMaterialization for asset {key}. {key} must be an upstream dependency" | ||
"in order to call latest_materialization_for_upstream_asset." | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My one concern is naming, in that it will perhaps lure the programmer into thinking that this will fetch the latest materialization event for any asset, rather than only the immeadiate upstream. My recommendation is
and then hard error with a clear error message if it is a key that is not immediately upstream. I think the current behavior in this PR qualifies as "surprising" and will lead to subtle errors. |
||
######## Deprecated methods | ||
|
||
@deprecated(**_get_deprecation_kwargs("dagster_run")) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
DagsterInstance, | ||
Definitions, | ||
GraphDefinition, | ||
MaterializeResult, | ||
OpExecutionContext, | ||
Output, | ||
asset, | ||
|
@@ -426,3 +427,33 @@ def a(context: AssetExecutionContext): | |
assert context == AssetExecutionContext.get() | ||
|
||
assert materialize([a]).success | ||
|
||
|
||
def test_upstream_metadata(): | ||
# with output metadata | ||
@asset | ||
def upstream(context: AssetExecutionContext): | ||
context.add_output_metadata({"foo": "bar"}) | ||
|
||
@asset | ||
def downstream(context: AssetExecutionContext, upstream): | ||
mat = context.latest_materialization_for_upstream_asset("upstream") | ||
assert mat is not None | ||
assert mat.metadata["foo"].value == "bar" | ||
|
||
materialize([upstream, downstream]) | ||
|
||
|
||
def test_upstream_metadata_materialize_result(): | ||
# with asset materialization | ||
@asset | ||
def upstream(): | ||
return MaterializeResult(metadata={"foo": "bar"}) | ||
|
||
@asset | ||
def downstream(context: AssetExecutionContext, upstream): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use deps 🤦 |
||
mat = context.latest_materialization_for_upstream_asset("upstream") | ||
assert mat is not None | ||
assert mat.metadata["foo"].value == "bar" | ||
|
||
materialize([upstream, downstream]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"a direct upstream dependency"