(original) (raw)

On Fri, Jan 5, 2018 at 2:41 AM, Paul Moore <p.f.moore@gmail.com> wrote:
On 4 January 2018 at 23:58, Guido van Rossum <guido@python.org> wrote:
\> Do you have a specific proposal for a wording change? PEP 567 describes
\> Context as "a read-only mapping, implemented using an immutable dictionary."
\> This sounds all right to me -- "read-only" is weaker than "immutable". Maybe
\> the implementation should not be mentioned here? (The crux here is that a
\> given Context acts as a variable referencing an immutable dict -- but it may
\> reference different immutable dicts at different times.)

I've been struggling to think of good alternative wordings (it's a
case of "I'm not sure what you're trying to say, so I can't work out
how you should say it", unfortunately). The best I can come up with is

"""
A Context is a mapping from ContextVar objects to their values. The
Context itself exposes the Mapping interface, so cannot be modified
directly - to modify the value associated with a variable you need to
use the ContextVar.set() method.
"""

This is clear, but IMO there's one important detail missing: using ContextVar.set() you can only modify the current Context.

This part of the PEP (in particular the definition of ContextVar.set() on line 90) also lies about what ContextVar.set() does -- it implies that Context is immutable. If it was, the recommended use of Context.run() in asyncio would make no sense, since run() clearly modifies the Context object in place. This is not an implementation detail -- it is an API detail that affects how frameworks should use Context objects.

Re-reading, there's a lot of language in this part of the PEP that needs updating... :-(
Does that explain things correctly? One thing I am sure of is that we
should remove "implemented using an immutable dictionary" - it's an
implementation detail, and adds nothing but confusion to mention it
here.

Agreed.

--
--Guido van Rossum (python.org/\~guido)