Fix circular import between connection and configuration modules#63787
Open
Dev-iL wants to merge 1 commit intoapache:mainfrom
Open
Fix circular import between connection and configuration modules#63787Dev-iL wants to merge 1 commit intoapache:mainfrom
Dev-iL wants to merge 1 commit intoapache:mainfrom
Conversation
Importing airflow.models.Connection pulled in airflow.configuration, and airflow.configuration initializes secrets backends at import time. That initialization itself does `from airflow.models import Connection`, so under Python 3.14 the lazy import path could re-enter `airflow.models.__getattr__ before connection.py` had finished defining the Connection class. The result was the ImportError: Module "airflow.models.connection" does not define a "Connection"; plus repeated SQLAlchemy warnings about redefining the model during partial imports. By deferring: `from airflow.configuration import conf, ensure_secrets_loaded` until the method actually needs them, importing the module no longer triggers that cycle. `Connection` gets fully defined first, and only later, when get_connection_from_secrets() runs, do we touch configuration and secrets loading. That keeps behavior the same at runtime, but removes the import-time recursion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
from airflow.configuration import conf, ensure_secrets_loadedfrom module-level to insideget_connection_from_secrets(), breaking a circular import cycle whereairflow.models.Connection→airflow.configuration→ secrets backend init →airflow.models.Connection.Details
connection.pyimportsairflow.configurationat module level. During startup,airflow.configurationinitializes secrets backends, which in turn importairflow.models.Connection. This creates a circular import that can causeImportError: Module "airflow.models.connection" does not define a "Connection"when the import is re-entered before theConnectionclass is fully defined. The fix moves the import to the only method that uses it (get_connection_from_secrets), keeping runtime behavior identical while eliminating the import-time recursion.related: #63520
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Opus 4.6 following the guidelines
{pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.