https://dagster.io/ logo
#ask-community
Title
# ask-community
j

Joseph McCartin

07/24/2023, 10:35 AM
Hi, I would like to override Dagster’s asset decorator so that I can set some defaults. That custom asset decorator would then be shared via a library, so other engineers don’t have to worry about various IO manager keys and so on. I’m struggling to get this to work, however.
In the very simplest case, I would like to have the following:
Copy code
from dagster import asset

# Custom libraries
import dataset1
from utilities import generate_name


def custom_asset(
    data_format: str,
    ins: Optional[Mapping[str, AssetIn]] = None,
):

    @asset(
        name=generate_name(data_format),
        ins=ins
    )
    def asset_override(func, **kwargs):
        return func(**kwargs)

    return asset_override


@custom_asset(
    ins={"dataset1": AssetIn(dataset1.asset_key)}
)
def generate_data(dataset1):
    ...
But when I run this, I get errors about
generate_data
missing a positional argument. Is this the best way to go about overriding a decorator? I’d rather not have to copy/paste and then modify the original asset decorator definition for obvious reasons.
Looks like I figured it out. Will post the solution here so that a search might aid others… Decorates can be a little confusing, but what I needed was an inner function. So modifying the example I had above, this worked:
Copy code
from dagster import asset

# Custom libraries
import dataset1
from utilities import generate_name


def custom_asset(
    data_format: str,
    ins: Optional[Mapping[str, AssetIn]] = None,
):

    def inner(func):
        return asset(
            name=generate_name(data_format),
            ins=ins
        )(func)

    return inner


@custom_asset(
    ins={"dataset1": AssetIn(dataset1.asset_key)}
)
def generate_data(dataset1):
    ...
s

sandy

07/24/2023, 4:24 PM
Hey @Joseph McCartin. Glad you were able to figure this out, and thanks for posting your solution. If you feel motivated to post it as a Github discussion, that would give it even wider visibility.
2 Views