Skip to content

Commit b71f036

Browse files
committed
A more elegant is_awaitable (deals with closures).
1 parent ed81f34 commit b71f036

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

umock.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,17 @@
5555
def is_awaitable(obj):
5656
"""
5757
Returns a boolean indication if the passed in obj is an awaitable
58-
function. (MicroPython treats awaitables as generator functions.)
58+
function. (MicroPython treats awaitables as generator functions, and if
59+
the object is a closure containing an async function we need to tread
60+
carefully.)
5961
"""
6062
if is_micropython:
63+
# MicroPython doesn't appear to have a way to determine if a closure is
64+
# an async function except via the repr. This is a bit hacky.
65+
if "<closure <generator>" in repr(obj):
66+
return True
6167
return inspect.isgeneratorfunction(obj)
68+
6269
return inspect.iscoroutinefunction(obj)
6370

6471

0 commit comments

Comments
 (0)