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

Add syntactic sugar for attr.ib(default=attr.Factory) #356

Merged
merged 6 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions changelog.d/178.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``attr.ib(factory=f)`` is now syntactic sugar for the common case of ``attr.ib(default=attr.Factory(f)``.
16 changes: 15 additions & 1 deletion src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def __hash__(self):

def attrib(default=NOTHING, validator=None,
repr=True, cmp=True, hash=None, init=True,
convert=None, metadata=None, type=None, converter=None):
convert=None, metadata=None, type=None, converter=None,
factory=None):
"""
Create a new attribute on a class.

Expand All @@ -83,6 +84,9 @@ def attrib(default=NOTHING, validator=None,

:type default: Any value.

:param callable factory: Syntactic sugar for
``default=attr.Factory(callable)``.

:param validator: :func:`callable` that is called by ``attrs``-generated
``__init__`` methods after the instance has been initialized. They
receive the initialized instance, the :class:`Attribute`, and the
Expand Down Expand Up @@ -137,6 +141,8 @@ def attrib(default=NOTHING, validator=None,
.. deprecated:: 17.4.0 *convert*
.. versionadded:: 17.4.0 *converter* as a replacement for the deprecated
*convert* to achieve consistency with other noun-based arguments.
.. versionadded:: 18.1.0
``factory=f`` is syntactic sugar for ``default=attr.Factory(f)``.
"""
if hash is not None and hash is not True and hash is not False:
raise TypeError(
Expand All @@ -156,6 +162,14 @@ def attrib(default=NOTHING, validator=None,
)
converter = convert

if factory is not None:
if default is not NOTHING:
raise ValueError(
"The `default` and `factory` arguments are mutually "
"exclusive."
)
default = Factory(factory)

This comment was marked as spam.


if metadata is None:
metadata = {}

Expand Down
21 changes: 21 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,27 @@ class C(object):

assert not isinstance(x, _CountingAttr)

def test_factory_sugar(self):
"""
Passing factory=f is syntactic sugar for passing default=Factory(f).
"""
@attr.s
class C(object):
x = attr.ib(factory=list)

assert Factory(list) == attr.fields(C).x.default

def test_factory_mutex(self):
"""
Passing both default and factory raises ValueError.
"""
with pytest.raises(ValueError) as ei:
@attr.s
class C(object):
x = attr.ib(factory=list, default=Factory(list))

assert "mutually exclusive" in ei.value.args[0]


@attr.s
class GC(object):
Expand Down