Skip to content

Commit

Permalink
Feature/fields properties (#88)
Browse files Browse the repository at this point in the history
🙈
  • Loading branch information
Tinche authored and hynek committed Sep 10, 2016
1 parent dcfbb7b commit 024a16c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
40 changes: 38 additions & 2 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import hashlib
import linecache

from operator import itemgetter

from . import _config
from ._compat import iteritems, isclass, iterkeys
from .exceptions import FrozenInstanceError

# This is used at least twice, so cache it here.
_obj_setattr = object.__setattr__
_init_convert_pat = '__attr_convert_{}'
_tuple_property_pat = " {attr_name} = property(itemgetter({index}))"


class _Nothing(object):
Expand Down Expand Up @@ -106,10 +109,34 @@ def attr(default=NOTHING, validator=None,
)


def _make_attr_tuple_class(cls_name, attr_names):
"""
Create a tuple subclass to hold `Attribute`s for an `attrs` class.
The subclass is a bare tuple with properties for names.
class MyClassAttributes(tuple):
x = property(itemgetter(0))
"""
attr_class_name = "{}Attributes".format(cls_name)
attr_class_template = ["class {}(tuple):".format(attr_class_name)]
if attr_names:
for i, attr_name in enumerate(attr_names):
attr_class_template.append(_tuple_property_pat.format(
index=i,
attr_name=attr_name,
))
else:
attr_class_template.append(" pass")
globs = {'itemgetter': itemgetter}
eval(compile("\n".join(attr_class_template), '', 'exec'), globs)
return globs[attr_class_name]


def _transform_attrs(cls, these):
"""
Transforms all `_CountingAttr`s on a class into `Attribute`s and saves the
list as a tuple in `__attrs_attrs__`.
list as a namedtuple in `__attrs_attrs__`.
If *these* is passed, use that and don't look for them on the class.
"""
Expand All @@ -128,7 +155,16 @@ def _transform_attrs(cls, these):
for name, ca
in iteritems(these)]

cls.__attrs_attrs__ = tuple(super_cls + [
non_super_attrs = [
Attribute.from_counting_attr(name=attr_name, ca=ca)
for attr_name, ca
in sorted(ca_list, key=lambda e: e[1].counter)
]
attr_names = [a.name for a in super_cls + non_super_attrs]

AttrsClass = _make_attr_tuple_class(cls.__name__, attr_names)

cls.__attrs_attrs__ = AttrsClass(super_cls + [
Attribute.from_counting_attr(name=attr_name, ca=ca)
for attr_name, ca
in sorted(ca_list, key=lambda e: e[1].counter)
Expand Down
11 changes: 10 additions & 1 deletion tests/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Factory,
)

from .utils import simple_attr, simple_attrs
from .utils import simple_attr, simple_attrs, simple_classes

attrs = simple_attrs.map(lambda c: Attribute.from_counting_attr('name', c))

Expand Down Expand Up @@ -369,12 +369,21 @@ def test_handler_non_attrs_class(self, C):
"{o!r} is not an attrs-decorated class.".format(o=object)
) == e.value.args[0]

@given(simple_classes())
def test_fields(self, C):
"""
Returns a list of `Attribute`a.
"""
assert all(isinstance(a, Attribute) for a in fields(C))

@given(simple_classes())
def test_fields_properties(self, C):
"""
Fields returns a tuple with properties.
"""
for attribute in fields(C):
assert getattr(fields(C), attribute.name) is attribute


class TestConvert(object):
"""
Expand Down

0 comments on commit 024a16c

Please sign in to comment.