asset()

Turn the decorated function into an asset that validates the frame it returns against schema.

Usage

Source

asset(
    schema,
    /,
    *,
    filter=False,
    error_rows_key=None,
    error_rows_io_manager_key=None,
    check_granularity=None,
    max_failure_examples=None,
    statistics=None,
    row_sample=None,
    name=None,
    key_prefix=None,
    ins=None,
    deps=None,
    metadata=None,
    tags=None,
    description=None,
    config_schema=None,
    required_resource_keys=None,
    resource_defs=None,
    hooks=None,
    io_manager_key=None,
    partitions_def=None,
    op_tags=None,
    group_name=None,
    automation_condition=None,
    freshness_policy=None,
    backfill_policy=None,
    retry_policy=None,
    code_version=None,
    owners=None,
    kinds=None,
    pool=None
)

The decorated function returns a pl.DataFrame or pl.LazyFrame, a dg.MaterializeResult whose value is one of those, or None to skip. The asset takes a context parameter whether or not the decorated function declares one, so a direct invocation passes dg.build_asset_context() first.

Every keyword parameter not listed below is @dg.asset’s, passed to it unchanged.

Parameters

schema: type[dy.Schema]

The schema the returned frame is validated against. It also fills the Columns tab, declares one check spec per rule set and sets the code version. A dy.Collection raises.

filter: bool = False

Whether a run writes the other rows when some rows fail an "error" rule. With True, the run writes the rows that failed an "error" rule, the error rows, under the error-rows key, and no rule’s check is blocking. A run fails with ErrorRowsKeyCollisionError if another asset already materializes that key. With False, a row that fails an "error" rule fails the run, and the asset writes nothing, including the error rows (docs/out-of-scope/dbt-test-configs.md#store_failures-on-a-test-with-severity-error).

error_rows_key: dg.AssetKey | str | Sequence[str] | None = None

The error-rows key, which needs filter=True. None uses <name>__error_rows with the asset’s key prefix. A string is one key part, as in dg.AssetKey.

error_rows_io_manager_key: str | None = None

The resource key of the IO manager that writes the error rows in a run, which needs filter=True. None uses the asset’s own IO manager.

check_granularity: Granularity | None = None

How many asset checks report the schema’s rules: one per rule at rule, and at column one per column with rules plus one per schema-level rule. Changing it on an asset that has already run starts a new check history. None uses DAGSTER_DATAFRAMELY_CHECK_GRANULARITY if set, else rule.

max_failure_examples: int | None = None

A rule’s check metadata has at most this many rows that failed it, under dataframely/failed_examples. A run writes the rows to the Dagster event log unredacted. dy.Config.set_max_failure_examples does not change this number. None uses DAGSTER_DATAFRAMELY_MAX_FAILURE_EXAMPLES if set, else 5.

statistics: bool | None = None

Whether the materialization metadata has statistics of the written rows, one table per dtype group. None uses DAGSTER_DATAFRAMELY_STATISTICS if set, else True.

row_sample: int | None = None

The materialization metadata has at most this many rows of the table, and this many error rows. A run writes the rows to the Dagster event log unredacted. None uses DAGSTER_DATAFRAMELY_ROW_SAMPLE if set, else 5.

key_prefix: str | Sequence[str] | None = None

The default error-rows key has the same prefix.

metadata: Mapping[str, Any] | None = None

Definition metadata. The schema’s dagster/column_schema entry replaces a key of the same name.

description: str | None = None

None uses the schema’s docstring, or the decorated function’s docstring if the schema has none.

required_resource_keys: AbstractSet[str] | None = None

With error_rows_io_manager_key, the asset also requires that key.

io_manager_key: str | None = None

In a run, build_io_manager_writer passes the error rows to the same IO manager, unless error_rows_io_manager_key names another.

partitions_def: dg.PartitionsDefinition[str] | None = None

The writer writes the error rows under the same partition key, and every check spec has the same partitions.

code_version: str | None = None

None derives one from the schema, with dd.wiring.get_code_version.

kinds: AbstractSet[str] | None = None
The asset has the polars kind as well as these, because Polars always computes it.

Returns

Callable[[DecoratedFn], dg.AssetsDefinition]
A decorator that returns a dg.AssetsDefinition with the schema’s check specs and a Columns tab filled from the schema.

Raises

CollectionNotSupportedError

schema is a dy.Collection.

dg.DagsterInvalidDefinitionError

error_rows_key or error_rows_io_manager_key is set and filter is False.

ReservedColumnError

A column name is in the reserved dy_ namespace.

InvalidColumnNameError

A column name has a character Dagster does not allow in an asset check name.

CheckNameCollisionError

Two rules produce the same asset check name.

InvalidSeverityError

The schema declares a severity this package cannot read.

InvalidSettingError

A setting’s argument or environment variable has a value the setting does not allow.

TypeError
The decorated callable has no __name__, such as a functools.partial, and name is not set.

Examples

import polars as pl

import dagster_dataframely as dd
from dagster_dataframely.data import OrdersSchema


@dd.asset(OrdersSchema, filter=True)
def orders(raw_orders: pl.DataFrame) -> pl.DataFrame:
    return raw_orders


[spec.name for spec in orders.check_specs][:4]
['dy_schema__columns',
 'dy_rule__paid_orders_have_amount',
 'dy_rule__line_numbers_are_dense',
 'dy_rule__primary_key']

The check specs exist before the asset first runs, so the catalog lists every check before its first result.