<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">Yes, it’s <a href="https://bugs.python.org/issue34776">https://bugs.python.org/issue34776</a><br><br><div id="AppleMailSignature" dir="ltr">--<div>Eric</div></div><div dir="ltr"><br>On Sep 27, 2018, at 12:05 PM, Ivan Levkivskyi <<a href="mailto:levkivskyi@gmail.com">levkivskyi@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div dir="ltr"><div dir="ltr">Do we have a b.p.o. issue about this? If no, then I would recommend to open one, so that we will not loose track of this.<div><br></div><div>--</div><div>Ivan</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Sat, 22 Sep 2018 at 16:32, David Hagen <<a href="mailto:david@drhagen.com">david@drhagen.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">The new postponed annotations have an unexpected interaction with dataclasses. Namely, you cannot get the type hints of any of the data classes methods.<div><br>For example, I have some code that inspects the type parameters of a class's `__init__` method. (The real use case is to provide a default serializer for the class, but that is not important here.)  </div><div><br></div>```<br>from dataclasses import dataclass<br>from typing import get_type_hints<br><br>class Foo:<br>    pass<br><br>@dataclass<br>class Bar:<br>    foo: Foo<br><br>print(get_type_hints(Bar.__init__))<div>```</div><div><br>In Python 3.6 and 3.7, this does what is expected; it prints `{'foo': <class '__main__.Foo'>, 'return': <class 'NoneType'>}`.<br><br>However, if in Python 3.7, I add `from __future__ import annotations`, then this fails with an error:<br><br>```<br>NameError: name 'Foo' is not defined</div><div>```</div><br>I know why this is happening. The `__init__` method is defined in the `dataclasses` module which does not have the `Foo` object in its environment, and the `Foo` annotation is being passed to `dataclass` and attached to `__init__` as the string `"Foo"` rather than as the original object `Foo`, but `get_type_hints` for the new annotations only does a name lookup in the module where `__init__` is defined not where the annotation is defined.<div><br></div><div>I know that the use of lambdas to implement PEP 563 was rejected for performance reasons. I could be wrong, but I think this was motivated by variable annotations because the lambda would have to be constructed each time the function body ran. I was wondering if I could motivate storing the annotations as lambdas in class bodies and function signatures, in which the environment is already being captured and is code that usually only runs once.</div></div>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/levkivskyi%40gmail.com" rel="noreferrer" target="_blank">https://mail.python.org/mailman/options/python-dev/levkivskyi%40gmail.com</a><br>
</blockquote></div>
</div></blockquote><blockquote type="cite"><div dir="ltr"><span>_______________________________________________</span><br><span>Python-Dev mailing list</span><br><span><a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a></span><br><span><a href="https://mail.python.org/mailman/listinfo/python-dev">https://mail.python.org/mailman/listinfo/python-dev</a></span><br><span>Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/eric%2Ba-python-dev%40trueblade.com">https://mail.python.org/mailman/options/python-dev/eric%2Ba-python-dev%40trueblade.com</a></span><br></div></blockquote></body></html>