Dev-iL opened a new pull request, #65586:
URL: https://github.com/apache/airflow/pull/65586
related: #64430
## Summary
- Add `dag[task_id]` subscript syntax to `DAG` and `SerializedDAG`,
mirroring the existing `TaskGroup.__getitem__` pattern.
- Update `TaskGroup.__getitem__` to raise `TaskItemNotFound` (instead of a
raw `KeyError`) on a miss, so callers can catch either `KeyError` or
`TaskNotFound`.
- Introduce `TaskItemNotFound(TaskNotFound, KeyError)` as a new exception in
`airflow.sdk.exceptions` (and re-export it from `airflow.exceptions`) — a
`KeyError` subtype that also satisfies `TaskNotFound`, giving callers
flexibility without breaking existing `KeyError` handlers.
## Details
| Component | Change |
|-----------|--------|
| `task-sdk/src/airflow/sdk/exceptions.py` | Add
`TaskItemNotFound(TaskNotFound, KeyError)` |
| `airflow-core/src/airflow/exceptions.py` | Re-export `TaskItemNotFound`;
add fallback stub |
| `task-sdk/src/airflow/sdk/definitions/dag.py` | Add `DAG.__getitem__`
(delegates to `get_task`, re-raises as `TaskItemNotFound`) |
| `task-sdk/src/airflow/sdk/definitions/taskgroup.py` | Update
`TaskGroup.__getitem__` to re-raise `KeyError` as `TaskItemNotFound` |
| `airflow-core/src/airflow/serialization/definitions/dag.py` | Add
`SerializedDAG.__getitem__` for consistency |
## Usage
```python
with DAG("my_dag", ...) as dag:
t = BashOperator(task_id="my_task", bash_command="echo 1")
# New: subscript access
assert dag["my_task"] is t
# Raises TaskItemNotFound (catchable as KeyError or TaskNotFound)
dag["nonexistent"] # raises TaskItemNotFound
# TaskGroup subscript now raises TaskItemNotFound instead of raw KeyError
with TaskGroup("section") as tg:
...
tg["nonexistent"] # raises TaskItemNotFound
```
---
##### 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 (please specify the tool below)
Generated-by: Claude Sonnet 4.6 following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
---
* 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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]