Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify asdict and has. #48

Merged
merged 4 commits into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hasattr(cl, "__attrs_attrs__")

This comment was marked as spam.



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