python-trio / python-trio/trio

use case for deriving from CancelScope

Open
#2,359 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.3k
Forks
431
Avg merge
2d 17h
Merged PRs (30d)
6

Description

CancelScope is a useful starting point for extension, so I'm wondering if the Final guard could be removed.

trio-util has move_on_when(). Since it yields a CancelScope, shield and deadline can be used in addition to its ability to cancel when the given async function returns. But like shield and deadline, I'd like the possibility to change the wait function dynamically.

async with move_on_when(my_event.wait) as cancel_scope:
    cancel_scope.shield = True
    await do_something()
    cancel_scope.wait_fn = None  # disable cancel by my_event
    await do_other_things()

It's not possible to extend CancelScope this way due to the meta=Final subclassing guard.

alternatives to subclassing

  • parent the cancel scope in some some other thing - move_on_when() returns MoveOnWhenContext, which has wait_fn and (similar to nursery) cancel_scope fields.
  • wrap CancelScope - make a MoveOnWhenScope that has the same API as CancelScope, plus wait_fn.

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.

Research direction

Start by reviewing CancelScope's meta=Final subclassing guard and the trio-util move_on_when() example described in the issue. Compare the proposed subclassing, parenting, and wrapping alternatives, then document or implement a decided approach that supports the requested dynamic wait-function behavior without breaking the existing CancelScope API.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.