Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
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:
withcontextvars.copy_context(): do_something_interesting()Pitch
This makes it possible to combine Context enter/exit with other context managers:
importcontextlibimportcontextvars@contextlib.contextmanagerdeffoo(): withcontextvars.copy_context(): set_some_context_vars() yieldMy personal motivating interest is controlling ContextVar values in pytest fixtures. For example:
importcontextvarsimportpytestimportmodule_under_test@pytest.fixturedefclean_slate(): withcontextvars.copy_context(): module_under_test._internal_state_context_var.set(None) yielddeftest_foo(clean_slate): assertnotmodule_under_test.is_initialized()Previous discussion
https://stackoverflow.com/q/59264158
Linked PRs
leofang, nl0, heckad, dtatarkin, Atry and 7 more
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement