You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The implementation needs some work with regards to string annotations. This only affects ClassVar and InitVar, since other annotations are ignored.
@dataclass
class C:
context: "InitVar[context]"
count: "ClassVar[Counter]"
This is all the more important with regards to PEP 563, "Postponed Evaluation of Annotations".
I'd like to keep the property that typing isn't imported by dataclasses.py, so maybe I'll resort to inspecting the string. I'll probably wait until PEP 563 is finalized before doing anything about this.
I think the easiest option is just using typing.get_type_hints() (as recommended by PEP 526). However, importing typing takes some time, but after PEP 560 it will be approx. 7x faster.
My limited understanding of typing.get_type_hints() is that it would still be a problem, in the case of a forward reference (and probably other cases).
@dataclass
class A:
b: "ClassVar[B]"
class B:
pass
Isn't it true that dataclass(), when processing class A, would get an error when calling typing.get_type_hints() because B isn't defined yet?
Activity
ericvsmith commentedon Dec 3, 2017
See python-attrs/attrs#265 and python-attrs/attrs#302 for the same issue with
attrs.ilevkivskyi commentedon Dec 3, 2017
I think the easiest option is just using
typing.get_type_hints()(as recommended by PEP 526). However, importingtypingtakes some time, but after PEP 560 it will be approx. 7x faster.ericvsmith commentedon Dec 3, 2017
My limited understanding of
typing.get_type_hints()is that it would still be a problem, in the case of a forward reference (and probably other cases).Isn't it true that
dataclass(), when processing classA, would get an error when callingtyping.get_type_hints()becauseBisn't defined yet?ilevkivskyi commentedon Dec 3, 2017
I think this is rather a problem with
get_type_hints(and it can be easily fixed).ilevkivskyi commentedon Dec 3, 2017
Opened python/typing#508
ambv commentedon Apr 18, 2018
This needs addressing before the last beta. I will handle that.
ericvsmith commentedon May 14, 2018
See bpo-33453 for further discussion.