Skip to content

Commit 1a24924

Browse files
committed
[refactor] Introduced new item type "AsyncStaticMethod" which represents a coroutine or async generator to which a @staticmethod decorator was applied.
Pytest unbinds staticmethods, so unbound async staticmethods need special treatment by pytest-asyncio. Signed-off-by: Michael Seifert <[email protected]>
1 parent d837555 commit 1a24924

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pytest_asyncio/plugin.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,28 @@ def from_function(cls, function: pytest.Function, /) -> Self:
373373
)
374374

375375

376+
class AsyncStaticMethod(pytest.Function):
377+
"""
378+
Pytest item that is a coroutine or an asynchronous generator
379+
decorated with staticmethod
380+
"""
381+
382+
@classmethod
383+
def from_function(cls, function: pytest.Function, /) -> Self:
384+
"""
385+
Instantiates an AsyncStaticMethod from the specified pytest.Function item.
386+
"""
387+
return cls.from_parent(
388+
function.parent,
389+
name=function.name,
390+
callspec=getattr(function, "callspec", None),
391+
callobj=function.obj,
392+
fixtureinfo=function._fixtureinfo,
393+
keywords=function.keywords,
394+
originalname=function.originalname,
395+
)
396+
397+
376398
class AsyncHypothesisTest(pytest.Function):
377399
"""
378400
Pytest item that is coroutine or an asynchronous generator decorated by
@@ -438,6 +460,10 @@ def pytest_pycollect_makeitem_convert_async_functions_to_subclass(
438460
for node in node_iterator:
439461
updated_item = node
440462
if isinstance(node, pytest.Function):
463+
if isinstance(obj, staticmethod) and _is_coroutine_or_asyncgen(
464+
obj.__func__
465+
):
466+
updated_item = AsyncStaticMethod.from_function(node)
441467
if _is_coroutine_or_asyncgen(obj):
442468
updated_item = AsyncFunction.from_function(node)
443469
if _is_hypothesis_test(obj) and _hypothesis_test_wraps_coroutine(obj):

0 commit comments

Comments
 (0)