This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author drhagen
Recipients drhagen
Date 2018-09-23.10:45:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The new postponed annotations have an unexpected interaction with dataclasses. Namely, you cannot get the type hints of any of the data classes methods.

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.)  

from dataclasses import dataclass
from typing import get_type_hints

class Foo:

class Bar:
    foo: Foo


In Python 3.6 and 3.7, this does what is expected; it prints `{'foo': <class '__main__.Foo'>, 'return': <class 'NoneType'>}`.

However, if in Python 3.7, I add `from __future__ import annotations`, then this fails with an error:

NameError: name 'Foo' is not defined

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.

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.

Original mailing list discussion:
Date User Action Args
2018-09-23 10:45:30drhagensetrecipients: + drhagen
2018-09-23 10:45:29drhagensetmessageid: <>
2018-09-23 10:45:29drhagenlinkissue34776 messages
2018-09-23 10:45:29drhagencreate