Skip to content

refactor: extract _add_test_tasks to reduce build_airflow_graph complexity#2734

Closed
Aaditya-git wants to merge 2 commits into
astronomer:mainfrom
Aaditya-git:refactor/build-airflow-graph-complexity
Closed

refactor: extract _add_test_tasks to reduce build_airflow_graph complexity#2734
Aaditya-git wants to merge 2 commits into
astronomer:mainfrom
Aaditya-git:refactor/build-airflow-graph-complexity

Conversation

@Aaditya-git
Copy link
Copy Markdown

Summary

Closes #1943

build_airflow_graph had a cyclomatic complexity of 11, exceeding the ruff C901 limit of 10 and requiring a # noqa: C901 suppression comment.

This PR extracts the test-behavior branching block into a dedicated _add_test_tasks helper function, reducing build_airflow_graph's complexity from 11 to 9 and removing the suppression comment.

Changes

  • cosmos/airflow/graph.py: new _add_test_tasks function handles both AFTER_ALL and BUILD/AFTER_EACH test behavior cases. build_airflow_graph now calls it in one line instead of ~40 lines of inline branching. The # noqa: C901 comment is removed.

No behavior changes

This is a pure refactor. All existing logic is preserved verbatim inside _add_test_tasks, the only difference is where the code lives. All existing tests for build_airflow_graph continue to cover the same behavior paths.

…exity

Extracts the test-behavior branching block from build_airflow_graph into
a dedicated _add_test_tasks helper, reducing cyclomatic complexity from
11 to 9 and removing the noqa: C901 suppression.

Closes astronomer#1943
@pankajkoti
Copy link
Copy Markdown
Contributor

Thanks for tackling #1943, @Aaditya-git, and apologies for the duplicated effort here.

This overlaps almost entirely with #2689, which was opened earlier (May 16) and is solving the same issue the same way: extracting the test-task branches into a new _add_test_tasks helper and removing the # noqa: C901. That PR has already been through a review round and, importantly, has been rebased on top of #2615 (Watcher depends on past), so its helper carries the tasks_map[f"{dbt_project_name}_test"] = test_task line that #2615 added. This branch predates that change, so as-is it would drop that line and regress the watcher's wait_for_downstream propagation.

To avoid two contributors maintaining the same refactor, I'm going to close this in favor of #2689. This isn't a reflection on the quality of your change, the approach is sound. Looking forward to your continued contributions to the project.

@pankajkoti pankajkoti closed this Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor function build_airflow_graph to reduce code complexity

2 participants