-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Add meaningful DAG-level documentation to example DAGs #60149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kalel-commits
wants to merge
8
commits into
apache:main
Choose a base branch
from
kalel-commits:add-doc-md-to-example-dags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+411
−295
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
06d7f88
Add doc_md documentation to python decorator example DAG
kalel-commits b2fed57
Add meaningful DAG-level documentation to example DAGs
kalel-commits d4f67b9
Improve TaskFlow example DAG documentation and fix formatting
kalel-commits 4ce430b
Fix doc_md formatting issues flagged by static checks
kalel-commits a5fb0f7
Fix trailing whitespace causing static check failure
kalel-commits ba8efec
Apply prek auto-fixes for static checks
kalel-commits 80504e5
Fix broken documentation links in example DAG doc_md
kalel-commits e46c52d
Merge branch 'main' into add-doc-md-to-example-dags
kalel-commits File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
329 changes: 187 additions & 142 deletions
329
...standard/src/airflow/providers/standard/example_dags/example_branch_operator_decorator.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,142 +1,187 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Example DAG demonstrating the usage of the branching TaskFlow API decorators. | ||
|
|
||
| It shows how to use standard Python ``@task.branch`` as well as the external Python | ||
| version ``@task.branch_external_python`` which calls an external Python interpreter and | ||
| the ``@task.branch_virtualenv`` which builds a temporary Python virtual environment. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import random | ||
| import sys | ||
| import tempfile | ||
|
|
||
| import pendulum | ||
|
|
||
| from airflow.providers.common.compat.sdk import TriggerRule | ||
| from airflow.providers.standard.operators.empty import EmptyOperator | ||
| from airflow.sdk import DAG, Label, task | ||
|
|
||
| PATH_TO_PYTHON_BINARY = sys.executable | ||
|
|
||
| with DAG( | ||
| dag_id="example_branch_python_operator_decorator", | ||
| start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), | ||
| catchup=False, | ||
| schedule="@daily", | ||
| tags=["example", "example2"], | ||
| ) as dag: | ||
| run_this_first = EmptyOperator(task_id="run_this_first") | ||
|
|
||
| options = ["a", "b", "c", "d"] | ||
|
|
||
| # Example branching on standard Python tasks | ||
|
|
||
| # [START howto_operator_branch_python] | ||
| @task.branch() | ||
| def branching(choices: list[str]) -> str: | ||
| return f"branch_{random.choice(choices)}" | ||
|
|
||
| # [END howto_operator_branch_python] | ||
|
|
||
| random_choice_instance = branching(choices=options) | ||
|
|
||
| run_this_first >> random_choice_instance | ||
|
|
||
| join = EmptyOperator(task_id="join", trigger_rule=TriggerRule.NONE_FAILED_MIN_ONE_SUCCESS) | ||
|
|
||
| for option in options: | ||
|
|
||
| @task(task_id=f"branch_{option}") | ||
| def some_task(): | ||
| print("doing something in Python") | ||
|
|
||
| t = some_task() | ||
| empty = EmptyOperator(task_id=f"follow_{option}") | ||
|
|
||
| # Label is optional here, but it can help identify more complex branches | ||
| random_choice_instance >> Label(option) >> t >> empty >> join | ||
|
|
||
| # Example the same with external Python calls | ||
|
|
||
| # [START howto_operator_branch_ext_py] | ||
| @task.branch_external_python(python=PATH_TO_PYTHON_BINARY) | ||
| def branching_ext_python(choices) -> str: | ||
| import random | ||
|
|
||
| return f"ext_py_{random.choice(choices)}" | ||
|
|
||
| # [END howto_operator_branch_ext_py] | ||
|
|
||
| random_choice_ext_py = branching_ext_python(choices=options) | ||
|
|
||
| join >> random_choice_ext_py | ||
|
|
||
| join_ext_py = EmptyOperator(task_id="join_ext_py", trigger_rule=TriggerRule.NONE_FAILED_MIN_ONE_SUCCESS) | ||
|
|
||
| for option in options: | ||
|
|
||
| @task.external_python(task_id=f"ext_py_{option}", python=PATH_TO_PYTHON_BINARY) | ||
| def some_ext_py_task(): | ||
| print("doing something in external Python") | ||
|
|
||
| t = some_ext_py_task() | ||
|
|
||
| # Label is optional here, but it can help identify more complex branches | ||
| random_choice_ext_py >> Label(option) >> t >> join_ext_py | ||
|
|
||
| # Example the same with Python virtual environments | ||
|
|
||
| # [START howto_operator_branch_virtualenv] | ||
| # Note: Passing a caching dir allows to keep the virtual environment over multiple runs | ||
| # Run the example a second time and see that it reuses it and is faster. | ||
| VENV_CACHE_PATH = tempfile.gettempdir() | ||
|
|
||
| @task.branch_virtualenv(requirements=["numpy~=1.26.0"], venv_cache_path=VENV_CACHE_PATH) | ||
| def branching_virtualenv(choices) -> str: | ||
| import random | ||
|
|
||
| import numpy as np | ||
|
|
||
| print(f"Some numpy stuff: {np.arange(6)}") | ||
| return f"venv_{random.choice(choices)}" | ||
|
|
||
| # [END howto_operator_branch_virtualenv] | ||
|
|
||
| random_choice_venv = branching_virtualenv(choices=options) | ||
|
|
||
| join_ext_py >> random_choice_venv | ||
|
|
||
| join_venv = EmptyOperator(task_id="join_venv", trigger_rule=TriggerRule.NONE_FAILED_MIN_ONE_SUCCESS) | ||
|
|
||
| for option in options: | ||
|
|
||
| @task.virtualenv( | ||
| task_id=f"venv_{option}", requirements=["numpy~=1.26.0"], venv_cache_path=VENV_CACHE_PATH | ||
| ) | ||
| def some_venv_task(): | ||
| import numpy as np | ||
|
|
||
| print(f"Some numpy stuff: {np.arange(6)}") | ||
|
|
||
| t = some_venv_task() | ||
|
|
||
| # Label is optional here, but it can help identify more complex branches | ||
| random_choice_venv >> Label(option) >> t >> join_venv | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ | ||
| Example DAG demonstrating the usage of branching TaskFlow API decorators. | ||
|
|
||
| This example shows how to use standard Python `@task.branch`, as well as | ||
| `@task.branch_external_python` and `@task.branch_virtualenv` for branching | ||
| logic executed in external Python interpreters or isolated virtual | ||
| environments. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import random | ||
| import sys | ||
| import tempfile | ||
|
|
||
| import pendulum | ||
|
|
||
| from airflow.providers.common.compat.sdk import TriggerRule | ||
| from airflow.providers.standard.operators.empty import EmptyOperator | ||
| from airflow.sdk import DAG, Label, task | ||
|
|
||
| PATH_TO_PYTHON_BINARY = sys.executable | ||
|
|
||
|
|
||
| with DAG( | ||
| dag_id="example_branch_python_operator_decorator", | ||
| start_date=pendulum.datetime(2021, 1, 1, tz="UTC"), | ||
| catchup=False, | ||
| schedule="@daily", | ||
| tags=["example", "example2"], | ||
| doc_md=""" | ||
| ### Branch Decorator: Runtime Path Selection | ||
|
|
||
| Branching enables conditional execution paths within a DAG by selecting | ||
| which downstream task(s) should run at runtime, while all other paths | ||
| are marked as skipped. This allows mutually exclusive workflows to be | ||
| expressed cleanly within a single DAG definition. | ||
|
|
||
| **How branching selects execution paths:** | ||
| - A branch task returns the `task_id` (or list of `task_id`s) corresponding | ||
| to the next task(s) that should execute | ||
| - Only the returned downstream task(s) are executed; all other immediate | ||
| downstream tasks are marked as skipped | ||
| - Skipped branches do not fail the DAG run and are treated as a normal | ||
| execution outcome | ||
|
|
||
| **Handling skipped branches downstream:** | ||
| - Tasks that follow a branching point must use trigger rules that account | ||
| for skipped upstream tasks (for example, `NONE_FAILED_MIN_ONE_SUCCESS`) | ||
| - Without appropriate trigger rules, downstream tasks may not execute | ||
| as expected due to skipped upstream states | ||
| - This behavior differs from short-circuiting, where all downstream | ||
| execution may be prevented entirely | ||
|
|
||
| **Common use cases:** | ||
| - Conditional data processing based on runtime characteristics | ||
| (for example, small vs. large datasets) | ||
| - Environment-driven workflows where different paths are selected | ||
| dynamically | ||
| - Optional enrichment or validation steps that should only run when needed | ||
| - Mutually exclusive downstream actions within a single DAG | ||
|
|
||
| **Branching vs. Python if/else:** | ||
| Branching is not equivalent to a Python `if/else` statement. All possible | ||
| branches exist in the DAG graph at parse time, and the branch task selects | ||
| which path is taken during execution. | ||
|
|
||
| 📖 **Related documentation** | ||
| https://airflow.apache.org/docs/apache-airflow/stable/core-concepts/dags.html#branching | ||
| """, | ||
| ) as dag: | ||
| run_this_first = EmptyOperator(task_id="run_this_first") | ||
|
|
||
| options = ["a", "b", "c", "d"] | ||
|
|
||
| # Example branching with standard Python tasks | ||
|
|
||
| # [START howto_operator_branch_python] | ||
| @task.branch() | ||
| def branching(choices: list[str]) -> str: | ||
| return f"branch_{random.choice(choices)}" | ||
|
|
||
| # [END howto_operator_branch_python] | ||
|
|
||
| random_choice_instance = branching(choices=options) | ||
|
|
||
| run_this_first >> random_choice_instance | ||
|
|
||
| join = EmptyOperator(task_id="join", trigger_rule=TriggerRule.NONE_FAILED_MIN_ONE_SUCCESS) | ||
|
|
||
| for option in options: | ||
|
|
||
| @task(task_id=f"branch_{option}") | ||
| def some_task(): | ||
| print("doing something in Python") | ||
|
|
||
| t = some_task() | ||
| empty = EmptyOperator(task_id=f"follow_{option}") | ||
|
|
||
| # Label is optional here, but it can help identify more complex branches | ||
| random_choice_instance >> Label(option) >> t >> empty >> join | ||
|
|
||
| # Example branching with external Python execution | ||
|
|
||
| # [START howto_operator_branch_ext_py] | ||
| @task.branch_external_python(python=PATH_TO_PYTHON_BINARY) | ||
| def branching_ext_python(choices) -> str: | ||
| import random | ||
|
|
||
| return f"ext_py_{random.choice(choices)}" | ||
|
|
||
| # [END howto_operator_branch_ext_py] | ||
|
|
||
| random_choice_ext_py = branching_ext_python(choices=options) | ||
|
|
||
| join >> random_choice_ext_py | ||
|
|
||
| join_ext_py = EmptyOperator(task_id="join_ext_py", trigger_rule=TriggerRule.NONE_FAILED_MIN_ONE_SUCCESS) | ||
|
|
||
| for option in options: | ||
|
|
||
| @task.external_python(task_id=f"ext_py_{option}", python=PATH_TO_PYTHON_BINARY) | ||
| def some_ext_py_task(): | ||
| print("doing something in external Python") | ||
|
|
||
| t = some_ext_py_task() | ||
|
|
||
| # Label is optional here, but it can help identify more complex branches | ||
| random_choice_ext_py >> Label(option) >> t >> join_ext_py | ||
|
|
||
| # Example branching with Python virtual environments | ||
|
|
||
| # [START howto_operator_branch_virtualenv] | ||
| # Passing a cache directory allows the virtual environment to be reused | ||
| # across runs, reducing setup overhead on subsequent executions. | ||
| VENV_CACHE_PATH = tempfile.gettempdir() | ||
|
|
||
| @task.branch_virtualenv(requirements=["numpy~=1.26.0"], venv_cache_path=VENV_CACHE_PATH) | ||
| def branching_virtualenv(choices) -> str: | ||
| import random | ||
|
|
||
| import numpy as np | ||
|
|
||
| print(f"Some numpy stuff: {np.arange(6)}") | ||
| return f"venv_{random.choice(choices)}" | ||
|
|
||
| # [END howto_operator_branch_virtualenv] | ||
|
|
||
| random_choice_venv = branching_virtualenv(choices=options) | ||
|
|
||
| join_ext_py >> random_choice_venv | ||
|
|
||
| join_venv = EmptyOperator(task_id="join_venv", trigger_rule=TriggerRule.NONE_FAILED_MIN_ONE_SUCCESS) | ||
|
|
||
| for option in options: | ||
|
|
||
| @task.virtualenv( | ||
| task_id=f"venv_{option}", | ||
| requirements=["numpy~=1.26.0"], | ||
| venv_cache_path=VENV_CACHE_PATH, | ||
| ) | ||
| def some_venv_task(): | ||
| import numpy as np | ||
|
|
||
| print(f"Some numpy stuff: {np.arange(6)}") | ||
|
|
||
| t = some_venv_task() | ||
|
|
||
| # Label is optional here, but it can help identify more complex branches | ||
| random_choice_venv >> Label(option) >> t >> join_venv |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure... how did you make this? The diff view in my browser shows a full diff like all deleted and re-created. Can you please rebase to have a diff only?