Detect if a block of code needs to be wrapped in an `async def` If the code block has a top-level return statement or is otherwise invalid, `False` will be returned.
(cell: str)
| 136 | |
| 137 | |
| 138 | def _should_be_async(cell: str) -> bool: |
| 139 | """Detect if a block of code needs to be wrapped in an `async def` |
| 140 | |
| 141 | If the code block has a top-level return statement or is otherwise |
| 142 | invalid, `False` will be returned. |
| 143 | """ |
| 144 | try: |
| 145 | code = compile( |
| 146 | cell, "<>", "exec", flags=getattr(ast, "PyCF_ALLOW_TOP_LEVEL_AWAIT", 0x0) |
| 147 | ) |
| 148 | return inspect.CO_COROUTINE & code.co_flags == inspect.CO_COROUTINE |
| 149 | except (SyntaxError, ValueError, MemoryError): |
| 150 | return False |
no outgoing calls
searching dependent graphs…