You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Eason09053360 (via GitHub)" <gi...@apache.org> on 2026/09/09 13:57:01 UTC
[PR] Fix DAG.cli() crashing on dags test --show-dagrun [airflow]
Eason09053360 opened a new pull request, #72810:
URL: https://github.com/apache/airflow/pull/72810
Running a Dag file as a script (`python my_dag.py dags test --show-dagrun`) goes through `DAG.cli()`. Its Dag-scoped parser drops the `dag_id` positional and passes the Dag object to the handler instead, so the parsed `Namespace` has no `dag_id` attribute at all. `dag_test` never needed it until it rendered the run: the task instance query read `args.dag_id`, so `--show-dagrun`, `--save-dagrun` and `--imgcat-dagrun` all raised `AttributeError: 'Namespace' object has no attribute 'dag_id'` after the Dag had already run, while the same command without a rendering flag succeeded. The regular `airflow dags test <dag_id> --show-dagrun` path was never affected.
### Fix
The query now filters on the resolved Dag's id. On the regular path that is the same string as `args.dag_id` (the Dag was looked up by it), and on the `DAG.cli()` path it is the only id available. That lookup was the single reader of `args.dag_id` after the Dag is resolved, because `dag or get_bagged_dag(...)` short-circuits when a Dag is passed in.
### Testing
- New `test_dag_test_show_dag_from_dag_cli` drives `dag_test` the way `DAG.cli()` does (Dag-scoped parser, Dag passed positionally), seeds a Dag run with one task instance via `dag_maker`, and asserts that instance reaches `render_dag`. Without the fix it fails on the same `AttributeError`; with it the whole file passes (107 tests).
- `test_dag_test_show_dag` now gives its mocked Dag a real `dag_id`, since a `MagicMock` cannot be bound as a SQL parameter.
- Reproduced end to end on a sqlite metadata DB with a one-task Dag in the dags folder: before the fix the `DAG.cli()` invocation exited 1 with the traceback above; after it prints the same DOT graph as `airflow dags test <dag_id> --show-dagrun`.
Note for reproduction: the Dag file has to live inside a configured bundle and be serialized first (`airflow dags reserialize`), otherwise `dag.test()` fails earlier on both paths with "Cannot create DagRun ... because the dag is not serialized".
Related: #72109 fixed the same `DAG.cli()` dispatch shape for `dags pause` and `dags unpause`.
A pre-existing, separate limitation surfaced during review and is out of scope here: `@action_cli` snapshots `dag_id` from the `Namespace` before the handler runs, so audit `Log` rows for any `DAG.cli()` subcommand carry `dag_id=NULL`. The natural place to fix that is `DAG.cli()` itself.
---
##### Was generative AI tooling used to co-author this PR?
- [X] Yes — Claude Code (Fable 5.1)
Generated-by: Claude Code (Fable 5.1) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
--
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] Fix DAG.cli() crashing on dags test --show-dagrun [airflow]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #72810:
URL: https://github.com/apache/airflow/pull/72810#issuecomment-5685865975
Hi maintainer, this PR was merged without a milestone set.
We've automatically set the milestone to **[Airflow 3.3.2](https://github.com/apache/airflow/milestone/150)** based on: backport label targeting v3-3-test
If this milestone is not correct, please update it to the appropriate milestone.
> This comment was generated by [Milestone Tag Assistant](https://github.com/apache/airflow/blob/main/.github/workflows/milestone-tag-assistant.yml).
--
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] Fix DAG.cli() crashing on dags test --show-dagrun [airflow]
Posted by "Eason09053360 (via GitHub)" <gi...@apache.org>.
Eason09053360 commented on code in PR #72810:
URL: https://github.com/apache/airflow/pull/72810#discussion_r3980088176
##########
airflow-core/tests/unit/cli/commands/test_dag_command.py:
##########
@@ -969,6 +970,23 @@ def test_dag_test_show_dag(self, mock_get_dag, mock_render_dag, stdout_capture):
mock_render_dag.assert_has_calls([mock.call(mock_get_dag.return_value, tis=[])])
assert "SOURCE" in output
+ @mock.patch("airflow.cli.commands.dag_command.render_dag", autospec=True)
+ @mock.patch.object(DAG, "test", autospec=True)
+ def test_dag_test_show_dag_from_dag_cli(self, mock_test, mock_render_dag, dag_maker):
+ """``DAG.cli()`` passes the Dag positionally and its parser drops ``dag_id``."""
+ with dag_maker("dag_cli_show_dagrun", schedule=None) as dag:
+ EmptyOperator(task_id="only_task")
+ mock_test.return_value = dag_maker.create_dagrun(run_id="dag_cli_run")
+
+ parser = cli_parser.get_parser(dag_parser=True)
+ dag_command.dag_test(parser.parse_args(["dags", "test", "--show-dagrun"]), dag)
Review Comment:
Applied :)
--
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] Fix DAG.cli() crashing on dags test --show-dagrun [airflow]
Posted by "henry3260 (via GitHub)" <gi...@apache.org>.
henry3260 commented on code in PR #72810:
URL: https://github.com/apache/airflow/pull/72810#discussion_r4018505350
##########
airflow-core/tests/unit/cli/commands/test_dag_command.py:
##########
@@ -945,6 +945,7 @@ def test_dag_test_conf(self, mock_get_dag):
@mock.patch("airflow.cli.commands.dag_command.get_bagged_dag")
Review Comment:
```suggestion
@mock.patch("airflow.cli.commands.dag_command.get_bagged_dag", autospec=True)
```
--
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] Fix DAG.cli() crashing on dags test --show-dagrun [airflow]
Posted by "rjgoyln (via GitHub)" <gi...@apache.org>.
rjgoyln commented on code in PR #72810:
URL: https://github.com/apache/airflow/pull/72810#discussion_r3970722268
##########
airflow-core/tests/unit/cli/commands/test_dag_command.py:
##########
@@ -969,6 +970,23 @@ def test_dag_test_show_dag(self, mock_get_dag, mock_render_dag, stdout_capture):
mock_render_dag.assert_has_calls([mock.call(mock_get_dag.return_value, tis=[])])
assert "SOURCE" in output
+ @mock.patch("airflow.cli.commands.dag_command.render_dag", autospec=True)
+ @mock.patch.object(DAG, "test", autospec=True)
+ def test_dag_test_show_dag_from_dag_cli(self, mock_test, mock_render_dag, dag_maker):
+ """``DAG.cli()`` passes the Dag positionally and its parser drops ``dag_id``."""
+ with dag_maker("dag_cli_show_dagrun", schedule=None) as dag:
+ EmptyOperator(task_id="only_task")
+ mock_test.return_value = dag_maker.create_dagrun(run_id="dag_cli_run")
+
+ parser = cli_parser.get_parser(dag_parser=True)
+ dag_command.dag_test(parser.parse_args(["dags", "test", "--show-dagrun"]), dag)
Review Comment:
With `render_dag` autospecced, `dot_graph.source` is a mock attribute, so the run prints `<MagicMock name='render_dag().source' id=...>` to stdout — visible under `-s`. `test_dag_test_show_dag` just above takes `stdout_capture` for exactly that, and line 310 in this file already uses the bare form.
```suggestion
def test_dag_test_show_dag_from_dag_cli(self, mock_test, mock_render_dag, dag_maker, stdout_capture):
"""``DAG.cli()`` passes the Dag positionally and its parser drops ``dag_id``."""
with dag_maker("dag_cli_show_dagrun", schedule=None) as dag:
EmptyOperator(task_id="only_task")
mock_test.return_value = dag_maker.create_dagrun(run_id="dag_cli_run")
parser = cli_parser.get_parser(dag_parser=True)
with stdout_capture:
dag_command.dag_test(parser.parse_args(["dags", "test", "--show-dagrun"]), dag)
```
--
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] Fix DAG.cli() crashing on dags test --show-dagrun [airflow]
Posted by "henry3260 (via GitHub)" <gi...@apache.org>.
henry3260 merged PR #72810:
URL: https://github.com/apache/airflow/pull/72810
--
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] Fix DAG.cli() crashing on dags test --show-dagrun [airflow]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #72810:
URL: https://github.com/apache/airflow/pull/72810#issuecomment-5685874404
### Backport successfully created: v3-3-test
Note: As of [Merging PRs targeted for Airflow 3.X](https://github.com/apache/airflow/blob/main/dev/README_AIRFLOW3_DEV.md#merging-prs-targeted-for-airflow-3x)
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.
In matter of doubt please ask in [#release-management](https://apache-airflow.slack.com/archives/C03G9H97MM2) Slack channel.
<table>
<tr>
<th>Status</th>
<th>Branch</th>
<th>Result</th>
</tr>
<tr>
<td>✅</td>
<td>v3-3-test</td>
<td><a href="https://github.com/apache/airflow/pull/73216"><img src="https://img.shields.io/badge/PR-73216-blue" alt="PR Link"></a></td>
</tr>
</table>
--
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