Skip to content

PY-ASYN0002 · task_group_candidate

Detect an async function manually pairing created tasks with gather.

This is a deterministic rule for python. Read its implementation.

Within each async function, resolve asyncio.create_task or ensure_future together with an awaited or returned asyncio.gather whose return_exceptions is not true. Exclude functions already constructing asyncio.TaskGroup. The Boolean result identifies a candidate function.

Findings identify the function and report syntactic task-creation and gather counts. The rule does not claim semantic equivalence.

gather deliberately allows siblings to continue after one ordinary exception, while TaskGroup cancels remaining siblings and raises an exception group. Keep gather when that behavior, result ordering, partial success, or explicit cancellation protocol is required. Calls using return_exceptions=True are excluded. Review exception and cancellation contracts before changing code, so no automatic fix is offered.

Creating two tasks, gathering them, and manually canceling unfinished tasks is reported as a structured-concurrency candidate. Directly gathering independent coroutines without first creating tasks is accepted. Code already using async with TaskGroup() is accepted.

  • Cites “The Python Standard Library”, coroutine and TaskGroup. Open reference
  • Cites “The Python Standard Library”, gather. Open reference