Skip to content

Pass Dag runs directly to Deadline references - #70714

Open
imrichardwu wants to merge 8 commits into
apache:mainfrom
imrichardwu:fix/deadline-reference-context
Open

Pass Dag runs directly to Deadline references#70714
imrichardwu wants to merge 8 commits into
apache:mainfrom
imrichardwu:fix/deadline-reference-context

Conversation

@imrichardwu

Copy link
Copy Markdown
Contributor

Preserve the Dag run evaluation context for custom references that accept additional keyword arguments, so they continue to evaluate correctly.

Pass the in-memory Dag run to Deadline references so built-in references avoid redundant database reads and custom references can evaluate the current state.

Deprecate required_kwargs while preserving it for existing custom references. Use the shared evaluator when recalculating queued-at deadlines.

closes: #70710


Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@imrichardwu
imrichardwu marked this pull request as draft July 30, 2026 05:29
@imrichardwu
imrichardwu force-pushed the fix/deadline-reference-context branch 2 times, most recently from e9c48fd to 3b99b93 Compare July 30, 2026 05:33
@imrichardwu
imrichardwu marked this pull request as ready for review July 30, 2026 05:33
@imrichardwu

Copy link
Copy Markdown
Contributor Author

@ferruzzi can I get a review?

Preserve the Dag run evaluation context for custom references that accept
additional keyword arguments, so they continue to evaluate correctly.
@imrichardwu
imrichardwu force-pushed the fix/deadline-reference-context branch from 3b99b93 to db15212 Compare July 31, 2026 01:39
Comment thread airflow-core/docs/howto/deadline-alerts.rst Outdated

@abstractmethod
def _evaluate_with(self, *, session: Session, **kwargs: Any) -> datetime | None:
def _evaluate_with(self, *, session: Session, dagrun: Any) -> datetime | None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Rather than using Any, I think a cleaner solution might be to type dagrun as a DagRunProtocol and add queued_at to that protocol.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It doesn't look like this was actually addressed, was it? You would at east need to add queued_at to DagRunProtocol for that to work and it's not showing in the diff.

Comment thread airflow-core/src/airflow/models/deadline.py
Comment thread airflow-core/src/airflow/models/deadline.py Outdated
imrichardwu and others added 6 commits August 6, 2026 20:55
Co-authored-by: D. Ferruzzi <ferruzzi@amazon.com>
Co-authored-by: D. Ferruzzi <ferruzzi@amazon.com>
Co-authored-by: D. Ferruzzi <ferruzzi@amazon.com>
   The custom deadline reference examples returned the dagrun attribute
   directly, which read as if the attribute was the required return value
   rather than an input available to the author's business logic. Type the
   evaluation dagrun with the shared DagRunProtocol so authors get a
   concrete contract for what is available, and show the attribute feeding
   into business logic in the examples.
A custom deadline reference whose _evaluate_with declares **kwargs is
expected to receive the full evaluation context, matching the standard
Python "accept anything" convention rather than having the context
filtered away. Align the test with that behaviour.
@imrichardwu
imrichardwu requested a review from ferruzzi August 7, 2026 16:01
def _evaluate_with(self, *, session: Session, **kwargs) -> datetime:
# Put your business logic here
return some_datetime
# Optionally, you can specify when you want it calculated by providing a DeadlineReference.TYPES

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This comment duplicates the one just above the decorator. The one outside feels like a better place.

_datetime: datetime

def _evaluate_with(self, *, session: Session, **kwargs: Any) -> datetime | None:
def _evaluate_with(self, *, session: Session, dagrun: Any) -> datetime | None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as before; dagrun is never used, so replacing it with **kwargs is a bit cleaner.

and give every field a default value.
* **Plugin Placement**: One convenient place for custom references is in the plugins directory.
* **API Server Restart**: Restart the Airflow API Server after adding or modifying custom references.
* **Required Parameters**: ``required_kwargs`` declares which Dag run context values Airflow should

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since Required Parameters is still available until 4.0, we should keep it here and just add a note that it is deprecated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deadline reference evaluation should receive the DagRun instead of dag_id/run_id

2 participants