MCPcopy Index your code
hub / github.com/pyscript/pyscript / is_awaitable

Function is_awaitable

core/src/stdlib/pyscript/util.py:55–79  ·  view source on GitHub ↗

Returns a boolean indication if the passed in obj is an awaitable function. This is interpreter agnostic. !!! info MicroPython treats awaitables as generator functions, and if the object is a closure containing an async function or a bound method we need to work

(obj)

Source from the content-addressed store, hash-verified

53
54
55def is_awaitable(obj):
56 """
57 Returns a boolean indication if the passed in obj is an awaitable
58 function. This is interpreter agnostic.
59
60 !!! info
61 MicroPython treats awaitables as generator functions, and if
62 the object is a closure containing an async function or a bound method
63 we need to work carefully.
64 """
65 from pyscript import config
66
67 if config["type"] == "mpy":
68 # MicroPython doesn't appear to have a way to determine if a closure is
69 # an async function except via the repr. This is a bit hacky.
70 r = repr(obj)
71 if "<closure <generator>" in r:
72 return True
73 # Same applies to bound methods.
74 if "<bound_method" in r and "<generator>" in r:
75 return True
76 # In MicroPython, generator functions are awaitable.
77 return inspect.isgeneratorfunction(obj)
78
79 return inspect.iscoroutinefunction(obj)

Callers 3

triggerMethod · 0.90
_create_wrapperFunction · 0.90
_attach_event_handlerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected