[Python-Dev] PEP 567 v2

Nathaniel Smith njs at pobox.com
Thu Jan 4 22:58:49 EST 2018


On Thu, Jan 4, 2018 at 6:09 PM, Guido van Rossum <guido at python.org> wrote:
> However you've not convinced me that it would be better to make Context
> implement the full MutableMapping interface (with `__delitem__` always
> raising). There are use cases for inspecting Context, e.g. a debugger that
> wants to print the Context for some or all suspended tasks. But I don't see
> a use case for mutating a Context object that's not the current context, and
> when it is the current context, ContextVar.set() is more direct. I also
> don't see use cases for other MutableMapping methods like pop() or update().
> (And clear() falls under the same prohibition as __delitem__().)

I was looking at this again, and I realized there's some confusion.
I've been repeating the thing about not wanting to implement
__delitem__ too, but actually, I think __delitem__ is not the problem
:-).

The problem is that we don't want to provide ContextVar.unset() --
that's the operation that adds complication in a PEP 550 world. If you
have a stack of Contexts that ContextVar.get() searches, and set/unset
are only allowed to mutate the top entry in the stack, then the only
way to implement unset() is to do something like
context_stack[-1][self] = _MISSING, so it can hide any entries below
it in the stack. This is extra complication for a feature that it's
not clear anyone cares about. (And if it turns out people do care, we
could add it later.)

Deleting entries from individual Context objects shouldn't create
conceptual problems. OTOH I don't see how it's useful either. I don't
think implementing MutableMapping would actually cause problems, but
it's a bunch of extra code to write/test/maintain without any clear
use cases.

This does make me think that I should write up a short PEP for
extending PEP 567 to add context lookup, PEP 550 style: it can start
out in Status: deferred and then we can debate it properly before 3.8,
but at least having the roadmap written down now would make it easier
to catch these details. (And it might also help address Paul's
reasonable complaint about "unstated requirements".)

-n

-- 
Nathaniel J. Smith -- https://vorpus.org


More information about the Python-Dev mailing list