Skip to content

Support context manager protocol by contextvars.Token #129889

Description

@asvetlov

Feature or enhancement

Proposal:

Sometimes, mostly in tests, I write something like

from contextvars import ContextVar
var = ContextVar('var')

def test_a():
    token = var.set('new val')
    do_stuff()
    var.reset(token)

It looks a little cumbersome, the support for with var: would be awesome:

def test_b():
    with var.set('new val'):
        do_stuff()

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions