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

Implement @converter decorator #728

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.cache
.coverage*
.hypothesis
.idea/
.mypy_cache
.pytest_cache
.tox
Expand All @@ -11,3 +12,5 @@ dist
docs/_build/
htmlcov
pip-wheel-metadata
Pipfile
Pipfile.lock
20 changes: 18 additions & 2 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -2384,6 +2384,7 @@ def from_counting_attr(cls, name, ca, type=None):
not in (
"name",
"validator",
"converter",
"default",
"type",
"inherited",
Expand All @@ -2392,6 +2393,7 @@ def from_counting_attr(cls, name, ca, type=None):
return cls(
name=name,
validator=ca._validator,
converter=ca._converter,
default=ca._default,
type=type,
cmp=None,
Expand Down Expand Up @@ -2500,7 +2502,7 @@ class _CountingAttr(object):
"init",
"metadata",
"_validator",
"converter",
"_converter",
"type",
"kw_only",
"on_setattr",
Expand Down Expand Up @@ -2568,7 +2570,7 @@ def __init__(
self.counter = _CountingAttr.cls_counter
self._default = default
self._validator = validator
self.converter = converter
self._converter = converter
self.repr = repr
self.eq = eq
self.order = order
Expand All @@ -2593,6 +2595,20 @@ def validator(self, meth):
self._validator = and_(self._validator, meth)
return meth

def converter(self, meth):
"""
Decorator that adds *meth* to the list of converters.

Returns *meth* unchanged.

.. versionadded:: # TODO: TBD
"""
if self._converter is None:
self._converter = meth
else:
self._converter = pipe(self._converter, meth)
return meth

def default(self, meth):
"""
Decorator that allows to set the default for an attribute.
Expand Down
30 changes: 30 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,36 @@ def v2(self, _, __):

assert _AndValidator((v, v2)) == a._validator

def test_converter_decorator_single(self):
"""
If _CountingAttr.converter is used as a decorator and there is no
decorator set, the decorated method is used as the converter.
"""
a = attr.ib()

@a.converter
def c():
pass

assert c == a._converter

def test_converter_decorator(self):
"""
If _CountingAttr.converter is used as a decorator and there is already
a decorator set, the decorators are composed using `pipe`.
"""

def c(val):
return ('c', val)

a = attr.ib(converter=c)

@a.converter
def c2(val):
return ('c2', val)

assert ('c2', ('c', 'foo')) == a._converter('foo')

def test_default_decorator_already_set(self):
"""
Raise DefaultAlreadySetError if the decorator is used after a default
Expand Down