Aaron T
05/19/2023, 5:51 PMZach
05/19/2023, 6:02 PMis_required=False
or make it `Noneable`:
from dagster import Field, Noneable, op, OpExecutionContext
@op(
config_schema={"optional_field": Field(str, is_required=False,
"noneable_field": Field(Noneable(str), is_required=True, default_value=None)}
)
def something(context: OpExecutionContext):
context.op_config["optional_field"] # will throw KeyError if optional_field not included in run config
assert context.op_config["noneable_field"] == None
Aaron T
05/19/2023, 6:03 PMNoneable
jamie
05/19/2023, 6:09 PMfrom pydantic import Field
from dagster import Config
from typing import Optional
class MyConfig(Config):
optional_field: Optional[str] = Field(default=None)
note that here Field
is imported from pydantic
not dagster
Aaron T
05/19/2023, 6:10 PMjamie
05/19/2023, 6:11 PMconfig_schema
dictionaries are still fully supported, so if that works better for your use case feel free to use it!jamie
05/19/2023, 6:13 PMfrom pydantic import Field
from dagster import Config, asset
from typing import Optional
class MyConfig(Config):
optional_field: Optional[str] = Field(default=None)
@asset
def my_asset(config: MyConfig):
if config.optional_field is not None:
...
Aaron T
05/19/2023, 6:14 PMjamie
05/19/2023, 6:57 PMconfig
and have the right type annotation. You can use the context too!
@asset
def my_asset(context, config: MyConfig):
if config.optional_field is not None:
...
would be totally fine
I don’t think the order matters (worth verifying with a small sample asset though)jamie
05/19/2023, 6:57 PMAaron T
05/19/2023, 6:58 PMAaron T
05/23/2023, 6:54 PMjamie
05/23/2023, 6:59 PMfrom pydantic import Field
from dagster import Config, job, op, RunConfig
from typing import Optional
class MyConfig(Config):
optional_field: Optional[str] = Field(default=None)
@op
def my_op(config: MyConfig):
if config.optional_field is not None:
...
@job(
config=RunConfig(ops={"op_name": MyConfig(optional_field="foo")})
)
def my_job():
my_op()
Aaron T
05/23/2023, 7:03 PMjamie
05/23/2023, 7:04 PMAaron T
05/23/2023, 7:05 PMAaron T
05/23/2023, 7:25 PMasset_1() missing 1 required positional argument: 'config'
. I am using the materialize()
function in my opjamie
05/24/2023, 2:16 PMmaterialize
should generally just be used for testing assets (ie in unit tests or integration tests) and not called from within ops. What is the larger goal of materializing the asset within the op? there is likely another way to accomplish the goalAaron T
05/24/2023, 3:36 PMrun_config
that will make some api calls and get the partition key, and then materialize the asset with that partition_keyjamie
05/24/2023, 3:40 PMAaron T
05/24/2023, 3:53 PMAaron T
05/24/2023, 4:02 PMmaterialize
instead of someone manually using the UI to materialize a partition. It just creates an ephemeral run, not sure of the implications that has, but I think an additional thread is held because the Op that calls materialize stays as running while the asset is materializing. That is the only real downside. I could change it to async, but maybe for one-off uses this is okAaron T
05/24/2023, 4:04 PM