From 67994dca1ee953bf933eafc4045fe4edbd1a6ed3 Mon Sep 17 00:00:00 2001 From: p-himik Date: Thu, 5 Jan 2017 19:18:01 +0700 Subject: [PATCH 1/6] Fix post init hook not being called if validators are disabled (#129) --- src/attr/_make.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/attr/_make.py b/src/attr/_make.py index 66b558ca5..cff2996ac 100644 --- a/src/attr/_make.py +++ b/src/attr/_make.py @@ -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:") + 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__()") From 8c8adb345968dd41a6d2df06ca2aa638d3957f08 Mon Sep 17 00:00:00 2001 From: p-himik Date: Thu, 5 Jan 2017 19:28:04 +0700 Subject: [PATCH 2/6] Fix indentation to make flake8 happy --- src/attr/_make.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/attr/_make.py b/src/attr/_make.py index cff2996ac..0b2136a8f 100644 --- a/src/attr/_make.py +++ b/src/attr/_make.py @@ -704,8 +704,8 @@ def fmt_setter_with_converter(attr_name, value_var): 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)) + 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: From c4291319bd50adb18f85c057a0a97d75806906b8 Mon Sep 17 00:00:00 2001 From: p-himik Date: Fri, 6 Jan 2017 13:55:51 +0700 Subject: [PATCH 3/6] Run test_post_init with different validation settings --- tests/test_make.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_make.py b/tests/test_make.py index e672305b3..3b27e2f66 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -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): """ Verify that __attrs_post_init__ gets called if defined. """ + _config.set_run_validators(with_validation) + @attributes class C(object): x = attr() From 654217eb8f2e20859b60be1c2aee3fbd56ab1726 Mon Sep 17 00:00:00 2001 From: p-himik Date: Fri, 6 Jan 2017 13:56:10 +0700 Subject: [PATCH 4/6] Add changelog entry for fixing post init hook --- CHANGELOG.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index f9125664f..b1902253d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -26,6 +26,8 @@ Changes: - Raise ``FrozenInstanceError`` when trying to delete an attribute from a frozen class. `#118 `_ +- Fix __attrs_post_init__ not being run when the validation is disabled. + `#130 `_ ---- From 56745ed96bb0594ed4412c432ab97240f45b2341 Mon Sep 17 00:00:00 2001 From: p-himik Date: Fri, 6 Jan 2017 14:23:00 +0700 Subject: [PATCH 5/6] Make test_post_init restore global config after it's run --- tests/test_make.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_make.py b/tests/test_make.py index 3b27e2f66..c96c97180 100644 --- a/tests/test_make.py +++ b/tests/test_make.py @@ -306,11 +306,11 @@ class D(object): assert C.D.__qualname__ == C.__qualname__ + ".D" @given(with_validation=booleans()) - def test_post_init(self, with_validation): + def test_post_init(self, with_validation, monkeypatch): """ Verify that __attrs_post_init__ gets called if defined. """ - _config.set_run_validators(with_validation) + monkeypatch.setattr(_config, '_run_validators', with_validation) @attributes class C(object): From 3925d632b23c977ea5a47e9875a6b3d1a82b503b Mon Sep 17 00:00:00 2001 From: p-himik Date: Fri, 6 Jan 2017 20:47:01 +0700 Subject: [PATCH 6/6] Make previous changes be in accord with the project style --- CHANGELOG.rst | 2 +- src/attr/_make.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b1902253d..affeaa71d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -26,7 +26,7 @@ Changes: - Raise ``FrozenInstanceError`` when trying to delete an attribute from a frozen class. `#118 `_ -- Fix __attrs_post_init__ not being run when the validation is disabled. +- Fix ``__attrs_post_init__`` not being run when the validation is disabled. `#130 `_ ---- diff --git a/src/attr/_make.py b/src/attr/_make.py index 0b2136a8f..ca2842c4a 100644 --- a/src/attr/_make.py +++ b/src/attr/_make.py @@ -700,7 +700,7 @@ 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:") + 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)