On Tue, Dec 8, 2020 at 8:58 PM Paul Bryan <pbr...@anode.ca> 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 is that
they're dynamically computed, maybe you can dynamically compute the
annotation (effectively overruling the "future" semantics)?
`__annotations__` is a mutable attribute so there's nothing to stop you
from writing e.g.

def make_foo(symbol):
    def foo(arg: symbol): ...
    foo.__annotations__['arg'] = symbol  # Or you could do e.g. this in a
decorator
    return foo


> Without __future__, type annotations specified in closure scope are
> correctly attached to class variables, function parameters and return
> types. Because they're in scope at the time they're evaluated.
>
> __future__ annotations breaks because the hint is not evaluated until
> get_type_hints is called, which is too late; the scope is lost.
>
> Instead of just storing an annotation as a string, how about storing an
> object containing the string, plus the local scope? Then get_type_hint
> could be made to successfully resolve it.
>

That would be a problem because it would keep everything in the local scope
alive forever (since perhaps the annotation is never used).

There may be a reason why my suggestion won't work either. If that's so,
please show a realistic example that captures the essence of your problem,
so we won't keep going back and forth attacking each other's toy examples.

Also please understand that this behavior is prescribed by an accepted PEP
and has been around since 3.7, so the bar to change this is very high.
(It's not the default behavior until 3.10 though, so theoretically there
might be a little bit more wiggle room for that. So far you haven't shown
sufficient evidence to consider that though.)

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/DMHM5BTBITZK26NJ2SZOOCYYK7AFWXFC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to