This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Adding the "with" statement support to ContextVar
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: achimnol, aeros, corona10, njs, yselivanov
Priority: normal Keywords:

Created on 2021-06-08 02:38 by achimnol, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg395302 - (view) Author: Joongi Kim (achimnol) * Date: 2021-06-08 02:38
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.
msg395397 - (view) Author: Joongi Kim (achimnol) * Date: 2021-06-09 08:46
After checking out PEP-567 (https://www.python.org/dev/peps/pep-0567/),
I'm adding njs to the nosy list.
History
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88509
2021-06-09 08:46:39achimnolsetnosy: + njs
messages: + msg395397
2021-06-08 02:43:56corona10setnosy: + yselivanov, aeros
2021-06-08 02:41:32corona10setnosy: + corona10
2021-06-08 02:38:29achimnolcreate