From 845a16057625127490ba8d655b3fe1114373ff7b Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Thu, 7 Nov 2024 10:51:02 +0800 Subject: [PATCH] Note in pytest_plugin to not global import Airflow (#43738) --- tests_common/pytest_plugin.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests_common/pytest_plugin.py b/tests_common/pytest_plugin.py index fbf6ce80b9c3..a5fdec95cbd2 100644 --- a/tests_common/pytest_plugin.py +++ b/tests_common/pytest_plugin.py @@ -50,6 +50,23 @@ Op = TypeVar("Op", bound=BaseOperator) +# NOTE: DO NOT IMPORT AIRFLOW THINGS HERE! +# +# This plugin is responsible for configuring Airflow correctly to run tests. +# Importing Airflow here loads Airflow too eagerly and break the configurations. +# Instead, import what you want lazily inside a fixture function. +# +# Be aware that many things in tests_common also indirectly imports Airflow, so +# those modules also should not be imported globally. +# +# (Things in the TYPE_CHECKING block are fine because they are not actually +# imported at runtime; those imports are only hints to the type checker.) + +assert "airflow" not in sys.modules, ( + "Airflow SHOULD NOT have been imported at this point! " + "Read comments in pytest_plugin.py to understand more." +) + # https://docs.pytest.org/en/stable/reference/reference.html#stash capture_warnings_key = pytest.StashKey["CaptureWarningsPlugin"]() forbidden_warnings_key = pytest.StashKey["ForbiddenWarningsPlugin"]()