You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "amoghrajesh (via GitHub)" <gi...@apache.org> on 2026/06/01 12:38:45 UTC
[PR] Add default parameter to task and asset state get() accessors [airflow]
amoghrajesh opened a new pull request, #67842:
URL: https://github.com/apache/airflow/pull/67842
<!-- SPDX-License-Identifier: Apache-2.0
https://www.apache.org/licenses/LICENSE-2.0 -->
<!--
Thank you for contributing!
Please provide above a brief description of the changes made in this pull request.
Write a good git commit message following this guide: http://chris.beams.io/posts/git-commit/
Please make sure that your code changes are covered with tests.
And in case of new features or big changes remember to adjust the documentation.
Feel free to ping (in general) for the review if you do not see reaction for a few days
(72 Hours is the minimum reaction time you can expect from volunteers) - we sometimes miss notifications.
In case of an existing issue, reference it using one of the following:
* closes: #ISSUE
* related: #ISSUE
-->
---
##### Was generative AI tooling used to co-author this PR?
<!--
If generative AI tooling has been used in the process of authoring this PR, please
change below checkbox to `[X]` followed by the name of the tool, uncomment the "Generated-by".
-->
- [x] Yes: claude sonnet 4.6
<!--
Generated-by: [Tool Name] following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
-->
closes: https://github.com/apache/airflow/issues/67841
### What?
`TaskStateAccessor.get()` and `AssetStateAccessor.get()` returned `None` when a key did not exist, which made callers to use None checks or `or` based fallbacks like `state.get("watermark") or "2026-01-01"`. This is not the best authoring experience and does not follow the standard Python `dict.get(key, default)` pattern.
### Current behaviour
`state.get("watermark")` returns None if the key has never been set. Callers must write `state.get("watermark") or "fallback"`, which is both verbose for no benefit.
### Proposed change
- Added a default parameter to `get()` on `TaskStateAccessor` and `AssetStateAccessor`, and the `AssetStateAccessors` shorthand accessor. The default is `None`. Example usage:
```python
watermark = state.get("watermark", default="2026-01-01T00:00:00+00:00")
total_runs = state.get("total_runs", default=0)
```
- set(key, None) now raises ValueError — `None` is not a valid state value
The example dag `example_asset_state.py` is updated to use the new form.
---
* Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#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](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment: `{pr_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments). You can add this file in a follow-up commit after the PR is created so you know the PR number.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add default parameter to task and asset state get() accessors [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #67842:
URL: https://github.com/apache/airflow/pull/67842#discussion_r3336353344
##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -550,6 +552,9 @@ def set(self, key: str, value: JsonValue, *, retention: timedelta | None = None)
from airflow.sdk.execution_time.comms import SetTaskState
from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
+ if value is None:
+ raise ValueError("Cannot set value as None")
Review Comment:
Rejecting `None` here changes behavior for the one in-tree caller, `ResumableMixin`. At [`resumablemixin.py:127-130`](https://github.com/apache/airflow/blob/cb02ce2ed7f0178dcf23965aabaaa0c3a0d86e0b/task-sdk/src/airflow/sdk/bases/resumablemixin.py#L127-L130) it does `external_id = self.submit_job(context)` then `task_state.set(self.external_id_key, external_id)`. `submit_job` is typed `-> JsonValue`, so a subclass can legitimately return `None`. Previously a `None` id was stored silently and the next run hit `if external_id:` as falsy and resubmitted, so the task still completed. Now it raises mid-execution.
Was that break intended? If not, options are to skip the write when the value is `None`, guard the call in the mixin (the existing `if task_state is not None:` guard at line 129 only covers `task_state`, not `external_id`), or at minimum document that `submit_job` must never return `None`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add default parameter to task and asset state get() accessors [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #67842:
URL: https://github.com/apache/airflow/pull/67842#issuecomment-4608836424
The failures are unrelated, merging
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add default parameter to task and asset state get() accessors [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #67842:
URL: https://github.com/apache/airflow/pull/67842#discussion_r3338719398
##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -550,6 +552,9 @@ def set(self, key: str, value: JsonValue, *, retention: timedelta | None = None)
from airflow.sdk.execution_time.comms import SetTaskState
from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
+ if value is None:
+ raise ValueError("Cannot set value as None")
Review Comment:
Good catch. The break was not intended.
Fixed it by adding None check on external_id. A None return is now treated as "no ID available" and skips the write, preserving the older behaviour. Also added a test for this, and updated the submit_job docstring to document that None means the ID won't be persisted.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add default parameter to task and asset state get() accessors [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #67842:
URL: https://github.com/apache/airflow/pull/67842#discussion_r3338725433
##########
task-sdk/src/airflow/sdk/execution_time/context.py:
##########
@@ -550,6 +552,9 @@ def set(self, key: str, value: JsonValue, *, retention: timedelta | None = None)
from airflow.sdk.execution_time.comms import SetTaskState
from airflow.sdk.execution_time.task_runner import SUPERVISOR_COMMS
+ if value is None:
+ raise ValueError("Cannot set value as None")
Review Comment:
Handled in 41b8749f20
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add default parameter to task and asset state get() accessors [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh merged PR #67842:
URL: https://github.com/apache/airflow/pull/67842
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org