Skip to content

BUG: fix evolve behaviour when a validator fails. #176

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

Closed
wants to merge 3 commits 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
12 changes: 8 additions & 4 deletions src/attr/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def evolve(inst, **changes):
.. versionadded:: 17.1.0
"""
cls = inst.__class__
for a in fields(cls):
attrs = fields(cls)
for a in attrs:
attr_name = a.name # To deal with private attributes.
if attr_name[0] == "_":
init_name = attr_name[1:]
Expand All @@ -216,6 +217,9 @@ def evolve(inst, **changes):
try:
return cls(**changes)
except TypeError as exc:
k = exc.args[0].split("'")[1]
raise AttrsAttributeNotFoundError(
"{k} is not an attrs attribute on {cl}.".format(k=k, cl=cls))
for name in changes:
if getattr(attrs, name, None) is None:
k = exc.args[0].split("'")[1]

This comment was marked as spam.

This comment was marked as spam.

raise AttrsAttributeNotFoundError(
"{} is not an attrs attribute on {}.".format(k, cls))
raise
16 changes: 16 additions & 0 deletions tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
)

from attr.exceptions import AttrsAttributeNotFoundError
from attr.validators import instance_of
from attr._compat import TYPE


MAPPING_TYPES = (dict, OrderedDict)
SEQUENCE_TYPES = (list, tuple)
Expand Down Expand Up @@ -464,3 +467,16 @@ def test_unknown(self, C):
assert (
"aaaa is not an attrs attribute on {cls!r}.".format(cls=C),
) == e.value.args


def test_validator_failure(self):
"""
Make sure we don't swallow TypeError when validation fails within evolve
"""
@attributes
class C(object):
a = attr(validator=instance_of(int))

with pytest.raises(TypeError) as e:
evolve(C(a=1), a="some string")
assert e.value.args[0].startswith("'a' must be <{type} 'int'>".format(type=TYPE))