Skip to content

Commit

Permalink
Simplify asdict and has (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinche authored and hynek committed Aug 8, 2016
1 parent e833e0c commit 588fe48
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Changes:

- ``attr.asdict``\ 's ``dict_factory`` arguments is now propagated on recursion.
`#45 <https://github.com/hynek/attrs/issues/45>`_
- ``attr.asdict`` and ``attr.has`` are significantly faster.
`#48 <https://github.com/hynek/attrs/issues/48>`_


----
Expand Down
11 changes: 3 additions & 8 deletions src/attr/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import copy

from ._compat import iteritems
from ._make import Attribute, NOTHING, fields
from ._make import Attribute, NOTHING, _fast_attrs_iterate


def asdict(inst, recurse=True, filter=None, dict_factory=dict):
Expand All @@ -30,7 +30,7 @@ def asdict(inst, recurse=True, filter=None, dict_factory=dict):
.. versionadded:: 16.0.0
*dict_factory*
"""
attrs = fields(inst.__class__)
attrs = _fast_attrs_iterate(inst)
rv = dict_factory()
for a in attrs:
v = getattr(inst, a.name)
Expand Down Expand Up @@ -71,12 +71,7 @@ def has(cl):
:rtype: :class:`bool`
"""
try:
fields(cl)
except ValueError:
return False
else:
return True
return getattr(cl, "__attrs_attrs__", None) is not None


def assoc(inst, **changes):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
from attr._funcs import (
asdict,
assoc,
fields,
has,
)
from attr._make import (
attr,
attributes,
fields,
)

MAPPING_TYPES = (dict, OrderedDict)
Expand Down

0 comments on commit 588fe48

Please sign in to comment.