Warn on `finally: cancel_scope.cancel()`

Open
#365 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Start by reading the existing async102 and async120 rules and the concerns in Trio issue 455. Identify the rule implementation, tests, and documentation locations from those rules. Done means the linter warns for the shown finally/cancel pattern and the rules documentation links to the relevant discussion if appropriate.

Written by the indexing model from the issue text.

Description

new rule postponed

Like async102 and async120, this is to address the concerns discussed in https://github.com/python-trio/trio/issues/455. (also: should the rules docs link there?)

with CancelScope() as scope:
    try:
        ...  # whatever sync and async code here
    finally:
        scope.cancel()
        # You'd hope this just stops any ongoing work, but it swallows exceptions too!

In most cases the solution is simply to scope.cancel() without the try/finally block - structured concurrency means we reliably clean up on exceptions anyway. For some, the cancel scope is entirely unnecessary, and you could call e.g. recv_chan.close() instead of cancelling.

Dominant language
Python
Stars
26
Forks
10
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-trio/flake8-async

All issues in python-trio/flake8-async

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.