[Python-Dev] PEP 563: Postponed Evaluation of Annotations

Nick Coghlan ncoghlan at gmail.com
Wed Nov 8 20:49:01 EST 2017


On 8 November 2017 at 16:24, Guido van Rossum <guido at python.org> wrote:
> I also don't like the idea that there's nothing you can do with a thunk
> besides calling it -- you can't meaningfully introspect it (not without
> building your own bytecode interpreter anyway).

Wait, that wasn't what I was suggesting at all - with thunks exposing
their code object the same way a function does (i.e. as a `__code__`
attribute), the introspection functions in `dis` would still work on
them, so you'd be able to look at things like which variable names
they referenced, thus granting the caller complete control over *how*
they resolved those variable names (by setting them in the local
namespace passed to the call).

This is why they'd have interesting potential future use cases as
general purpose callbacks - every local, nonlocal, global, and builtin
name reference would implicitly be an optional parameter (or a
required parameter if the name couldn't be resolved as a nonlocal,
global, or builtin).

> Using an AST instead of a string is also undesirable -- the AST changes in
> each release, and the usual strong compatibility guarantees don't apply
> here. And how are you going to do anything with it? If you've got a string
> and you want an AST node, it's one call away. But if you've got an AST node
> and you want either a string *or* the object to which that string would
> evaluate, you've got a lot of work to do. Plus the AST takes up a lot more
> space than the string, and we don't have a way to put an AST in a bytecode
> file. (And as Inada-san pointed out a thunk *also* takes up more space than
> a string.)
>
> Nick, please don't try to save the thunk proposal by carefully dissecting
> every one of my objections. That will just prolong its demise.

Just the one objection, since you seem to be rejecting something I
didn't suggest (i.e. adding an opaque callable type that the dis and
inspect modules didn't understand). I agree that would be a bad idea,
but it's also not what I was suggesting we do.

Instead, thunks would offer all the same introspection features as
lambda expressions do, they'd just differ in the following ways:

* the parameter list on their code objects would always be empty
* the parameter list for their __call__ method would always be "ns=None"
* they'd be compiled without CO_OPTIMIZED (the same as a class namespace)
* they'd look up their closure references using LOAD_CLASSDEREF (the
same as a class namespace)

That said, even without a full-fledged thunk based solution to
handling lexical scoping I think there's a way to resolve the nested
class problem in PEP 563 that works for both explicitly and implicitly
quoted strings, while still leaving the door open to replacing
implicitly quoted strings with thunks at a later date: stating that
*if* users want such nested references to be resolvable at runtime,
they need to inject a runtime reference to the outermost class into
the inner class namespace.

That is, if you want to take:

    class C:
        field = 1
        class D:
            def method(a: C.field):
                ...

and move it inside a function, that would actually look like:

    def f():
        class C:
            field = 1
            class D:
                def method(a: C.field):
                    ...
        C.D.C = C # Make annotations work at runtime
        return f

That leaves the door open to a future PEP that proposes thunk-based
annotations as part of proposing thunks as a new low level delayed
evaluation primitive.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list