On Thu, Jun 6, 2019 at 8:04 AM Christoph Groth <christ...@grothesque.org> wrote: > > 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. > > I'll do that. Le me just raise one more aspect: for the usage of > context vars as configuration variables (and likely for other uses as > well), it would be useful if there was some way to validate new values > when they are set. This would provide immediate feedback to the user > and would avoid having to check them at every use. > > 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 an exception).
For that you should definitely use a wrapper. Let's use composition instead of complicating the existing APIs. > > (A somewhat related issue would be support for ContextVars in the typing > module.) contextvars should already support this syntax: c: ContextVar[int] = ContextVar('c') Now it's up to mypy et al to support this if they don't already (I haven't checked that.) [..] > > > A final, unrelated comment: I find the use of the word "context" in the > > > standard library for both context managers and context variables (and > > > related to them "contexts") needlessly confusing. (...) > > > > Pull requests to improve the docs are always welcome! > > Sure, I just wanted to double check whether I'm overseeing anything here > or whether both kinds of contexts are indeed completely independent > concepts. They aren't completely independent, as it's quite useful to store context manager's state in a context variable (decimal context is a great example of that). OTOH I can totally understand why people can be confused about this. I would love to improve the docs to lessen the confusion where possible, but we're going to keep using the term for both "context managers" and "context variables" as it's too late to change that. -- Yury _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Code of Conduct: http://python.org/psf/codeofconduct/