[Python-Dev] PEP 567 v3

Nathaniel Smith njs at pobox.com
Tue Jan 16 20:27:53 EST 2018


On Tue, Jan 16, 2018 at 2:44 PM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> 4. ContextVar.reset(token) now raises a ValueError if the token was
> created in a different Context.

A minor bit of polish: given that Token objects have to track the
associated ContextVar anyway, I think it'd be cleaner if instead of
doing:

token = cvar.set(...)
cvar.reset(token)

we made the API be:

token = cvar.set(...)
token.reset()

In the first version, we use 'cvar' twice, and it's a mandatory
invariant that the same ContextVar object gets used in both places;
you had to add extra code to check this and raise an error if that's
violated. It's level 5 on Rusty's scale
(http://sweng.the-davies.net/Home/rustys-api-design-manifesto)

In the second version, the ContextVar is only mentioned once, so the
invariant is automatically enforced by the API -- you can't even
express the broken version. That's level 10 on Rusty's scale, and
gives a simpler implementation too.

-n

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


More information about the Python-Dev mailing list