Skip to content

Fix SFTPHookAsync test fixture to return async context managers#66678

Merged
jscheffl merged 1 commit into
apache:mainfrom
potiuk:fix-sftp-async-mock-context-manager
May 11, 2026
Merged

Fix SFTPHookAsync test fixture to return async context managers#66678
jscheffl merged 1 commit into
apache:mainfrom
potiuk:fix-sftp-async-mock-context-manager

Conversation

@potiuk
Copy link
Copy Markdown
Member

@potiuk potiuk commented May 11, 2026

Summary

PR #64465 (SFTP async refactor) changed the production code from
await ssh_conn.start_sftp_client() to
async with ssh_conn.start_sftp_client() as sftp: (same for
sftp.open()), but the sftp_hook_mocked fixture in
providers/sftp/tests/conftest.py was still set up as if those methods
were awaited.

AsyncMock(spec=SSHClientConnection) auto-makes start_sftp_client an
AsyncMock whose call returns a coroutine — not an async context
manager — so every TestSFTPHookAsync test in
providers/sftp/tests/unit/sftp/hooks/test_sftp.py blew up with:

TypeError: 'coroutine' object does not support the asynchronous context manager protocol

In real asyncssh, both SSHClientConnection.start_sftp_client() and
SFTPClient.open() are decorated so a call returns an object that is
both awaitable and async-context-manageable. The production code only
uses the context-manager half. Override the auto-spec'd AsyncMocks with
sync MagicMocks that return a context-manager mock with __aenter__ /
__aexit__ configured.

Test plan

  • All 28 TestSFTPHookAsync tests in
    providers/sftp/tests/unit/sftp/hooks/test_sftp.py pass locally
    against upstream/main.
  • prek run clean on the changed file.

Blocking CI on unrelated PRs since #64465 merged — example failure:
https://github.com/apache/airflow/actions/runs/25638333348/job/75256444992?pr=65840
(PR #65840 only touches docs-theme/breeze; the SFTP failures are
purely fixture rot).


Was generative AI tooling used to co-author this PR?
  • Yes — Claude Opus 4.7 (1M context)

Generated-by: Claude Opus 4.7 (1M context) following the guidelines

After apache#64465 changed the production code from
``sftp = await ssh_conn.start_sftp_client()`` to
``async with ssh_conn.start_sftp_client() as sftp:`` (and likewise for
``sftp.open()``), the ``sftp_hook_mocked`` fixture in
``providers/sftp/tests/conftest.py`` started returning coroutines from
those calls — ``AsyncMock(spec=SSHClientConnection)`` auto-makes
``start_sftp_client`` an ``AsyncMock`` whose call yields a coroutine,
not an async context manager — and every ``TestSFTPHookAsync`` test
that uses the fixture failed with ``TypeError: 'coroutine' object does
not support the asynchronous context manager protocol``.

In real asyncssh, both ``start_sftp_client()`` and ``SFTPClient.open()``
are decorated so a call returns an object that is both awaitable and
async-context-manageable; the production code only uses the
context-manager half. Override the auto-spec'd AsyncMocks with sync
MagicMocks that return a context-manager mock with ``__aenter__`` /
``__aexit__`` configured.

Restores 28 ``TestSFTPHookAsync`` tests in
``providers/sftp/tests/unit/sftp/hooks/test_sftp.py`` (blocking CI on
unrelated PRs since apache#64465 merged).
@potiuk potiuk added the all versions If set, the CI build will be forced to use all versions of Python/K8S/DBs label May 11, 2026
@potiuk potiuk closed this May 11, 2026
@potiuk potiuk reopened this May 11, 2026
@potiuk
Copy link
Copy Markdown
Member Author

potiuk commented May 11, 2026

RE-run with "all versions"

@jscheffl jscheffl merged commit 2c9e759 into apache:main May 11, 2026
356 of 363 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

all versions If set, the CI build will be forced to use all versions of Python/K8S/DBs area:providers provider:sftp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants