seanmuth commented on code in PR #68305:
URL: https://github.com/apache/airflow/pull/68305#discussion_r3404946458
##########
providers/hashicorp/src/airflow/providers/hashicorp/secrets/vault.py:
##########
@@ -237,19 +237,35 @@ def get_connection(self, conn_id: str, team_name: str |
None = None) -> Connecti
:return: A Connection object constructed from Vault data
"""
- # The Connection needs to be locally imported because otherwise we get
into cyclic import
- # problems when instantiating the backend during configuration
- from airflow.models.connection import Connection
+ # Use the compat SDK Connection (Pydantic in Airflow 3, SQLAlchemy in
Airflow 2) to avoid
+ # triggering SQLAlchemy mapper initialization for unrelated models
(e.g. DagModel) in
+ # task-execution subprocesses such as PythonVirtualenvOperator.
+ from airflow.providers.common.compat.sdk import Connection
response = self._get_team_or_global_secret(self.connections_path,
team_name, conn_id)
if response is None:
return None
uri = response.get("conn_uri")
if uri:
- return Connection(conn_id, uri=uri)
+ # from_uri is available on the SDK Connection (Airflow 3.2+ /
task-sdk 1.2.0+).
+ # On Airflow 2 the SQLAlchemy Connection accepts uri= in its
__init__.
+ # On Airflow 3.0/3.1 the attrs-based Connection has neither
from_uri nor a uri=
+ # constructor arg; return None so the secrets-backend loop tries
the next backend.
+ if hasattr(Connection, "from_uri"):
+ return Connection.from_uri(uri, conn_id=conn_id) # type:
ignore[attr-defined]
+ try:
+ return Connection(conn_id=conn_id, uri=uri) # type:
ignore[call-arg]
Review Comment:
Restored `# type: ignore[call-arg]` on the `Connection(conn_id=conn_id,
uri=uri)` line after a mypy CI failure — the attrs mypy plugin replaces the
manually-written `@overload` signatures (which do include `uri=`) with the
attrs-generated `__init__` (which only knows about declared fields). So mypy
never sees `uri=` as valid regardless of what's written in the class body. The
ignore on that specific line is load-bearing.
Drafted-by: Claude Code (Sonnet 4.6); reviewed by @seanmuth before posting
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]