flake8-async is a highly opinionated Flake8 plugin designed to identify common problems, potential bugs, dead code, performance issues, and idiom violations specific to asynchronous programming with Trio, AnyIO, and asyncio. It is actively maintained, with frequent releases following a CalVer (YY.month.patch) scheme. The plugin integrates with the standard `flake8` tool and some of its checks are also incorporated into the `ruff` linter.
pip install flake8-asyncNo compatibility data collected yet for this library.
To use flake8-async, install it and then run the `flake8` command on your Python files. The plugin will automatically detect and report issues. This example demonstrates a `busy_wait_function` that would trigger the `ASYNC110` warning, which suggests replacing `await trio.sleep()` in a `while` loop with an event-based mechanism for better efficiency.
Update all `TRIOxxx` error codes to their `ASYNCxxx` equivalents in your `flake8` configuration files or inline `# noqa` comments. Ensure you are installing `flake8-async`.
Install the library with the `flake8` extra: `pip install "flake8-async[flake8]"`.
Ensure that any `fail_after` or `move_on_after` blocks contain at least one `await` expression. If the block genuinely requires no `await`, consider if the timeout is truly necessary or refactor to include an `await` that enables cancellation.
Replace blocking calls with their asynchronous equivalents (e.g., `aiofiles` for file I/O, `httpx.AsyncClient` for HTTP requests) or offload them to a thread pool using functions like `trio.to_thread.run_sync()`, `anyio.to_thread.run_sync()`, or `asyncio.loop.run_in_executor()`.
Ensure that the `with` block includes at least one `await` call. For example, add an `await trio.sleep(0)` if a checkpoint is needed without blocking for time.
Refactor the code to use an event object (e.g., `trio.Event`, `anyio.Event`, `asyncio.Event`). Instead of `while not condition: await trio.sleep(...)`, use `await event.wait()` to pause until the condition is met efficiently.
Update all instances of `TRIOxxx` error codes (e.g., in `pyproject.toml`, `.flake8` config, or inline `# noqa` comments) to their new `ASYNCxxx` counterparts. Consult the `flake8-async` documentation for the mapping if needed.