test: regression test for #403 (mock.patch.dict + asyncio_mode=auto)#1443
test: regression test for #403 (mock.patch.dict + asyncio_mode=auto)#1443mvanhorn wants to merge 1 commit into
Conversation
…de=auto pytest-dev#403 reported that async tests decorated with unittest.mock.patch.dict were silently skipped under asyncio_mode=auto (PytestUnhandledCoroutine Warning). The root cause was a CPython bug in iscoroutinefunction unwrapping through patch.dict; the fix was backported to CPython 3.10. pytest-asyncio currently requires Python >= 3.10, so the scenario is no longer reachable. This adds a regression test in tests/modes/test_auto_mode.py that asserts both the unmarked auto-mode case and the explicitly-marked case (the issue's test_doesnt_run and test_runs_anyway from the original repro) are collected and pass. Closes pytest-dev#403
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1443 +/- ##
=======================================
Coverage 94.50% 94.50%
=======================================
Files 2 2
Lines 510 510
Branches 62 62
=======================================
Hits 482 482
Misses 22 22
Partials 6 6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks for the initiative, @mvanhorn . I wish I could give a different response, but I don't want to merge the code. The PR adds a test for a bug that occurred in a CPython module of a Python version that is no longer supported by pytest-asyncio. I understand that the issue originally surfaced in pytest-asyncio, but it's not realistic to test every possible interaction with other libraries or modules. Nor is it desired, since test code also needs to be maintained. This particular patch doesn't contribute to test coverage either, so I'm closing this PR. I'm happy to discuss this further and be swayed, if you can convince me otherwise. |
Summary
#403 reported that async tests decorated with
unittest.mock.patch.dictwere silently skipped underasyncio_mode=auto, surfaced as aPytestUnhandledCoroutineWarning. The root cause was a CPython bug in howiscoroutinefunctionunwrappedpatch.dict. seifertm filed python/cpython#98086 and the fix was backported to CPython 3.10 (and 3.11). pytest-asyncio now requires Python >= 3.10, so the scenario is no longer reachable.This PR adds a regression test in
tests/modes/test_auto_mode.pyto lock that property in. The test reproduces the issue's two failing variants -test_doesnt_run(only decorated with@mock.patch.dict) andtest_runs_anyway(also explicitly marked@pytest.mark.asyncio) - and asserts both are collected and pass under--asyncio-mode=auto. If a future refactor reintroduces the unwrapping bug at this layer (e.g. switching back to aiscoroutinefunctioncall that doesn't see throughpatch.dict), this test fails immediately.Files
tests/modes/test_auto_mode.py: newtest_auto_mode_async_test_with_mock_patch_dicttestchangelog.d/403.fixed.rst: news fragmentVerification
Also verified the original repro from the issue body runs cleanly under Python 3.14.3 + current pytest-asyncio main (3/3 passed), confirming the CPython backport is the right reason this test passes today rather than a workaround in this repo.
Closes #403
AI disclosure: regression test and PR body drafted with Claude as a coding assistant; I ran the tests and reviewed the changes before pushing.