[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-10 Thread Yonatan Zunger
On Thu, Jun 6, 2019 at 4:15 AM Christoph Groth wrote: > Yonatan Zunger wrote: > > > I had a similar recent need, with a bit more on top of it, and solved it > > with this slightly insane library. (Alas, I haven't figured out a good > way > > to make it act as a true subtype of UnderlyingType

[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-06 Thread Christoph Groth
Yury Selivanov wrote: > On Thu, Jun 6, 2019 at 5:20 PM Christoph Groth > wrote: > [..] > > The other possibility I see would be wrapping ContextVar inside a > > class (like in the example module that I attached to the first post > > in this thread), but that's not a good solution. It requires >

[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-06 Thread Yury Selivanov
On Thu, Jun 6, 2019 at 5:20 PM Christoph Groth wrote: [..] > The other possibility I see would be wrapping ContextVar inside a class > (like in the example module that I attached to the first post in this > thread), but that's not a good solution. It requires replicating the > complete API, and

[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-06 Thread Christoph Groth
Yury Selivanov wrote: > Christoph Groth wrote: > > Did you consider anything like that? For example, ContextVar could > > accept an optional keyword arg 'validate' that must be a function > > that is then called for each new value, and somehow reports problems > > (by return value or by raising

[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-06 Thread Christoph Groth
Yury Selivanov wrote: > I suggest you to open an issue on bugs.python.org to implement support > for context manager protocol for contextvars.Token. I'm not opposed > to the idea. Keep in mind that Python 3.8 is already in a feature > freeze mode, so the earliest we can get this is Python 3.9.

[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-06 Thread Christoph Groth
Yonatan Zunger wrote: > I had a similar recent need, with a bit more on top of it, and solved it > with this slightly insane library. (Alas, I haven't figured out a good way > to make it act as a true subtype of UnderlyingType yet) > > (...) To me, your trick seems to address a different

[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-05 Thread Yonatan Zunger
I had a similar recent need, with a bit more on top of it, and solved it with this slightly insane library. (Alas, I haven't figured out a good way to make it act as a true subtype of UnderlyingType yet) import contextvars from typing import Any, Generic, TypeVar UnderlyingType =

[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-05 Thread Yury Selivanov
On Wed, Jun 5, 2019 at 6:22 PM Christoph Groth wrote: [..] > I'm aware of this possibility, however it is not suitable for the use > case that I have in mind (configuration variables), because it requires > too much code for each variable. > > Of course one could wrap your example inside a

[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-05 Thread Christoph Groth
Hi Yury, thanks for the quick reply. Yury Selivanov wrote: > Adding context manager protocol support to contextvars.Token was > considered when PEP 567 was discussed. There wasn't a strong argument > against that; however we decided not to immediately add it because > context variables is a

[Python-ideas] Re: Adding context manager interface to contextvars.Token

2019-06-05 Thread Yury Selivanov
Hi Christoph, Adding context manager protocol support to contextvars.Token was considered when PEP 567 was discussed. There wasn't a strong argument against that; however we decided not to immediately add it because context variables is a relatively low-level API. In you case, you can simply