fix: "The test […] is not an async function" when asyncio marker is#1379
fix: "The test […] is not an async function" when asyncio marker is#1379JiwaniZakir wants to merge 2 commits intopytest-dev:mainfrom
asyncio marker is#1379Conversation
…modifyitems Add a trylast pytest_collection_modifyitems hook that converts Function items with a dynamically added asyncio marker to PytestAsyncioFunction.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1379 +/- ##
==========================================
- Coverage 93.64% 93.26% -0.38%
==========================================
Files 2 2
Lines 409 416 +7
Branches 44 47 +3
==========================================
+ Hits 383 388 +5
- Misses 20 21 +1
- Partials 6 7 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| ): | ||
| specialized_item_class = PytestAsyncioFunction.item_subclass_for(item) | ||
| if specialized_item_class: | ||
| items[i] = specialized_item_class._from_function(item) |
There was a problem hiding this comment.
I think this will override items in a way that's breaking and could conflict with, e.g., other pytest plugins.
|
Good point -- I'll rework the implementation to avoid overriding items directly and instead use a more targeted approach that won't conflict with other plugins. |
I appreciate you might be busy with 60-ish PRs you've opened on GitHub since this one, but ideally you'd include a test that fails on main but passes on this merging branch. 😊 |
|
Fair point -- I'll add a regression test that demonstrates the fix before updating the implementation. |
Add test_asyncio_mark_added_via_collection_modifyitems_is_recognized that fails on main (pytest raises 'The test is not an async function') but passes on this branch after the pytest_collection_modifyitems hook is in place. Reproduces the exact pattern from the issue: a user conftest that uses pytest_collection_modifyitems + inspect.iscoroutinefunction to add the asyncio marker to async test functions in strict mode.
|
Added a regression test in 5129c8e that reproduces the exact pattern from issue #810 — a user conftest using I looked again at the |
|
@JiwaniZakir I haven't thought much about the issue you're trying to solve, but it seems non-trivial at first glance. |
Fixes #810
When the
asynciomarker is added to a test item viapytest_collection_modifyitemsin user plugins, pytest-asyncio would raise "The test is not an async function" becausepytest_pycollect_makeitem_convert_async_functions_to_subclasshad already run and did not see the marker at collection time. The root cause was that asyncFunctionitems marked after collection were never converted toPytestAsyncioFunctionsubclasses. Adds apytest_collection_modifyitemshook inpytest_asyncio/plugin.pywithtrylast=Truethat iterates collected items and callsPytestAsyncioFunction.item_subclass_forand_from_functionto perform the conversion for anyFunctioncarrying anasynciomarker that was not already specialized. Verified by the existing test suite covering thepytest_collection_modifyitemsmarker injection scenario introduced alongside this fix.