Skip to content

Support context manager protocol by contextvars.Token #129889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
asvetlov opened this issue Feb 9, 2025 · 1 comment · Fixed by #129888
Closed

Support context manager protocol by contextvars.Token #129889

asvetlov opened this issue Feb 9, 2025 · 1 comment · Fixed by #129888
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@asvetlov
Copy link
Contributor

asvetlov commented Feb 9, 2025

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

@asvetlov
Copy link
Contributor Author

asvetlov commented Feb 9, 2025

#129888 provides a simple patch for the proposed functionality.

@picnixz picnixz added extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) and removed extension-modules C modules in the Modules dir labels Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants