Simplify asdict and has (#48)

This commit is contained in:
Tin Tvrtković 2016-08-08 08:44:30 +02:00 committed by Hynek Schlawack
parent e833e0c1cd
commit 588fe48019
3 changed files with 6 additions and 9 deletions

View File

@ -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>`_
----

View File

@ -3,7 +3,7 @@ from __future__ import absolute_import, division, print_function
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):
@ -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)
@ -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):

View File

@ -15,12 +15,12 @@ from . import simple_classes, nested_classes
from attr._funcs import (
asdict,
assoc,
fields,
has,
)
from attr._make import (
attr,
attributes,
fields,
)
MAPPING_TYPES = (dict, OrderedDict)