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)
| 53 | |
| 54 | |
| 55 | def 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) |
no outgoing calls
no test coverage detected