Skip to content

Commit

Permalink
Use gross hacks in ClassVar detection to avoid importing typing
Browse files Browse the repository at this point in the history
Father forgive me for I have sinned.

ref ericvsmith/dataclasses#61
  • Loading branch information
hynek committed Nov 7, 2017
1 parent 23e4e06 commit 33b9ac9
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,14 @@ class MyClassAttributes(tuple):
])


try:
from typing import ClassVar

# You cannot use ClassVar together with isinstance.
_CLASS_VAR_CLS = ClassVar.__class__
except ImportError:
class FakeClassVar(object):
pass
def _is_class_var(annot):
"""
Check whether *annot* is a typing.ClassVar.
_CLASS_VAR_CLS = FakeClassVar
The implementation is gross but importing `typing` is slow and there are
discussions to remove it from the stdlib alltogether.
"""
return str(annot).startswith("typing.ClassVar")


def _transform_attrs(cls, these, auto_attribs):
Expand Down Expand Up @@ -231,7 +229,7 @@ def _transform_attrs(cls, these, auto_attribs):
ca_list = []
annot_names = set()
for attr_name, type in anns.items():
if isinstance(type, _CLASS_VAR_CLS):
if _is_class_var(type):
continue
annot_names.add(attr_name)
a = cd.get(attr_name, NOTHING)
Expand Down

0 comments on commit 33b9ac9

Please sign in to comment.