[New-bugs-announce] [issue44343] Adding the "with" statement support to ContextVar

Joongi Kim report at bugs.python.org
Mon Jun 7 22:38:29 EDT 2021


New submission from Joongi Kim <me at daybreaker.info>:

This is just an idea: ContextVar.set() and ContextVar.reset() looks naturally mappable with the "with" statement.

For example:

a = ContextVar('a')
token = a.set(1234)
...
a.reset(token)

could be naturally rewritten as:

a = ContextVar('a')
with a.set(1234):
    ...

Is there any particular reason *not* to do this?
If not, I'd like make a PR to add this API.
Naming suggestions of this API are welcome, but it also seems possible to keep it "set()" if we retain the reference to the ContextVar instance in the Token instance.

----------
components: Library (Lib)
messages: 395302
nosy: achimnol
priority: normal
severity: normal
status: open
title: Adding the "with" statement support to ContextVar
type: enhancement
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44343>
_______________________________________


More information about the New-bugs-announce mailing list