Skip to content

Commit

Permalink
Implement AutomationCondition.replace() function (#27109)
Browse files Browse the repository at this point in the history
## Summary & Motivation

Users often want to make small changes to the existing policies. There
is an existing utility to remove a sub-condition (`.without(…)`), and
you can add a new sub-condition with `&`.

However, these utilities aren't always the most ergonomic for certain
use cases.

This would allow the following types of changes to our examples:

[Older time window
partitions](https://docs.dagster.io/concepts/automation/declarative-automation/customizing-automation-conditions#update-an-older-time-partition-when-using-automationconditionon_cron)

before:

```python
condition = five_days_ago_condition & AutomationCondition.eager().without(
    AutomationCondition.in_latest_time_window(),
)
```

after:

```python
AutomationCondition.eager().replace(
    "in_latest_time_window", five_days_ago_condition
)
```

## How I Tested These Changes

Added unit tests.

## Changelog

Implemented `AutomationCondition.replace()` function to improve
ergonomics of updating sub-conditions.
  • Loading branch information
deepyaman authored and prha committed Jan 16, 2025
1 parent 558acb1 commit 9fa0bba
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ def replace(
else copy(self, operands=[child.replace(old, new) for child in self.operands])
)

@public
def replace(
self, old: Union[AutomationCondition, str], new: AutomationCondition
) -> AutomationCondition:
"""Replaces all instances of ``old`` across any sub-conditions with ``new``.
If ``old`` is a string, then conditions with a label matching
that string will be replaced.
Args:
old (Union[AutomationCondition, str]): The condition to replace.
new (AutomationCondition): The condition to replace with.
"""
return (
new
if old in [self, self.get_label()]
else copy(self, operands=[child.replace(old, new) for child in self.operands])
)


@whitelist_for_serdes(storage_name="OrAssetCondition")
@record
Expand Down

0 comments on commit 9fa0bba

Please sign in to comment.