This is an automated email from the ASF dual-hosted git repository.
husseinawala pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 2f844c7646 Add TCH001 and TCH002 rules to pre-commit to detect and
move type checking modules (#33865)
2f844c7646 is described below
commit 2f844c76464c675e0fc431ca8cf70e32ec4d7e52
Author: Hussein Awala <[email protected]>
AuthorDate: Thu Aug 31 10:23:59 2023 +0200
Add TCH001 and TCH002 rules to pre-commit to detect and move type checking
modules (#33865)
* Activate ruff TCH001 and TCH002 rules to move typing module under
TYPE_CHECKING condition
* Fix static checks
* address comments from code review
* Two spaces before comment
---------
Co-authored-by: Tzu-ping Chung <[email protected]>
---
dev/assign_cherry_picked_prs_with_milestone.py | 8 +++++---
dev/breeze/src/airflow_breeze/commands/ci_image_commands.py | 6 ++++--
dev/breeze/src/airflow_breeze/commands/main_command.py | 7 ++++---
dev/breeze/src/airflow_breeze/utils/ci_group.py | 5 ++++-
dev/breeze/src/airflow_breeze/utils/docker_command_utils.py | 9 ++++++---
dev/breeze/src/airflow_breeze/utils/image.py | 8 +++++---
dev/breeze/src/airflow_breeze/utils/mark_image_as_refreshed.py | 6 +++++-
dev/breeze/src/airflow_breeze/utils/recording.py | 5 ++++-
dev/stats/calculate_statistics_provider_testing_issues.py | 5 ++++-
dev/stats/get_important_pr_candidates.py | 5 ++++-
docs/exts/airflow_intersphinx.py | 6 ++++--
docs/exts/operators_and_hooks_ref.py | 6 ++++--
docs/exts/provider_init_hack.py | 4 +++-
docs/exts/providers_packages_ref.py | 6 +++++-
docs/exts/substitution_extensions.py | 8 +++++---
pyproject.toml | 6 +++++-
tests/api_connexion/endpoints/test_dag_source_endpoint.py | 5 ++++-
tests/api_internal/endpoints/test_rpc_api_endpoint.py | 6 ++++--
tests/api_internal/test_internal_api_call.py | 5 ++++-
tests/auth/managers/fab/cli_commands/test_role_command.py | 5 ++++-
tests/cli/commands/test_task_command.py | 5 ++++-
tests/cluster_policies/__init__.py | 6 ++++--
tests/dags/test_on_kill.py | 5 ++++-
tests/dags/test_parsing_context.py | 5 ++++-
tests/decorators/test_python.py | 5 +++--
tests/models/test_baseoperator.py | 6 ++++--
tests/models/test_dagrun.py | 6 ++++--
tests/models/test_mappedoperator.py | 5 ++++-
tests/models/test_xcom.py | 5 ++++-
tests/notifications/test_basenotifier.py | 5 ++++-
tests/sensors/test_base.py | 5 ++++-
tests/sensors/test_timeout_sensor.py | 5 ++++-
tests/serialization/test_dag_serialization.py | 5 ++++-
tests/system/providers/amazon/aws/example_glue.py | 5 ++++-
tests/system/providers/amazon/aws/utils/__init__.py | 5 ++++-
tests/system/providers/amazon/aws/utils/k8s.py | 6 +++++-
.../google/cloud/cloud_sql/example_cloud_sql_query_mysql.py | 5 ++++-
.../google/cloud/cloud_sql/example_cloud_sql_query_postgres.py | 5 ++++-
tests/system/utils/__init__.py | 6 ++++--
tests/test_utils/mock_operators.py | 9 ++++++---
tests/test_utils/remote_user_api_auth_backend.py | 6 ++++--
tests/ti_deps/deps/test_trigger_rule_dep.py | 6 ++++--
tests/utils/log/test_log_reader.py | 5 ++++-
tests/utils/test_helpers.py | 7 +++++--
tests/www/views/test_views_grid.py | 6 +++++-
45 files changed, 191 insertions(+), 69 deletions(-)
diff --git a/dev/assign_cherry_picked_prs_with_milestone.py
b/dev/assign_cherry_picked_prs_with_milestone.py
index 8a98e0b4ee..c00e5d0373 100755
--- a/dev/assign_cherry_picked_prs_with_milestone.py
+++ b/dev/assign_cherry_picked_prs_with_milestone.py
@@ -24,16 +24,18 @@ import subprocess
import sys
import textwrap
from pathlib import Path
-from typing import Any, NamedTuple, cast
+from typing import TYPE_CHECKING, Any, NamedTuple, cast
import rich_click as click
from github import Github, UnknownObjectException
-from github.Milestone import Milestone
from github.PullRequest import PullRequest
-from github.Repository import Repository
from rich.console import Console
from rich.prompt import Confirm, Prompt
+if TYPE_CHECKING:
+ from github.Milestone import Milestone
+ from github.Repository import Repository
+
CHANGELOG_SKIP_LABEL = "changelog:skip"
TYPE_DOC_ONLY_LABEL = "type:doc-only"
diff --git a/dev/breeze/src/airflow_breeze/commands/ci_image_commands.py
b/dev/breeze/src/airflow_breeze/commands/ci_image_commands.py
index 9d343ed571..f5ab0b2d0f 100644
--- a/dev/breeze/src/airflow_breeze/commands/ci_image_commands.py
+++ b/dev/breeze/src/airflow_breeze/commands/ci_image_commands.py
@@ -24,12 +24,11 @@ import sys
import time
from functools import partial
from pathlib import Path
-from typing import Any, Callable
+from typing import TYPE_CHECKING, Any, Callable
import click
from airflow_breeze.params.build_ci_params import BuildCiParams
-from airflow_breeze.params.shell_params import ShellParams
from airflow_breeze.utils.ci_group import ci_group
from airflow_breeze.utils.click_utils import BreezeGroup
from airflow_breeze.utils.common_options import (
@@ -107,6 +106,9 @@ from airflow_breeze.utils.run_utils import (
)
from airflow_breeze.utils.shared_options import get_dry_run, get_verbose
+if TYPE_CHECKING:
+ from airflow_breeze.params.shell_params import ShellParams
+
@click.group(
cls=BreezeGroup, name="ci-image", help="Tools that developers can use to
manually manage CI images"
diff --git a/dev/breeze/src/airflow_breeze/commands/main_command.py
b/dev/breeze/src/airflow_breeze/commands/main_command.py
index cf29cb3450..8b1a115a23 100644
--- a/dev/breeze/src/airflow_breeze/commands/main_command.py
+++ b/dev/breeze/src/airflow_breeze/commands/main_command.py
@@ -20,9 +20,7 @@ import platform
import shutil
import subprocess
import sys
-from typing import Any
-
-from click import Context
+from typing import TYPE_CHECKING, Any
from airflow_breeze.commands.ci_image_commands import ci_image
from airflow_breeze.commands.production_image_commands import prod_image
@@ -52,6 +50,9 @@ from airflow_breeze.utils.path_utils import BUILD_CACHE_DIR
from airflow_breeze.utils.run_utils import run_command
from airflow_breeze.utils.shared_options import get_dry_run
+if TYPE_CHECKING:
+ from click import Context
+
def print_deprecated(deprecated_command: str, command_to_use: str):
get_console().print("[yellow]" + ("#" * 80) + "\n")
diff --git a/dev/breeze/src/airflow_breeze/utils/ci_group.py
b/dev/breeze/src/airflow_breeze/utils/ci_group.py
index 13a1110cb0..a3e68075b6 100644
--- a/dev/breeze/src/airflow_breeze/utils/ci_group.py
+++ b/dev/breeze/src/airflow_breeze/utils/ci_group.py
@@ -18,11 +18,14 @@ from __future__ import annotations
import os
from contextlib import contextmanager
+from typing import TYPE_CHECKING
from airflow_breeze.utils.console import MessageType, get_console
-from airflow_breeze.utils.parallel import Output
from airflow_breeze.utils.path_utils import skip_group_output
+if TYPE_CHECKING:
+ from airflow_breeze.utils.parallel import Output
+
# only allow top-level group
_in_ci_group = False
diff --git a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
index 37a2ba3f8a..5216b08f6d 100644
--- a/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
+++ b/dev/breeze/src/airflow_breeze/utils/docker_command_utils.py
@@ -23,11 +23,9 @@ import sys
from copy import deepcopy
from random import randint
from subprocess import DEVNULL, CalledProcessError, CompletedProcess
+from typing import TYPE_CHECKING
-from airflow_breeze.params.build_ci_params import BuildCiParams
from airflow_breeze.params.build_prod_params import BuildProdParams
-from airflow_breeze.params.common_build_params import CommonBuildParams
-from airflow_breeze.params.shell_params import ShellParams
from airflow_breeze.utils.host_info_utils import get_host_group_id,
get_host_os, get_host_user_id
from airflow_breeze.utils.image import find_available_ci_image
from airflow_breeze.utils.path_utils import AIRFLOW_SOURCES_ROOT
@@ -65,6 +63,11 @@ from airflow_breeze.utils.run_utils import (
run_command,
)
+if TYPE_CHECKING:
+ from airflow_breeze.params.build_ci_params import BuildCiParams
+ from airflow_breeze.params.common_build_params import CommonBuildParams
+ from airflow_breeze.params.shell_params import ShellParams
+
# Those are volumes that are mounted when MOUNT_SELECTED is chosen (which is
the default when
# entering Breeze. MOUNT_SELECTED prevents to mount the files that you can
have accidentally added
# in your sources (or they were added automatically by setup.py etc.) to be
mounted to container.
diff --git a/dev/breeze/src/airflow_breeze/utils/image.py
b/dev/breeze/src/airflow_breeze/utils/image.py
index d008e08c7f..04a0d368d4 100644
--- a/dev/breeze/src/airflow_breeze/utils/image.py
+++ b/dev/breeze/src/airflow_breeze/utils/image.py
@@ -18,7 +18,7 @@ from __future__ import annotations
import subprocess
import time
-from typing import Callable
+from typing import TYPE_CHECKING, Callable
from airflow_breeze.global_constants import (
ALLOWED_PYTHON_MAJOR_MINOR_VERSIONS,
@@ -26,8 +26,6 @@ from airflow_breeze.global_constants import (
MOUNT_ALL,
)
from airflow_breeze.params.build_ci_params import BuildCiParams
-from airflow_breeze.params.build_prod_params import BuildProdParams
-from airflow_breeze.params.common_build_params import CommonBuildParams
from airflow_breeze.params.shell_params import ShellParams
from airflow_breeze.utils.ci_group import ci_group
from airflow_breeze.utils.console import Output, get_console
@@ -42,6 +40,10 @@ from airflow_breeze.utils.run_tests import verify_an_image
from airflow_breeze.utils.run_utils import RunCommandResult, run_command
from airflow_breeze.utils.shared_options import get_dry_run, get_verbose
+if TYPE_CHECKING:
+ from airflow_breeze.params.build_prod_params import BuildProdParams
+ from airflow_breeze.params.common_build_params import CommonBuildParams
+
def run_pull_in_parallel(
parallelism: int,
diff --git a/dev/breeze/src/airflow_breeze/utils/mark_image_as_refreshed.py
b/dev/breeze/src/airflow_breeze/utils/mark_image_as_refreshed.py
index 9e3de033db..38a2916fd9 100644
--- a/dev/breeze/src/airflow_breeze/utils/mark_image_as_refreshed.py
+++ b/dev/breeze/src/airflow_breeze/utils/mark_image_as_refreshed.py
@@ -16,11 +16,15 @@
# under the License.
from __future__ import annotations
-from airflow_breeze.params.build_ci_params import BuildCiParams
+from typing import TYPE_CHECKING
+
from airflow_breeze.utils.cache import touch_cache_file
from airflow_breeze.utils.md5_build_check import
calculate_md5_checksum_for_files
from airflow_breeze.utils.path_utils import BUILD_CACHE_DIR
+if TYPE_CHECKING:
+ from airflow_breeze.params.build_ci_params import BuildCiParams
+
def mark_image_as_refreshed(ci_image_params: BuildCiParams):
ci_image_cache_dir = BUILD_CACHE_DIR / ci_image_params.airflow_branch
diff --git a/dev/breeze/src/airflow_breeze/utils/recording.py
b/dev/breeze/src/airflow_breeze/utils/recording.py
index 7125b3d725..aa2afbe25c 100644
--- a/dev/breeze/src/airflow_breeze/utils/recording.py
+++ b/dev/breeze/src/airflow_breeze/utils/recording.py
@@ -19,12 +19,15 @@ from __future__ import annotations
import atexit
import os
import sys
+from typing import TYPE_CHECKING
import rich
-from rich.console import Console
from airflow_breeze.utils.path_utils import in_autocomplete
+if TYPE_CHECKING:
+ from rich.console import Console
+
help_console: Console | None = None
DEFAULT_COLUMNS = 129
diff --git a/dev/stats/calculate_statistics_provider_testing_issues.py
b/dev/stats/calculate_statistics_provider_testing_issues.py
index 7172c2beef..18a64daad6 100755
--- a/dev/stats/calculate_statistics_provider_testing_issues.py
+++ b/dev/stats/calculate_statistics_provider_testing_issues.py
@@ -22,14 +22,17 @@ import os
import re
import textwrap
from pathlib import Path
+from typing import TYPE_CHECKING
import rich_click as click
from attr import dataclass
from github import Github
-from github.Issue import Issue
from rich.console import Console
from tabulate import tabulate
+if TYPE_CHECKING:
+ from github.Issue import Issue
+
PROVIDER_TESTING_LABEL = "testing status"
logger = logging.getLogger(__name__)
diff --git a/dev/stats/get_important_pr_candidates.py
b/dev/stats/get_important_pr_candidates.py
index 3830953c6f..6256df4a3c 100755
--- a/dev/stats/get_important_pr_candidates.py
+++ b/dev/stats/get_important_pr_candidates.py
@@ -25,13 +25,16 @@ import re
import textwrap
from datetime import datetime
from functools import cached_property
+from typing import TYPE_CHECKING
import pendulum
import rich_click as click
from github import Github, UnknownObjectException
-from github.PullRequest import PullRequest
from rich.console import Console
+if TYPE_CHECKING:
+ from github.PullRequest import PullRequest
+
logger = logging.getLogger(__name__)
console = Console(width=400, color_system="standard")
diff --git a/docs/exts/airflow_intersphinx.py b/docs/exts/airflow_intersphinx.py
index 129d037e7a..790e601a4f 100644
--- a/docs/exts/airflow_intersphinx.py
+++ b/docs/exts/airflow_intersphinx.py
@@ -18,10 +18,12 @@ from __future__ import annotations
import os
import time
-from typing import Any
+from typing import TYPE_CHECKING, Any
from provider_yaml_utils import load_package_data
-from sphinx.application import Sphinx
+
+if TYPE_CHECKING:
+ from sphinx.application import Sphinx
CURRENT_DIR = os.path.dirname(__file__)
ROOT_DIR = os.path.abspath(os.path.join(CURRENT_DIR, os.pardir, os.pardir))
diff --git a/docs/exts/operators_and_hooks_ref.py
b/docs/exts/operators_and_hooks_ref.py
index 06c37cf0b4..e5f61684e2 100644
--- a/docs/exts/operators_and_hooks_ref.py
+++ b/docs/exts/operators_and_hooks_ref.py
@@ -20,13 +20,12 @@ import ast
import os
from functools import lru_cache
from pathlib import Path
-from typing import Any, Iterable, Iterator
+from typing import TYPE_CHECKING, Any, Iterable, Iterator
import jinja2
import rich_click as click
import yaml
from docutils import nodes
-from docutils.nodes import Element
# No stub exists for docutils.parsers.rst.directives. See
https://github.com/python/typeshed/issues/5755.
from docutils.parsers.rst import Directive, directives # type:
ignore[attr-defined]
@@ -35,6 +34,9 @@ from provider_yaml_utils import get_provider_yaml_paths,
load_package_data
from sphinx.util import nested_parse_with_titles
from sphinx.util.docutils import switch_source_input
+if TYPE_CHECKING:
+ from docutils.nodes import Element
+
CMD_OPERATORS_AND_HOOKS = "operators-and-hooks"
CMD_TRANSFERS = "transfers"
diff --git a/docs/exts/provider_init_hack.py b/docs/exts/provider_init_hack.py
index fa082afc49..33188c5db8 100644
--- a/docs/exts/provider_init_hack.py
+++ b/docs/exts/provider_init_hack.py
@@ -22,8 +22,10 @@ like a real package for building docs
from __future__ import annotations
import os
+from typing import TYPE_CHECKING
-from sphinx.application import Sphinx
+if TYPE_CHECKING:
+ from sphinx.application import Sphinx
ROOT_PROJECT_DIR = os.path.abspath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir,
os.pardir)
diff --git a/docs/exts/providers_packages_ref.py
b/docs/exts/providers_packages_ref.py
index 1f5a3ac1e5..8fcac01e85 100644
--- a/docs/exts/providers_packages_ref.py
+++ b/docs/exts/providers_packages_ref.py
@@ -16,8 +16,12 @@
# under the License.
from __future__ import annotations
+from typing import TYPE_CHECKING
+
from provider_yaml_utils import load_package_data
-from sphinx.application import Sphinx
+
+if TYPE_CHECKING:
+ from sphinx.application import Sphinx
def _on_config_inited(app, config):
diff --git a/docs/exts/substitution_extensions.py
b/docs/exts/substitution_extensions.py
index b7aa9268b7..d8c6c4e48d 100644
--- a/docs/exts/substitution_extensions.py
+++ b/docs/exts/substitution_extensions.py
@@ -17,18 +17,20 @@
from __future__ import annotations
import logging
-from typing import Any
+from typing import TYPE_CHECKING, Any
from docutils import nodes
-from docutils.nodes import Node, system_message
# No stub exists for docutils.parsers.rst.directives. See
https://github.com/python/typeshed/issues/5755.
from docutils.parsers.rst import Directive, directives # type:
ignore[attr-defined]
from docutils.parsers.rst.roles import code_role
-from sphinx.application import Sphinx
from sphinx.transforms import SphinxTransform
from sphinx.transforms.post_transforms.code import HighlightLanguageTransform
+if TYPE_CHECKING:
+ from docutils.nodes import Node, system_message
+ from sphinx.application import Sphinx
+
LOGGER = logging.getLogger(__name__)
OriginalCodeBlock: Directive = directives._directives["code-block"]
diff --git a/pyproject.toml b/pyproject.toml
index ca6f9c61cf..1732edc78f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -62,7 +62,9 @@ extend-select = [
"D402",
"D403",
"D412",
- "D419"
+ "D419",
+ "TCH001", # typing-only-first-party-import
+ "TCH002", # typing-only-third-party-import
]
extend-ignore = [
"D203",
@@ -169,3 +171,5 @@ exclude_also = [
"@(typing(_extensions)?\\.)?overload",
"if TYPE_CHECKING:"
]
+[tool.ruff.flake8-type-checking]
+exempt-modules = ["typing", "typing_extensions"]
diff --git a/tests/api_connexion/endpoints/test_dag_source_endpoint.py
b/tests/api_connexion/endpoints/test_dag_source_endpoint.py
index 7a585b5902..c8010117aa 100644
--- a/tests/api_connexion/endpoints/test_dag_source_endpoint.py
+++ b/tests/api_connexion/endpoints/test_dag_source_endpoint.py
@@ -18,15 +18,18 @@ from __future__ import annotations
import ast
import os
+from typing import TYPE_CHECKING
import pytest
-from airflow import DAG
from airflow.models import DagBag
from airflow.security import permissions
from tests.test_utils.api_connexion_utils import assert_401, create_user,
delete_user
from tests.test_utils.db import clear_db_dag_code, clear_db_dags,
clear_db_serialized_dags
+if TYPE_CHECKING:
+ from airflow import DAG
+
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir,
os.pardir))
EXAMPLE_DAG_FILE = os.path.join("airflow", "example_dags",
"example_bash_operator.py")
diff --git a/tests/api_internal/endpoints/test_rpc_api_endpoint.py
b/tests/api_internal/endpoints/test_rpc_api_endpoint.py
index d72448324e..0c8c7c0c0b 100644
--- a/tests/api_internal/endpoints/test_rpc_api_endpoint.py
+++ b/tests/api_internal/endpoints/test_rpc_api_endpoint.py
@@ -17,11 +17,10 @@
from __future__ import annotations
import json
-from typing import Generator
+from typing import TYPE_CHECKING, Generator
from unittest import mock
import pytest
-from flask import Flask
from airflow.models.connection import Connection
from airflow.models.taskinstance import TaskInstance
@@ -34,6 +33,9 @@ from airflow.www import app
from tests.test_utils.config import conf_vars
from tests.test_utils.decorators import dont_initialize_flask_app_submodules
+if TYPE_CHECKING:
+ from flask import Flask
+
TEST_METHOD_NAME = "test_method"
TEST_METHOD_WITH_LOG_NAME = "test_method_with_log"
diff --git a/tests/api_internal/test_internal_api_call.py
b/tests/api_internal/test_internal_api_call.py
index 67c4fb533c..c3eda6e184 100644
--- a/tests/api_internal/test_internal_api_call.py
+++ b/tests/api_internal/test_internal_api_call.py
@@ -19,6 +19,7 @@
from __future__ import annotations
import json
+from typing import TYPE_CHECKING
from unittest import mock
import pytest
@@ -27,12 +28,14 @@ import requests
from airflow.api_internal.internal_api_call import InternalApiConfig,
internal_api_call
from airflow.models.taskinstance import TaskInstance
from airflow.operators.empty import EmptyOperator
-from airflow.serialization.pydantic.taskinstance import TaskInstancePydantic
from airflow.serialization.serialized_objects import BaseSerialization
from airflow.settings import _ENABLE_AIP_44
from airflow.utils.state import State
from tests.test_utils.config import conf_vars
+if TYPE_CHECKING:
+ from airflow.serialization.pydantic.taskinstance import
TaskInstancePydantic
+
@pytest.fixture(autouse=True)
def reset_init_api_config():
diff --git a/tests/auth/managers/fab/cli_commands/test_role_command.py
b/tests/auth/managers/fab/cli_commands/test_role_command.py
index 5259ca8482..24dc899256 100644
--- a/tests/auth/managers/fab/cli_commands/test_role_command.py
+++ b/tests/auth/managers/fab/cli_commands/test_role_command.py
@@ -20,15 +20,18 @@ from __future__ import annotations
import io
import json
from contextlib import redirect_stdout
+from typing import TYPE_CHECKING
import pytest
from airflow.auth.managers.fab.cli_commands import role_command
from airflow.auth.managers.fab.cli_commands.utils import
get_application_builder
-from airflow.auth.managers.fab.models import Role
from airflow.cli import cli_parser
from airflow.security import permissions
+if TYPE_CHECKING:
+ from airflow.auth.managers.fab.models import Role
+
TEST_USER1_EMAIL = "[email protected]"
TEST_USER2_EMAIL = "[email protected]"
diff --git a/tests/cli/commands/test_task_command.py
b/tests/cli/commands/test_task_command.py
index 19100bb5af..c02ccec203 100644
--- a/tests/cli/commands/test_task_command.py
+++ b/tests/cli/commands/test_task_command.py
@@ -28,6 +28,7 @@ import unittest
from argparse import ArgumentParser
from contextlib import contextmanager, redirect_stdout
from pathlib import Path
+from typing import TYPE_CHECKING
from unittest import mock
from unittest.mock import sentinel
@@ -35,7 +36,6 @@ import pendulum
import pytest
import sqlalchemy.exc
-from airflow import DAG
from airflow.cli import cli_parser
from airflow.cli.commands import task_command
from airflow.cli.commands.task_command import LoggerMutationHelper
@@ -52,6 +52,9 @@ from setup import AIRFLOW_SOURCES_ROOT
from tests.test_utils.config import conf_vars
from tests.test_utils.db import clear_db_pools, clear_db_runs
+if TYPE_CHECKING:
+ from airflow import DAG
+
DEFAULT_DATE = timezone.datetime(2022, 1, 1)
ROOT_FOLDER = os.path.realpath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir,
os.pardir)
diff --git a/tests/cluster_policies/__init__.py
b/tests/cluster_policies/__init__.py
index 5aaf5701ac..f4e57a67aa 100644
--- a/tests/cluster_policies/__init__.py
+++ b/tests/cluster_policies/__init__.py
@@ -19,13 +19,15 @@ from __future__ import annotations
from abc import ABC
from datetime import timedelta
-from typing import Callable
+from typing import TYPE_CHECKING, Callable
from airflow.configuration import conf
from airflow.exceptions import AirflowClusterPolicySkipDag,
AirflowClusterPolicyViolation
-from airflow.models import DAG, TaskInstance
from airflow.models.baseoperator import BaseOperator
+if TYPE_CHECKING:
+ from airflow.models import DAG, TaskInstance
+
# [START example_cluster_policy_rule]
def task_must_have_owners(task: BaseOperator):
diff --git a/tests/dags/test_on_kill.py b/tests/dags/test_on_kill.py
index 90ffd90e59..a78a5489d8 100644
--- a/tests/dags/test_on_kill.py
+++ b/tests/dags/test_on_kill.py
@@ -18,12 +18,15 @@
from __future__ import annotations
import time
+from typing import TYPE_CHECKING
from airflow.models import DAG
from airflow.operators.empty import EmptyOperator
-from airflow.utils.context import Context
from airflow.utils.timezone import datetime
+if TYPE_CHECKING:
+ from airflow.utils.context import Context
+
class DummyWithOnKill(EmptyOperator):
def execute(self, context: Context):
diff --git a/tests/dags/test_parsing_context.py
b/tests/dags/test_parsing_context.py
index aca3bf4981..2a88a7829a 100644
--- a/tests/dags/test_parsing_context.py
+++ b/tests/dags/test_parsing_context.py
@@ -18,16 +18,19 @@
from __future__ import annotations
from pathlib import Path
+from typing import TYPE_CHECKING
from airflow.models.dag import DAG
from airflow.operators.empty import EmptyOperator
-from airflow.utils.context import Context
from airflow.utils.dag_parsing_context import (
_AIRFLOW_PARSING_CONTEXT_DAG_ID,
_AIRFLOW_PARSING_CONTEXT_TASK_ID,
)
from airflow.utils.timezone import datetime
+if TYPE_CHECKING:
+ from airflow.utils.context import Context
+
class DagWithParsingContext(EmptyOperator):
def execute(self, context: Context):
diff --git a/tests/decorators/test_python.py b/tests/decorators/test_python.py
index f1c84721b4..b24fcb1707 100644
--- a/tests/decorators/test_python.py
+++ b/tests/decorators/test_python.py
@@ -41,6 +41,9 @@ from airflow.utils.types import DagRunType
from airflow.utils.xcom import XCOM_RETURN_KEY
from tests.operators.test_python import BasePythonTest
+if TYPE_CHECKING:
+ from airflow.models.dagrun import DagRun
+
DEFAULT_DATE = timezone.datetime(2016, 1, 1)
@@ -795,7 +798,6 @@ def test_task_decorator_has_wrapped_attr():
def test_upstream_exception_produces_none_xcom(dag_maker, session):
from airflow.exceptions import AirflowSkipException
- from airflow.models.dagrun import DagRun
from airflow.utils.trigger_rule import TriggerRule
result = None
@@ -890,7 +892,6 @@ def test_no_warnings(reset_logging_config, caplog):
def test_task_decorator_dataset(dag_maker, session):
from airflow import Dataset
- from airflow.models.dagrun import DagRun
result = None
uri = "s3://test"
diff --git a/tests/models/test_baseoperator.py
b/tests/models/test_baseoperator.py
index b5735c650c..0f96704784 100644
--- a/tests/models/test_baseoperator.py
+++ b/tests/models/test_baseoperator.py
@@ -22,7 +22,7 @@ import logging
import uuid
from collections import defaultdict
from datetime import date, datetime, timedelta
-from typing import Any, NamedTuple
+from typing import TYPE_CHECKING, Any, NamedTuple
from unittest import mock
import jinja2
@@ -39,7 +39,6 @@ from airflow.models.baseoperator import (
chain_linear,
cross_downstream,
)
-from airflow.utils.context import Context
from airflow.utils.edgemodifier import Label
from airflow.utils.task_group import TaskGroup
from airflow.utils.trigger_rule import TriggerRule
@@ -49,6 +48,9 @@ from tests.models import DEFAULT_DATE
from tests.test_utils.config import conf_vars
from tests.test_utils.mock_operators import DeprecatedOperator, MockOperator
+if TYPE_CHECKING:
+ from airflow.utils.context import Context
+
class ClassWithCustomAttributes:
"""Class for testing purpose: allows to create objects with custom
attributes in one single statement."""
diff --git a/tests/models/test_dagrun.py b/tests/models/test_dagrun.py
index 62affe2485..15b70bffdd 100644
--- a/tests/models/test_dagrun.py
+++ b/tests/models/test_dagrun.py
@@ -19,13 +19,12 @@ from __future__ import annotations
import datetime
from functools import reduce
-from typing import Mapping
+from typing import TYPE_CHECKING, Mapping
from unittest import mock
from unittest.mock import call
import pendulum
import pytest
-from sqlalchemy.orm.session import Session
from airflow import settings
from airflow.callbacks.callback_requests import DagCallbackRequest
@@ -58,6 +57,9 @@ from tests.test_utils import db
from tests.test_utils.config import conf_vars
from tests.test_utils.mock_operators import MockOperator
+if TYPE_CHECKING:
+ from sqlalchemy.orm.session import Session
+
DEFAULT_DATE = pendulum.instance(_DEFAULT_DATE)
diff --git a/tests/models/test_mappedoperator.py
b/tests/models/test_mappedoperator.py
index 8366acf0b7..51760b8570 100644
--- a/tests/models/test_mappedoperator.py
+++ b/tests/models/test_mappedoperator.py
@@ -20,6 +20,7 @@ from __future__ import annotations
import logging
from collections import defaultdict
from datetime import timedelta
+from typing import TYPE_CHECKING
from unittest import mock
from unittest.mock import patch
@@ -36,7 +37,6 @@ from airflow.models.taskinstance import TaskInstance
from airflow.models.taskmap import TaskMap
from airflow.models.xcom_arg import XComArg
from airflow.operators.python import PythonOperator
-from airflow.utils.context import Context
from airflow.utils.state import TaskInstanceState
from airflow.utils.task_group import TaskGroup
from airflow.utils.trigger_rule import TriggerRule
@@ -45,6 +45,9 @@ from tests.models import DEFAULT_DATE
from tests.test_utils.mapping import expand_mapped_task
from tests.test_utils.mock_operators import MockOperator,
MockOperatorWithNestedFields, NestedFields
+if TYPE_CHECKING:
+ from airflow.utils.context import Context
+
def test_task_mapping_with_dag():
with DAG("test-dag", start_date=DEFAULT_DATE) as dag:
diff --git a/tests/models/test_xcom.py b/tests/models/test_xcom.py
index a211a9324d..7d1a63d8e9 100644
--- a/tests/models/test_xcom.py
+++ b/tests/models/test_xcom.py
@@ -19,11 +19,11 @@ from __future__ import annotations
import datetime
import operator
import os
+from typing import TYPE_CHECKING
from unittest import mock
from unittest.mock import MagicMock
import pytest
-from sqlalchemy.orm import Session
from airflow.configuration import conf
from airflow.models.dagrun import DagRun, DagRunType
@@ -37,6 +37,9 @@ from airflow.utils.session import create_session
from airflow.utils.xcom import XCOM_RETURN_KEY
from tests.test_utils.config import conf_vars
+if TYPE_CHECKING:
+ from sqlalchemy.orm import Session
+
class CustomXCom(BaseXCom):
orm_deserialize_value = mock.Mock()
diff --git a/tests/notifications/test_basenotifier.py
b/tests/notifications/test_basenotifier.py
index cb768890d7..289005345e 100644
--- a/tests/notifications/test_basenotifier.py
+++ b/tests/notifications/test_basenotifier.py
@@ -17,6 +17,7 @@
from __future__ import annotations
+from typing import TYPE_CHECKING
from unittest.mock import MagicMock
import jinja2
@@ -24,7 +25,9 @@ import pytest
from airflow.notifications.basenotifier import BaseNotifier
from airflow.operators.empty import EmptyOperator
-from airflow.utils.context import Context
+
+if TYPE_CHECKING:
+ from airflow.utils.context import Context
class MockNotifier(BaseNotifier):
diff --git a/tests/sensors/test_base.py b/tests/sensors/test_base.py
index d4879165f3..2851febc47 100644
--- a/tests/sensors/test_base.py
+++ b/tests/sensors/test_base.py
@@ -18,6 +18,7 @@
from __future__ import annotations
from datetime import timedelta
+from typing import TYPE_CHECKING
from unittest.mock import Mock, patch
import pytest
@@ -54,11 +55,13 @@ from
airflow.providers.cncf.kubernetes.executors.local_kubernetes_executor impor
from airflow.sensors.base import BaseSensorOperator, PokeReturnValue,
poke_mode_only
from airflow.ti_deps.deps.ready_to_reschedule import ReadyToRescheduleDep
from airflow.utils import timezone
-from airflow.utils.context import Context
from airflow.utils.state import State
from airflow.utils.timezone import datetime
from tests.test_utils import db
+if TYPE_CHECKING:
+ from airflow.utils.context import Context
+
DEFAULT_DATE = datetime(2015, 1, 1)
TEST_DAG_ID = "unit_test_dag"
DUMMY_OP = "dummy_op"
diff --git a/tests/sensors/test_timeout_sensor.py
b/tests/sensors/test_timeout_sensor.py
index 3fcf66d199..7b76e3d848 100644
--- a/tests/sensors/test_timeout_sensor.py
+++ b/tests/sensors/test_timeout_sensor.py
@@ -19,6 +19,7 @@ from __future__ import annotations
import time
from datetime import timedelta
+from typing import TYPE_CHECKING
import pytest
@@ -26,9 +27,11 @@ from airflow.exceptions import AirflowSensorTimeout,
AirflowSkipException
from airflow.models.dag import DAG
from airflow.sensors.base import BaseSensorOperator
from airflow.utils import timezone
-from airflow.utils.context import Context
from airflow.utils.timezone import datetime
+if TYPE_CHECKING:
+ from airflow.utils.context import Context
+
DEFAULT_DATE = datetime(2015, 1, 1)
TEST_DAG_ID = "unit_test_dag"
diff --git a/tests/serialization/test_dag_serialization.py
b/tests/serialization/test_dag_serialization.py
index 061a1531ec..7ecadf9d2a 100644
--- a/tests/serialization/test_dag_serialization.py
+++ b/tests/serialization/test_dag_serialization.py
@@ -28,6 +28,7 @@ import pickle
from datetime import datetime, timedelta
from glob import glob
from pathlib import Path
+from typing import TYPE_CHECKING
from unittest import mock
import attr
@@ -64,7 +65,6 @@ from airflow.serialization.serialized_objects import (
from airflow.ti_deps.deps.base_ti_dep import BaseTIDep
from airflow.timetables.simple import NullTimetable, OnceTimetable
from airflow.utils import timezone
-from airflow.utils.context import Context
from airflow.utils.operator_resources import Resources
from airflow.utils.task_group import TaskGroup
from airflow.utils.xcom import XCOM_RETURN_KEY
@@ -72,6 +72,9 @@ from tests.test_utils.config import conf_vars
from tests.test_utils.mock_operators import AirflowLink2, CustomOperator,
GoogleLink, MockOperator
from tests.test_utils.timetables import CustomSerializationTimetable,
cron_timetable, delta_timetable
+if TYPE_CHECKING:
+ from airflow.utils.context import Context
+
repo_root = Path(airflow.__file__).parent.parent
diff --git a/tests/system/providers/amazon/aws/example_glue.py
b/tests/system/providers/amazon/aws/example_glue.py
index e0ce565cdf..635a3fe7c1 100644
--- a/tests/system/providers/amazon/aws/example_glue.py
+++ b/tests/system/providers/amazon/aws/example_glue.py
@@ -17,9 +17,9 @@
from __future__ import annotations
from datetime import datetime
+from typing import TYPE_CHECKING
import boto3
-from botocore.client import BaseClient
from airflow import DAG
from airflow.decorators import task
@@ -36,6 +36,9 @@ from airflow.providers.amazon.aws.sensors.glue_crawler import
GlueCrawlerSensor
from airflow.utils.trigger_rule import TriggerRule
from tests.system.providers.amazon.aws.utils import ENV_ID_KEY,
SystemTestContextBuilder, prune_logs
+if TYPE_CHECKING:
+ from botocore.client import BaseClient
+
DAG_ID = "example_glue"
# Externally fetched variables:
diff --git a/tests/system/providers/amazon/aws/utils/__init__.py
b/tests/system/providers/amazon/aws/utils/__init__.py
index b1993e5708..fa942aae34 100644
--- a/tests/system/providers/amazon/aws/utils/__init__.py
+++ b/tests/system/providers/amazon/aws/utils/__init__.py
@@ -22,10 +22,10 @@ import logging
import os
from os.path import basename, splitext
from time import sleep
+from typing import TYPE_CHECKING
from uuid import uuid4
import boto3
-from botocore.client import BaseClient
from botocore.exceptions import ClientError, NoCredentialsError
from airflow.decorators import task
@@ -33,6 +33,9 @@ from airflow.providers.amazon.aws.hooks.ssm import SsmHook
from airflow.utils.state import State
from airflow.utils.trigger_rule import TriggerRule
+if TYPE_CHECKING:
+ from botocore.client import BaseClient
+
ENV_ID_ENVIRON_KEY: str = "SYSTEM_TESTS_ENV_ID"
ENV_ID_KEY: str = "ENV_ID"
DEFAULT_ENV_ID_PREFIX: str = "env"
diff --git a/tests/system/providers/amazon/aws/utils/k8s.py
b/tests/system/providers/amazon/aws/utils/k8s.py
index 5868cc6e71..551d09629e 100644
--- a/tests/system/providers/amazon/aws/utils/k8s.py
+++ b/tests/system/providers/amazon/aws/utils/k8s.py
@@ -16,9 +16,13 @@
# under the License.
from __future__ import annotations
-from airflow.models.operator import Operator
+from typing import TYPE_CHECKING
+
from airflow.operators.bash import BashOperator
+if TYPE_CHECKING:
+ from airflow.models.operator import Operator
+
def get_describe_pod_operator(cluster_name: str, pod_name: str) -> Operator:
"""Returns an operator that'll print the output of a `k describe pod` in
the airflow logs."""
diff --git
a/tests/system/providers/google/cloud/cloud_sql/example_cloud_sql_query_mysql.py
b/tests/system/providers/google/cloud/cloud_sql/example_cloud_sql_query_mysql.py
index 2a3050ead6..8ac3608286 100644
---
a/tests/system/providers/google/cloud/cloud_sql/example_cloud_sql_query_mysql.py
+++
b/tests/system/providers/google/cloud/cloud_sql/example_cloud_sql_query_mysql.py
@@ -25,6 +25,7 @@ import os
from collections import namedtuple
from copy import deepcopy
from datetime import datetime
+from typing import TYPE_CHECKING
from googleapiclient import discovery
@@ -38,9 +39,11 @@ from airflow.providers.google.cloud.operators.cloud_sql
import (
CloudSQLDeleteInstanceOperator,
CloudSQLExecuteQueryOperator,
)
-from airflow.settings import Session
from airflow.utils.trigger_rule import TriggerRule
+if TYPE_CHECKING:
+ from airflow.settings import Session
+
ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID")
PROJECT_ID = os.environ.get("SYSTEM_TESTS_GCP_PROJECT")
DAG_ID = "cloudsql-query-mysql"
diff --git
a/tests/system/providers/google/cloud/cloud_sql/example_cloud_sql_query_postgres.py
b/tests/system/providers/google/cloud/cloud_sql/example_cloud_sql_query_postgres.py
index e2a21f7b46..8acb62186e 100644
---
a/tests/system/providers/google/cloud/cloud_sql/example_cloud_sql_query_postgres.py
+++
b/tests/system/providers/google/cloud/cloud_sql/example_cloud_sql_query_postgres.py
@@ -25,6 +25,7 @@ import os
from collections import namedtuple
from copy import deepcopy
from datetime import datetime
+from typing import TYPE_CHECKING
from googleapiclient import discovery
@@ -38,9 +39,11 @@ from airflow.providers.google.cloud.operators.cloud_sql
import (
CloudSQLDeleteInstanceOperator,
CloudSQLExecuteQueryOperator,
)
-from airflow.settings import Session
from airflow.utils.trigger_rule import TriggerRule
+if TYPE_CHECKING:
+ from airflow.settings import Session
+
ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID")
PROJECT_ID = os.environ.get("SYSTEM_TESTS_GCP_PROJECT")
DAG_ID = "cloudsql-query-pg"
diff --git a/tests/system/utils/__init__.py b/tests/system/utils/__init__.py
index 72b3c861ba..d6b01b598a 100644
--- a/tests/system/utils/__init__.py
+++ b/tests/system/utils/__init__.py
@@ -19,13 +19,15 @@ from __future__ import annotations
import logging
import os
from datetime import timedelta
-from typing import Callable
+from typing import TYPE_CHECKING, Callable
from tabulate import tabulate
-from airflow.utils.context import Context
from airflow.utils.state import State
+if TYPE_CHECKING:
+ from airflow.utils.context import Context
+
def get_test_run(dag):
def callback(context: Context):
diff --git a/tests/test_utils/mock_operators.py
b/tests/test_utils/mock_operators.py
index b1ece18722..4d0e4b7489 100644
--- a/tests/test_utils/mock_operators.py
+++ b/tests/test_utils/mock_operators.py
@@ -17,14 +17,17 @@
from __future__ import annotations
import warnings
-from typing import Any, Sequence
+from typing import TYPE_CHECKING, Any, Sequence
import attr
-import jinja2
from airflow.models.baseoperator import BaseOperator, BaseOperatorLink
from airflow.models.xcom import XCom
-from airflow.utils.context import Context
+
+if TYPE_CHECKING:
+ import jinja2
+
+ from airflow.utils.context import Context
class MockOperator(BaseOperator):
diff --git a/tests/test_utils/remote_user_api_auth_backend.py
b/tests/test_utils/remote_user_api_auth_backend.py
index 3161d08364..b86a404fd1 100644
--- a/tests/test_utils/remote_user_api_auth_backend.py
+++ b/tests/test_utils/remote_user_api_auth_backend.py
@@ -20,14 +20,16 @@ from __future__ import annotations
import logging
from functools import wraps
-from typing import Callable, TypeVar, cast
+from typing import TYPE_CHECKING, Callable, TypeVar, cast
from flask import Response, request
from flask_login import login_user
-from requests.auth import AuthBase
from airflow.utils.airflow_flask_app import get_airflow_app
+if TYPE_CHECKING:
+ from requests.auth import AuthBase
+
log = logging.getLogger(__name__)
CLIENT_AUTH: tuple[str, str] | AuthBase | None = None
diff --git a/tests/ti_deps/deps/test_trigger_rule_dep.py
b/tests/ti_deps/deps/test_trigger_rule_dep.py
index faa70b5a49..4293303bd0 100644
--- a/tests/ti_deps/deps/test_trigger_rule_dep.py
+++ b/tests/ti_deps/deps/test_trigger_rule_dep.py
@@ -18,7 +18,7 @@
from __future__ import annotations
from datetime import datetime
-from typing import Iterator
+from typing import TYPE_CHECKING, Iterator
from unittest import mock
from unittest.mock import Mock
@@ -27,7 +27,6 @@ from pytest import param
from airflow.decorators import task, task_group
from airflow.models.baseoperator import BaseOperator
-from airflow.models.dagrun import DagRun
from airflow.models.taskinstance import TaskInstance
from airflow.operators.empty import EmptyOperator
from airflow.ti_deps.dep_context import DepContext
@@ -35,6 +34,9 @@ from airflow.ti_deps.deps.trigger_rule_dep import
TriggerRuleDep, _UpstreamTISta
from airflow.utils.state import DagRunState, TaskInstanceState
from airflow.utils.trigger_rule import TriggerRule
+if TYPE_CHECKING:
+ from airflow.models.dagrun import DagRun
+
SKIPPED = TaskInstanceState.SKIPPED
UPSTREAM_FAILED = TaskInstanceState.UPSTREAM_FAILED
diff --git a/tests/utils/log/test_log_reader.py
b/tests/utils/log/test_log_reader.py
index da7dbc98da..81dffc8afc 100644
--- a/tests/utils/log/test_log_reader.py
+++ b/tests/utils/log/test_log_reader.py
@@ -22,6 +22,7 @@ import logging
import os
import sys
import tempfile
+from typing import TYPE_CHECKING
from unittest import mock
import pendulum
@@ -29,7 +30,6 @@ import pytest
from airflow import settings
from airflow.config_templates.airflow_local_settings import
DEFAULT_LOGGING_CONFIG
-from airflow.models import DagRun
from airflow.models.tasklog import LogTemplate
from airflow.operators.python import PythonOperator
from airflow.timetables.base import DataInterval
@@ -41,6 +41,9 @@ from airflow.utils.types import DagRunType
from tests.test_utils.config import conf_vars
from tests.test_utils.db import clear_db_dags, clear_db_runs
+if TYPE_CHECKING:
+ from airflow.models import DagRun
+
class TestLogView:
DAG_ID = "dag_log_reader"
diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py
index 46462c30d1..dad1e4dba6 100644
--- a/tests/utils/test_helpers.py
+++ b/tests/utils/test_helpers.py
@@ -19,13 +19,12 @@ from __future__ import annotations
import itertools
import re
+from typing import TYPE_CHECKING
import pytest
from airflow import AirflowException
from airflow.jobs.base_job_runner import BaseJobRunner
-from airflow.jobs.job import Job
-from airflow.serialization.pydantic.job import JobPydantic
from airflow.utils import helpers, timezone
from airflow.utils.helpers import (
at_most_one,
@@ -40,6 +39,10 @@ from airflow.utils.types import NOTSET
from tests.test_utils.config import conf_vars
from tests.test_utils.db import clear_db_dags, clear_db_runs
+if TYPE_CHECKING:
+ from airflow.jobs.job import Job
+ from airflow.serialization.pydantic.job import JobPydantic
+
@pytest.fixture()
def clear_db():
diff --git a/tests/www/views/test_views_grid.py
b/tests/www/views/test_views_grid.py
index 57e7e56865..16abb2bea1 100644
--- a/tests/www/views/test_views_grid.py
+++ b/tests/www/views/test_views_grid.py
@@ -17,6 +17,8 @@
# under the License.
from __future__ import annotations
+from typing import TYPE_CHECKING
+
import pendulum
import pytest
from dateutil.tz import UTC
@@ -25,7 +27,6 @@ from airflow.datasets import Dataset
from airflow.decorators import task_group
from airflow.lineage.entities import File
from airflow.models import DagBag
-from airflow.models.dagrun import DagRun
from airflow.models.dataset import DatasetDagRunQueue, DatasetEvent,
DatasetModel
from airflow.operators.empty import EmptyOperator
from airflow.utils import timezone
@@ -37,6 +38,9 @@ from tests.test_utils.asserts import assert_queries_count
from tests.test_utils.db import clear_db_datasets, clear_db_runs
from tests.test_utils.mock_operators import MockOperator
+if TYPE_CHECKING:
+ from airflow.models.dagrun import DagRun
+
DAG_ID = "test"