Skip to content
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

provide context class based on type hint #16759

Merged
merged 5 commits into from
Sep 25, 2023

Conversation

jamiedemaria
Copy link
Contributor

@jamiedemaria jamiedemaria commented Sep 25, 2023

Summary & Motivation

Introduces the logic to determine which kind of context to provide to which kind of step.

Follows these rules:

@asset + no type annotation -> AssetExecutionContext
@asset + AssetExecutionContext annotation -> AssetExecutionContext
@asset + OpExecutionContext annotation -> OpExecutionContext 
@op + no type annotation -> OpExecutionContext 
@op + AssetExecutionContext annotation -> Error (see note)
@op + OpExecutionContext -> OpExecutionContext 

note: We error when AssetExecutionContext type hint is provided to @op because we will likely require an AssetsDefinition in the __init__ of AssetExecutionContext once we begin to split methods out (see here). It will be easier to make a change allowing the AssetExecutionContext type annotation than it will be to disallow it, so I think we should error in this case for now

For ops in graph-backed assets, we do:

@graph_asset @op + no type annotation -> OpExecutionContext
@graph_asset @op + AssetExecutionContext -> AssetExecutionContext 
@graph_asset @op + OpExecutionContext -> OpExecutionContext

The reasoning behind @graph_asset @op + no type annotation -> OpExecutionContext is best explained with an example:
Imagine you have an op that is used in both a job and a graph-backed asset

@op 
def my_fun_op(context):
   context.describe_op # this function is on the OpExecutionContext, but not on the AssetExecutionContext

@job 
def my_fun_job():
    my_fun_op()

@graph_asset 
def my_fun_asset():
    my_fun_op()

When executing my_fun_job, my_fun_op will receive an OpExecutionContext and run as expected. When materializing my_fun_asset, my_fun_op will receive an AssetExecutionContext. It will fire a deprecation warning, but still call describe_op on the internally held op_execution_context. When we deprecate the __get_attr__ behavior here, my_fun_op will instead error.

How I Tested These Changes

new unit tests

@jamiedemaria
Copy link
Contributor Author

jamiedemaria commented Sep 25, 2023

@jamiedemaria jamiedemaria changed the base branch from jamie/context-provided-helper-fns to jamie/asset-context-subclass September 25, 2023 15:20
@jamiedemaria jamiedemaria force-pushed the jamie/provide-different-context-type branch from 3011279 to 5450524 Compare September 25, 2023 15:20

# TODO - i dont know how to move this check to Definition time since we don't know if the op is
# part of a graph-backed asset until we have the step execution context, i think
if context_annotation is AssetExecutionContext and not is_sda_step:
Copy link
Contributor Author

@jamiedemaria jamiedemaria Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to move this check to definition time, but i dont know if we can. At definition time, I dont know of a way to know if

@op 
def my_op(context: AssetExecutionContext)

is being used in a job or in a graph-backed asset

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this makes sense

nit: would reframe the comment before land to something like "would be nice to ... currently doesnt seem possible"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok cool, will update

@jamiedemaria jamiedemaria force-pushed the jamie/asset-context-subclass branch from f8b50e1 to c355260 Compare September 25, 2023 16:12
@jamiedemaria jamiedemaria force-pushed the jamie/provide-different-context-type branch from 5450524 to c385c3f Compare September 25, 2023 16:12
@jamiedemaria jamiedemaria force-pushed the jamie/asset-context-subclass branch from c355260 to 4715a30 Compare September 25, 2023 16:14
@jamiedemaria jamiedemaria force-pushed the jamie/provide-different-context-type branch from c385c3f to f45c313 Compare September 25, 2023 16:14
@jamiedemaria jamiedemaria force-pushed the jamie/asset-context-subclass branch from 4715a30 to c925179 Compare September 25, 2023 16:56
@jamiedemaria jamiedemaria force-pushed the jamie/provide-different-context-type branch from f45c313 to 6c91460 Compare September 25, 2023 16:56
@jamiedemaria jamiedemaria marked this pull request as ready for review September 25, 2023 16:59

# TODO - i dont know how to move this check to Definition time since we don't know if the op is
# part of a graph-backed asset until we have the step execution context, i think
if context_annotation is AssetExecutionContext and not is_sda_step:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this makes sense

nit: would reframe the comment before land to something like "would be nice to ... currently doesnt seem possible"

@alangenfeld
Copy link
Member

note I would classify these new errors as breaking changes so, make sure it waits for the 1.5 release and get documented accordingly

@jamiedemaria jamiedemaria force-pushed the jamie/provide-different-context-type branch from 6c91460 to f9601c3 Compare September 25, 2023 18:09
@jamiedemaria
Copy link
Contributor Author

note I would classify these new errors as breaking changes so, make sure it waits for the 1.5 release and get documented accordingly

yes, i agree. I think the last release branch before 1.5 has been cut, so this should be good to go in any time. i'll make a note to give this behavior change a callout in the changelog

@jamiedemaria jamiedemaria force-pushed the jamie/asset-context-subclass branch from b447c3b to c240643 Compare September 25, 2023 18:49
@jamiedemaria jamiedemaria force-pushed the jamie/provide-different-context-type branch 2 times, most recently from e373310 to 75c8b69 Compare September 25, 2023 19:55
Base automatically changed from jamie/asset-context-subclass to master September 25, 2023 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants