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.
|
:param init: Include this attribute in the generated ``__init__`` method.
|
||||||
:type init: bool
|
:type init: bool
|
||||||
|
|
||||||
:param convert: :func:`callable` that is called by ``attrs``-generated
|
:param callable convert: :func:`callable` that is called by
|
||||||
``__init__`` methods to convert attribute's value to the desired
|
``attrs``-generated ``__init__`` methods to convert attribute's value
|
||||||
format. It is given the passed-in value, and the returned value will
|
to the desired format. It is given the passed-in value, and the
|
||||||
be used as the new value of the attribute.
|
returned value will be used as the new value of the attribute. The
|
||||||
:type convert: callable
|
value is converted before being passed to the validator, if any.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return _CountingAttr(
|
return _CountingAttr(
|
||||||
default=default,
|
default=default,
|
||||||
|
@ -461,10 +460,10 @@ else:
|
||||||
arg_name=arg_name,
|
arg_name=arg_name,
|
||||||
))
|
))
|
||||||
|
|
||||||
if has_validator:
|
|
||||||
lines.append("validate(self)")
|
|
||||||
if has_convert:
|
if has_convert:
|
||||||
lines.append("_convert(self)")
|
lines.append("_convert(self)")
|
||||||
|
if has_validator:
|
||||||
|
lines.append("validate(self)")
|
||||||
|
|
||||||
return """\
|
return """\
|
||||||
def __init__(self, {args}):
|
def __init__(self, {args}):
|
||||||
|
|
|
@ -406,9 +406,9 @@ class TestConvert(object):
|
||||||
assert c.x == 2
|
assert c.x == 2
|
||||||
assert c.y == 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):
|
def validator(inst, attr, val):
|
||||||
raise RuntimeError("foo")
|
raise RuntimeError("foo")
|
||||||
|
@ -416,7 +416,7 @@ class TestConvert(object):
|
||||||
"C",
|
"C",
|
||||||
{"x": attr(validator=validator, convert=lambda v: 1 / 0),
|
{"x": attr(validator=validator, convert=lambda v: 1 / 0),
|
||||||
"y": attr()})
|
"y": attr()})
|
||||||
with pytest.raises(RuntimeError):
|
with pytest.raises(ZeroDivisionError):
|
||||||
C(1, 2)
|
C(1, 2)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue