[Python-Dev] Postponed annotations break inspection of dataclasses

David Hagen david at drhagen.com
Sun Sep 23 06:40:29 EDT 2018


On Sat, Sep 22, 2018 at 3:11 PM Guido van Rossum <guido at python.org> wrote:
> Still, I wonder if there's a tweak possible of the globals and locals
used when exec()'ing the function definitions in dataclasses.py, so that
get_type_hints() gets the right globals for this use case.

On Sat, Sep 22, 2018 at 4:38 PM Yury Selivanov <yselivanov.ml at gmail.com>
wrote:
> If it's possible to fix exec() to accept any Mapping (not just dicts),
> then we can create a proxy mapping for "Dataclass.__init__.__module__"
> module and everything would work as expected.

Another possible solution is that `__annotations__` are *permitted* to be
lambdas, but not created as such by default, and `get_type_hints` is aware
of this. Operations that are known to break type hints (such as when data
classes copy the type hints from the class body to the `__init__` method)
could covert the type hint from a string to a lambda before moving it. It
does not even have to be a lambda; an instance of some `TypeHint` class,
which stores the string and pointers to the original `globals` and `locals`
(or just the original object whose `globals` and `locals` are relevant),
but that's essentially what a lambda is anyway.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180923/5acbe92c/attachment.html>


More information about the Python-Dev mailing list