@@ -356,6 +356,12 @@ async def setup():
356
356
class AsyncFunction (pytest .Function ):
357
357
"""Pytest item that is a coroutine or an asynchronous generator"""
358
358
359
+ @staticmethod
360
+ def can_substitute (item : pytest .Function ) -> bool :
361
+ """Returns whether the specified function can be replaced by this class"""
362
+ func = item .obj
363
+ return _is_coroutine_or_asyncgen (func )
364
+
359
365
@classmethod
360
366
def from_function (cls , function : pytest .Function , / ) -> Self :
361
367
"""
@@ -386,6 +392,14 @@ class AsyncStaticMethod(pytest.Function):
386
392
decorated with staticmethod
387
393
"""
388
394
395
+ @staticmethod
396
+ def can_substitute (item : pytest .Function ) -> bool :
397
+ """Returns whether the specified function can be replaced by this class"""
398
+ func = item .obj
399
+ return isinstance (func , staticmethod ) and _is_coroutine_or_asyncgen (
400
+ func .__func__
401
+ )
402
+
389
403
@classmethod
390
404
def from_function (cls , function : pytest .Function , / ) -> Self :
391
405
"""
@@ -416,6 +430,12 @@ class AsyncHypothesisTest(pytest.Function):
416
430
@hypothesis.given.
417
431
"""
418
432
433
+ @staticmethod
434
+ def can_substitute (item : pytest .Function ) -> bool :
435
+ """Returns whether the specified function can be replaced by this class"""
436
+ func = item .obj
437
+ return _is_hypothesis_test (func ) and _hypothesis_test_wraps_coroutine (func )
438
+
419
439
@classmethod
420
440
def from_function (cls , function : pytest .Function , / ) -> Self :
421
441
"""
@@ -483,13 +503,11 @@ def pytest_pycollect_makeitem_convert_async_functions_to_subclass(
483
503
for node in node_iterator :
484
504
updated_item = node
485
505
if isinstance (node , pytest .Function ):
486
- if isinstance (obj , staticmethod ) and _is_coroutine_or_asyncgen (
487
- obj .__func__
488
- ):
506
+ if AsyncStaticMethod .can_substitute (node ):
489
507
updated_item = AsyncStaticMethod .from_function (node )
490
- if _is_coroutine_or_asyncgen ( obj ):
508
+ if AsyncFunction . can_substitute ( node ):
491
509
updated_item = AsyncFunction .from_function (node )
492
- if _is_hypothesis_test ( obj ) and _hypothesis_test_wraps_coroutine ( obj ):
510
+ if AsyncHypothesisTest . can_substitute ( node ):
493
511
updated_item = AsyncHypothesisTest .from_function (node )
494
512
updated_node_collection .append (updated_item )
495
513
0 commit comments