diff --git a/airflow-core/src/airflow/api_fastapi/core_api/services/ui/connections.py b/airflow-core/src/airflow/api_fastapi/core_api/services/ui/connections.py index 75a13a175e488..73519c5271734 100644 --- a/airflow-core/src/airflow/api_fastapi/core_api/services/ui/connections.py +++ b/airflow-core/src/airflow/api_fastapi/core_api/services/ui/connections.py @@ -153,14 +153,14 @@ def mock_any_of(allowed_values: list) -> HookMetaService.MockEnum: ]: try: if not find_spec(mod_name): - raise ModuleNotFoundError + raise ModuleNotFoundError(f"No module named {mod_name!r}", name=mod_name) except ModuleNotFoundError: sys.modules[mod_name] = MagicMock() # We conditionally inject mock classes for missing dependencies # to ensure `ProvidersManager` can initialize hook connection widgets # without crashing when FAB/WTForms are not installed. - if "wtforms.StringField" not in sys.modules: + if isinstance(sys.modules.get("wtforms"), MagicMock): # Only apply mocks if the actual module wasn't loaded beforehand. # This avoids thread-safety issues caused by `unittest.mock.patch` mutating global states. with (