summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorCarlton Gibson <carlton@noumenal.es>2022-12-18 19:13:24 (GMT)
committerGitHub <noreply@github.com>2022-12-18 19:13:24 (GMT)
commit532aa4e4e019812d0388920768ede7c04232ebe1 (patch)
treebac6a6c3f467bc6a7d7ac1210fc5f9929cc2742a /Lib/test
parent1cf3d78c92eb07dc09d15cc2e773b0b1b9436825 (diff)
downloadcpython-532aa4e4e019812d0388920768ede7c04232ebe1.zip
cpython-532aa4e4e019812d0388920768ede7c04232ebe1.tar.gz
cpython-532aa4e4e019812d0388920768ede7c04232ebe1.tar.bz2
gh-94912: Added marker for non-standard coroutine function detection (#99247)
This introduces a new decorator `@inspect.markcoroutinefunction`, which, applied to a sync function, makes it appear async to `inspect.iscoroutinefunction()`.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_inspect.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 0f8217b..78e6e9e 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -202,6 +202,51 @@ class TestPredicates(IsTestBase):
gen_coroutine_function_example))))
self.assertTrue(inspect.isgenerator(gen_coro))
+ async def _fn3():
+ pass
+
+ @inspect.markcoroutinefunction
+ def fn3():
+ return _fn3()
+
+ self.assertTrue(inspect.iscoroutinefunction(fn3))
+ self.assertTrue(
+ inspect.iscoroutinefunction(
+ inspect.markcoroutinefunction(lambda: _fn3())
+ )
+ )
+
+ class Cl:
+ async def __call__(self):
+ pass
+
+ self.assertFalse(inspect.iscoroutinefunction(Cl))
+ # instances with async def __call__ are NOT recognised.
+ self.assertFalse(inspect.iscoroutinefunction(Cl()))
+
+ class Cl2:
+ @inspect.markcoroutinefunction
+ def __call__(self):
+ pass
+
+ self.assertFalse(inspect.iscoroutinefunction(Cl2))
+ # instances with marked __call__ are NOT recognised.
+ self.assertFalse(inspect.iscoroutinefunction(Cl2()))
+
+ class Cl3:
+ @inspect.markcoroutinefunction
+ @classmethod
+ def do_something_classy(cls):
+ pass
+
+ @inspect.markcoroutinefunction
+ @staticmethod
+ def do_something_static():
+ pass
+
+ self.assertTrue(inspect.iscoroutinefunction(Cl3.do_something_classy))
+ self.assertTrue(inspect.iscoroutinefunction(Cl3.do_something_static))
+
self.assertFalse(
inspect.iscoroutinefunction(unittest.mock.Mock()))
self.assertTrue(