You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "kaxil (via GitHub)" <gi...@apache.org> on 2025/05/08 20:45:56 UTC
[PR] Replace DagBag from global app state to with FastAPI dependency [airflow]
kaxil opened a new pull request, #50372:
URL: https://github.com/apache/airflow/pull/50372
This change replaces the usage of `app.state.dag_bag` with a proper FastAPI dependency for better dependency injection and testability. The DagBag is now provided through a dependency that can be easily mocked in tests and follows FastAPI best practices as mentioned [here](https://fastapi.tiangolo.com/reference/fastapi/#fastapi.FastAPI.state:~:text=You%20normally%20wouldn%27t%20use%20this%20in%20FastAPI%2C%20for%20most%20of%20the%20cases%20you%20would%20instead%20use%20FastAPI%20dependencies.).
- Explicit Dependencies: Using `DagBagDep` makes it clear in the function signature what each endpoint needs, rather than hiding the dependency in app.state.
- Better Testing: Dependencies can be easily mocked (via FastAPI's `dependency_overrides`) in tests without modifying app state, making unit testing simpler and more reliable.
- Request Isolation: Each request gets its own `DagBag` instance through the dependency, preventing any potential state leakage between requests.
- Type Safety: The dependency system provides better type checking and IDE support compared to accessing `app.state`.
<!--
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.
-->
<!--
Thank you for contributing! 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 committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
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] Replace DagBag from global app state to with FastAPI dependency [airflow]
Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #50372:
URL: https://github.com/apache/airflow/pull/50372#discussion_r2083142999
##########
airflow-core/src/airflow/api_fastapi/common/deps.py:
##########
@@ -14,16 +14,22 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
+
from __future__ import annotations
import os
+from typing import Annotated
+
+from fastapi import Depends
-from airflow.models import DagBag
+from airflow.models.dagbag import DagBag
from airflow.settings import DAGS_FOLDER
-def get_dag_bag() -> DagBag:
- """Instantiate the appropriate DagBag based on the ``SKIP_DAGS_PARSING`` environment variable."""
+def _get_dag_bag() -> DagBag:
if os.environ.get("SKIP_DAGS_PARSING") == "True":
return DagBag(os.devnull, include_examples=False)
return DagBag(DAGS_FOLDER, read_dags_from_db=True)
+
+
+DagBagDep = Annotated[DagBag, Depends(_get_dag_bag)]
Review Comment:
It's okay making this a dep, but given DagBag acts as a cache for DAGs, I think we still want to have the "backing store" for this dependency be `app.state` so that there is a single shared cached instance, rather than each and every request creating a new dag bag when it needs it
--
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] Replace DagBag from global app state to with FastAPI dependency [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil merged PR #50372:
URL: https://github.com/apache/airflow/pull/50372
--
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] Replace DagBag from global app state to with FastAPI dependency [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on PR #50372:
URL: https://github.com/apache/airflow/pull/50372#issuecomment-2864712449
> Love it. Handling this with annotated depends is a great idea and follows the best practices. Not loading the request each time we need DagBag is already great for security measures and prevents leakage, as mentioned. Only CI and tests need some care
CI is green now 🎉
--
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] Replace DagBag from global app state to with FastAPI dependency [airflow]
Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 commented on PR #50372:
URL: https://github.com/apache/airflow/pull/50372#issuecomment-2864942522
**Follow up:**
For the Auth Manager, should we also consider replacing the use of the global app state with FastAPI `Depends`?
Currently, `init_auth_manager` just sets the Auth Manager on the global app state.
--
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] Replace DagBag from global app state to with FastAPI dependency [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on PR #50372:
URL: https://github.com/apache/airflow/pull/50372#issuecomment-2867214685
> **Follow up:**
>
> For the Auth Manager, should we also consider replacing the use of the global app state with FastAPI `Depends`? Currently, `init_auth_manager` just sets the Auth Manager on the global app state.
Yeah, go for it
--
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] Replace DagBag from global app state to with FastAPI dependency [airflow]
Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #50372:
URL: https://github.com/apache/airflow/pull/50372#discussion_r2083358740
##########
airflow-core/src/airflow/api_fastapi/common/deps.py:
##########
@@ -14,16 +14,22 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
+
from __future__ import annotations
import os
+from typing import Annotated
+
+from fastapi import Depends
-from airflow.models import DagBag
+from airflow.models.dagbag import DagBag
from airflow.settings import DAGS_FOLDER
-def get_dag_bag() -> DagBag:
- """Instantiate the appropriate DagBag based on the ``SKIP_DAGS_PARSING`` environment variable."""
+def _get_dag_bag() -> DagBag:
if os.environ.get("SKIP_DAGS_PARSING") == "True":
return DagBag(os.devnull, include_examples=False)
return DagBag(DAGS_FOLDER, read_dags_from_db=True)
+
+
+DagBagDep = Annotated[DagBag, Depends(_get_dag_bag)]
Review Comment:
Agreed, addressed in https://github.com/apache/airflow/pull/50447
--
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