[Python-Dev] Re: __future__ annotations loses closure scope

2020-12-08 Thread Guido van Rossum
Great. If you’d like to add something to the docs for get_type_hints() about this behavior please submit a PR — I don’t see a reason this would suddenly change but it’s better to make sure. On Tue, Dec 8, 2020 at 22:29 Paul Bryan wrote: > Rereading PEP 563, I'm clearly railing against it. I beli

[Python-Dev] Re: __future__ annotations loses closure scope

2020-12-08 Thread Paul Bryan
Rereading PEP 563, I'm clearly railing against it. I believe I can work with __annotations__ as you suggest assuming the contract going forward is for get_type_hints to return annotations verbatim if they're not strings or ForwardRefs (the current implementation). Thank you and Gregory for your ti

[Python-Dev] Re: __future__ annotations loses closure scope

2020-12-08 Thread Guido van Rossum
On Tue, Dec 8, 2020 at 8:58 PM Paul Bryan wrote: > My use case is to for type annotations to resolve type encoders, decoders > and validators at runtime. > What is the reason you can't insist that the annotations be globals or at least accessible from there (e.g. class attributes)? If the reason

[Python-Dev] Re: __future__ annotations loses closure scope

2020-12-08 Thread Paul Bryan
Yep, your example, *boom*. My use case is to for type annotations to resolve type encoders, decoders and validators at runtime. Without __future__, type annotations specified in closure scope are correctly attached to class variables, function parameters and return types. Because they're in scope

[Python-Dev] Re: __future__ annotations loses closure scope

2020-12-08 Thread Gregory P. Smith
What is the utility of a type annotation when the thing it refers to cannot exist? Deferred annotation lookups are intended to be something that analysis time can make sense of but can always have no useful meaning at runtime. No nesting required: ``` from __future__ import annotations Class X:

[Python-Dev] Re: __future__ annotations loses closure scope

2020-12-08 Thread Paul Bryan
Let's try an example that static type checkers should have no problem with: Python 3.9.0 (default, Oct 7 2020, 23:09:01) [GCC 10.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from __future__ import annotations >>> >>> def make_a_class(): ... class

[Python-Dev] Re: __future__ annotations loses closure scope

2020-12-08 Thread Guido van Rossum
Yeah, static type checkers won't like it regardless. On Tue, Dec 8, 2020 at 6:39 PM Paul Bryan wrote: > It appears that when from future import __annotations__, a type hint > annotation derived from a closure loses scope. > > Simplistic example: > > Python 3.9.0 (default, Oct 7 2020, 23:09:01)