convert before validate, also fix two-space thing
This commit is contained in:
parent
20357d4368
commit
2e7b750696
|
@ -83,12 +83,11 @@ def attr(default=NOTHING, validator=None,
|
|||
:param init: Include this attribute in the generated ``__init__`` method.
|
||||
:type init: bool
|
||||
|
||||
:param convert: :func:`callable` that is called by ``attrs``-generated
|
||||
``__init__`` methods to convert attribute's value to the desired
|
||||
format. It is given the passed-in value, and the returned value will
|
||||
be used as the new value of the attribute.
|
||||
:type convert: callable
|
||||
|
||||
:param callable convert: :func:`callable` that is called by
|
||||
``attrs``-generated ``__init__`` methods to convert attribute's value
|
||||
to the desired format. It is given the passed-in value, and the
|
||||
returned value will be used as the new value of the attribute. The
|
||||
value is converted before being passed to the validator, if any.
|
||||
"""
|
||||
return _CountingAttr(
|
||||
default=default,
|
||||
|
@ -461,10 +460,10 @@ else:
|
|||
arg_name=arg_name,
|
||||
))
|
||||
|
||||
if has_validator:
|
||||
lines.append("validate(self)")
|
||||
if has_convert:
|
||||
lines.append("_convert(self)")
|
||||
if has_validator:
|
||||
lines.append("validate(self)")
|
||||
|
||||
return """\
|
||||
def __init__(self, {args}):
|
||||
|
|
|
@ -406,9 +406,9 @@ class TestConvert(object):
|
|||
assert c.x == 2
|
||||
assert c.y == 2
|
||||
|
||||
def test_convert_after_validate(self):
|
||||
def test_convert_before_validate(self):
|
||||
"""
|
||||
Validation happens before conversion.
|
||||
Validation happens after conversion.
|
||||
"""
|
||||
def validator(inst, attr, val):
|
||||
raise RuntimeError("foo")
|
||||
|
@ -416,7 +416,7 @@ class TestConvert(object):
|
|||
"C",
|
||||
{"x": attr(validator=validator, convert=lambda v: 1 / 0),
|
||||
"y": attr()})
|
||||
with pytest.raises(RuntimeError):
|
||||
with pytest.raises(ZeroDivisionError):
|
||||
C(1, 2)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue