On 2/2/2021 8:37 PM, Caleb Donovick wrote:
The discussion around PEP 649 got me thinking about what I believe is the largest downside to PEP 563: the inability to evaluate annotations created with closures.  While this is in general unavoidable,  if the type is ever referenced in an annotated function (including as an annotation) it should be resolvable via `__closure__`.

For example:
```
from __future__ import annotations
import typing
def gen(T):
     def f(x: T):
         y: T = ...
     return f

f = gen(int)
nonlocal_vars = {
   var : cell.cell_contents
   for var, cell in zip(f.__code__.co_freevars, f.__closure__)
}
assert typing.get_type_hints(f, localns=nonlocal_vars)  == {'x': int}
```

I would just open a PR to have `get_type_hints` attempt to resolve closure variables by default.  However, this would require an update to PEP 563 and I don't know what the protocol is there.

Once a PEP is accepted and implemented, the PEP document itself is frozen and not updated. The Python language and docs and the CPython implementation are still subject to what is hopefully further progress.

You are free to open an thread on python-ideas list or an issue on bpo, optionally with a PR, or both. It would be good to reference the PEP and note whether this idea appears there or not. Further discussion of the idea itself would depend on whether is was a rejected alternative, a deferred addition, or just mentioned.

A speculative PR for an idea not yet accepted in principle may be rejected but may help gain acceptance.

PS: I don't use type hints yet and have not much opinion about about your idea (it naively seems plausible), so the above is a generic answer.

--
Terry Jan Reedy

_______________________________________________
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/FQLVHTIMDEI6IPO5FFL2ZIUKZC5RQK4U/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to