This is an automated email from the ASF dual-hosted git repository.

uranusjr 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 845a1605762 Note in pytest_plugin to not global import Airflow (#43738)
845a1605762 is described below

commit 845a16057625127490ba8d655b3fe1114373ff7b
Author: Tzu-ping Chung <[email protected]>
AuthorDate: Thu Nov 7 10:51:02 2024 +0800

    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 fbf6ce80b9c..a5fdec95cbd 100644
--- a/tests_common/pytest_plugin.py
+++ b/tests_common/pytest_plugin.py
@@ -50,6 +50,23 @@ if TYPE_CHECKING:
 
     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"]()

Reply via email to