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

Fix post init hook not being called if validators are disabled (#129) #130

Merged
merged 6 commits into from
Jan 6, 2017
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Changes:

- Raise ``FrozenInstanceError`` when trying to delete an attribute from a frozen class.
`#118 <https://github.com/hynek/attrs/pull/118>`_
- Fix ``__attrs_post_init__`` not being run when the validation is disabled.
`#130 <https://github.com/hynek/attrs/pull/130>`_

----

Expand Down
17 changes: 8 additions & 9 deletions src/attr/_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,15 +700,14 @@ def fmt_setter_with_converter(attr_name, value_var):

if attrs_to_validate: # we can skip this if there are no validators.
names_for_globals["_config"] = _config
lines.append("if _config._run_validators is False:")
lines.append(" return")
for a in attrs_to_validate:
val_name = "__attr_validator_{}".format(a.name)
attr_name = "__attr_{}".format(a.name)
lines.append("{}(self, {}, self.{})".format(val_name, attr_name,
a.name))
names_for_globals[val_name] = a.validator
names_for_globals[attr_name] = a
lines.append("if _config._run_validators is True:")
for a in attrs_to_validate:
val_name = "__attr_validator_{}".format(a.name)
attr_name = "__attr_{}".format(a.name)
lines.append(" {}(self, {}, self.{})".format(
val_name, attr_name, a.name))
names_for_globals[val_name] = a.validator
names_for_globals[attr_name] = a
if post_init:
lines.append("self.__attrs_post_init__()")

Expand Down
5 changes: 4 additions & 1 deletion tests/test_make.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,13 @@ class D(object):
assert C.D.__name__ == "D"
assert C.D.__qualname__ == C.__qualname__ + ".D"

def test_post_init(self):
@given(with_validation=booleans())
def test_post_init(self, with_validation, monkeypatch):
"""
Verify that __attrs_post_init__ gets called if defined.
"""
monkeypatch.setattr(_config, '_run_validators', with_validation)

@attributes
class C(object):
x = attr()
Expand Down