SameerMesiah97 commented on code in PR #69140:
URL: https://github.com/apache/airflow/pull/69140#discussion_r3500996009
##########
providers/common/compat/src/airflow/providers/common/compat/connection/__init__.py:
##########
@@ -29,20 +29,24 @@
log = logging.getLogger(__name__)
-async def get_async_connection(conn_id: str) -> Connection:
+async def get_async_connection(conn_id: str, hook: type[BaseHook] | None =
None) -> Connection:
"""
Get an asynchronous Airflow connection that is backwards compatible.
:param conn_id: The provided connection ID.
+ :param hook: Hook (class or instance) to resolve the connection through,
so a
+ subclass override of ``aget_connection``/``get_connection`` is honored.
+ Defaults to ``BaseHook``.
:returns: Connection
"""
from asgiref.sync import sync_to_async
- if hasattr(BaseHook, "aget_connection"):
+ hook = hook or BaseHook
+ if hasattr(hook, "aget_connection"):
log.debug("Get connection using `BaseHook.aget_connection().")
- return await BaseHook.aget_connection(conn_id=conn_id)
+ return await hook.aget_connection(conn_id=conn_id)
Review Comment:
Is the log message accurate now? Why not do something like this:
`log.debug("Get connection using `%s.aget_connection()`.", hook.__name__)`
Same applies to the `sync_to_async` branch.
##########
providers/common/compat/src/airflow/providers/common/compat/connection/__init__.py:
##########
@@ -29,20 +29,24 @@
log = logging.getLogger(__name__)
-async def get_async_connection(conn_id: str) -> Connection:
+async def get_async_connection(conn_id: str, hook: type[BaseHook] | None =
None) -> Connection:
Review Comment:
I would widen the typing for the hook parameter to this as I believe you
want to include instances:
`BaseHook | type[BaseHook] | None`
##########
providers/common/compat/tests/unit/common/compat/connection/test_connection.py:
##########
@@ -67,3 +67,14 @@ async def
test_get_async_connection_with_get_connection(self, _, caplog):
assert conn.password == "secret_token"
assert conn.conn_type == "http"
assert "Get connection using `BaseHook.get_connection()." in
caplog.text
+
+ @mock.patch("airflow.providers.common.compat.connection.BaseHook",
new_callable=MockAgetBaseHook)
+ @pytest.mark.asyncio
+ async def test_get_async_connection_honors_passed_hook(self, _):
+ class OverrideHook:
+ @classmethod
+ async def aget_connection(cls, conn_id: str):
+ return Connection(conn_id="override", conn_type="http",
password="override_token")
+
+ conn = await get_async_connection("test_conn", hook=OverrideHook)
+ assert conn.password == "override_token"
Review Comment:
Might be worth adding a test for `get_connection` as well:
```
@mock.patch("airflow.providers.common.compat.connection.BaseHook",
new_callable=MockBaseHook)
@pytest.mark.asyncio
async def test_get_async_connection_honors_passed_hook_get_connection(self,
_):
class OverrideHook:
@classmethod
def get_connection(cls, conn_id: str):
return Connection(
conn_id="override",
conn_type="http",
password="override_token",
)
conn = await get_async_connection("test_conn", hook=OverrideHook)
assert conn.password == "override_token"
```
##########
providers/http/src/airflow/providers/http/hooks/http.py:
##########
@@ -615,7 +615,7 @@ async def config(self) -> SessionConfig:
extra_options: dict[str, Any] = {}
if self.http_conn_id:
- conn = await get_async_connection(conn_id=self.http_conn_id)
+ conn = await get_async_connection(conn_id=self.http_conn_id,
hook=type(self))
Review Comment:
Is there a reason to pass `type(self)` rather than `self`? The helper
appears to support either, and passing the instance would preserve any
instance-specific behavior.
--
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]