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()
This is a minor feature, which does not need previous discussion elsewhere
No response
#129888 provides a simple patch for the proposed functionality.
gh-129889: Support context manager protocol by contextvars.Token (#12…
Related but bigger: #99633
Activity
asvetlov commented on Feb 9, 2025
#129888 provides a simple patch for the proposed functionality.
gh-129889: Support context manager protocol by contextvars.Token (#12…
cjw296 commented on May 6, 2025
Related but bigger: #99633