uranusjr edited a comment on issue #8171:
URL: https://github.com/apache/airflow/issues/8171#issuecomment-806169069
I wonder if type checks can be used to catch these kinds of issues. There
are a lot of `current_app.dag_bag`, `current_app.appbuilder`, etc., which are
not covered by Mypy due to the dynamic nature of `flask.current_app` (basically
any attributes on it is `Any`). Since we “know” what attributes to expect on
the `current_app` instance within Airflow, maybe we can introduce a typing shim
around it? Something like
```python
# airflow/www/app.py
if TYPE_CHECKING:
from airflow.models.dagbag import DagBag
from airflow.www.security import AirflowSecurityManager
class _AirflowAppBuilder(Protocol):
sm: AirflowSecurityManager
...
class _CurrentApp(Protocol):
dag_bag: DagBag
appbuilder: _AirflowAppBuilder
...
from flask import current_app as _current_app
current_app = cast("_CurrentApp", _current_app)
```
And then all code can import this instead of directly from Flask to be type
checked.
We can alternatively supply a [type
stub](https://mypy.readthedocs.io/en/stable/stubs.html#creating-a-stub)
`flask.pyi` to “lie to” Mypy what type `flask.current_app` is `_CurrentApp`.
--
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.
For queries about this service, please contact Infrastructure at:
[email protected]