mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
Issue #24400: Resurrect inspect.isawaitable()
collections.abc.Awaitable and collections.abc.Coroutine no longer use __instancecheck__ hook to detect generator-based coroutines. inspect.isawaitable() can be used to detect generator-based coroutines and to distinguish them from regular generator objects.
This commit is contained in:
parent
2ab5b092e5
commit
fdbeb2b4b6
10 changed files with 92 additions and 39 deletions
|
|
@ -151,6 +151,29 @@ class TestPredicates(IsTestBase):
|
|||
|
||||
coro.close(); gen_coro.close() # silence warnings
|
||||
|
||||
def test_isawaitable(self):
|
||||
def gen(): yield
|
||||
self.assertFalse(inspect.isawaitable(gen()))
|
||||
|
||||
coro = coroutine_function_example(1)
|
||||
gen_coro = gen_coroutine_function_example(1)
|
||||
|
||||
self.assertTrue(inspect.isawaitable(coro))
|
||||
self.assertTrue(inspect.isawaitable(gen_coro))
|
||||
|
||||
class Future:
|
||||
def __await__():
|
||||
pass
|
||||
self.assertTrue(inspect.isawaitable(Future()))
|
||||
self.assertFalse(inspect.isawaitable(Future))
|
||||
|
||||
class NotFuture: pass
|
||||
not_fut = NotFuture()
|
||||
not_fut.__await__ = lambda: None
|
||||
self.assertFalse(inspect.isawaitable(not_fut))
|
||||
|
||||
coro.close(); gen_coro.close() # silence warnings
|
||||
|
||||
def test_isroutine(self):
|
||||
self.assertTrue(inspect.isroutine(mod.spam))
|
||||
self.assertTrue(inspect.isroutine([].count))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue