I would like to add __enter__ and __exit__ methods to contextvars.Context such that these two are mostly equivalent:
contextvars.copy_context().run(do_something_interesting)
and:
with contextvars.copy_context(): do_something_interesting()
This makes it possible to combine Context enter/exit with other context managers:
import contextlib import contextvars @contextlib.contextmanager def foo(): with contextvars.copy_context(): set_some_context_vars() yield
My personal motivating interest is controlling ContextVar values in pytest fixtures. For example:
import contextvars import pytest import module_under_test @pytest.fixture def clean_slate(): with contextvars.copy_context(): module_under_test._internal_state_context_var.set(None) yield def test_foo(clean_slate): assert not module_under_test.is_initialized()
https://stackoverflow.com/q/59264158
pythongh-99633: Add context manager support to `contextvars.Context`
The idea should probably be reviewed by @1st1.
Friendly ping @1st1
+1
Maybe someone could contribute a PR?
pythongh-99633: Inline independent coroutine check
pythongh-99633: _contextvars: Change private types to reduce casting
pythongh-99633: _contextvars: Clarify that the ref is stolen, not bor…
pythongh-99633: _contextvars: Enter the thread's initial context
pythongh-99633: _contextvars: Call non-tstate variants
pythongh-99633: New generator/coroutine `_context` property
pythongh-124872: Replace enter/exit events with "switched"
gh-124872: Replace enter/exit events with "switched" (#124776)
pythongh-124872: Replace enter/exit events with "switched" (python#12…
gh-124872: Replace enter/exit events with "switched" (#125532)
Activity
pythongh-99633: Add context manager support to `contextvars.Context`
gvanrossum commented on Nov 21, 2022
The idea should probably be reviewed by @1st1.
pythongh-99633: Add context manager support to `contextvars.Context`
pythongh-99633: Add context manager support to `contextvars.Context`
rhansen commented on Feb 2, 2023
Friendly ping @1st1
apostolos-geyer commented on May 26, 2024
+1
gvanrossum commented on May 26, 2024
Maybe someone could contribute a PR?
pythongh-99633: Add context manager support to `contextvars.Context`
44 remaining items
pythongh-99633: Inline independent coroutine check
pythongh-99633: Add context manager support to `contextvars.Context`
pythongh-99633: _contextvars: Change private types to reduce casting
pythongh-99633: _contextvars: Clarify that the ref is stolen, not bor…
pythongh-99633: _contextvars: Enter the thread's initial context
pythongh-99633: _contextvars: Call non-tstate variants
pythongh-99633: New generator/coroutine `_context` property
pythongh-99633: Inline independent coroutine check
pythongh-99633: Add context manager support to `contextvars.Context`
pythongh-124872: Replace enter/exit events with "switched"
pythongh-124872: Replace enter/exit events with "switched"
gh-124872: Replace enter/exit events with "switched" (#124776)
pythongh-124872: Replace enter/exit events with "switched" (python#12…
gh-124872: Replace enter/exit events with "switched" (#125532)
pythongh-124872: Replace enter/exit events with "switched" (python#12…