From 68bf50d8c0a1397209e891e65ccf07862bd06e40 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 11 Apr 2022 20:13:09 +0200 Subject: [PATCH 01/21] feat(ValueError of _InValidator): Add attr, options and value The docstring of _in() says the ValueError would contain these, however it didn't. Adding said arguments enables uniform processing of ValueErrors raised by validators. One might for example want to extract the attr.name and the value that raised the error to show a custom, more user-friendly error message. --- src/attr/validators.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/attr/validators.py b/src/attr/validators.py index 6a1e198f8..6b8641511 100644 --- a/src/attr/validators.py +++ b/src/attr/validators.py @@ -299,7 +299,10 @@ def __call__(self, inst, attr, value): raise ValueError( "'{name}' must be in {options!r} (got {value!r})".format( name=attr.name, options=self.options, value=value - ) + ), + attr, + self.options, + value ) def __repr__(self): From 470e6543464301a79ae5fac19da554137344339c Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 11 Apr 2022 20:22:09 +0200 Subject: [PATCH 02/21] test(tests for _InValidator): Adapt tests to new error signature Since the error message isn't the only error argument anymore (but should still suffice to test functionality), it was necessary to only assert against the first argument. --- tests/test_validators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_validators.py b/tests/test_validators.py index f3fe69cc1..8ed020c70 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -471,7 +471,7 @@ def test_fail(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in [1, 2, 3] (got None)",) == e.value.args + assert ("'test' must be in [1, 2, 3] (got None)",) == e.value.args[0] def test_fail_with_string(self): """ @@ -482,7 +482,7 @@ def test_fail_with_string(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in 'abc' (got None)",) == e.value.args + assert ("'test' must be in 'abc' (got None)",) == e.value.args[0] def test_repr(self): """ From 33a369426dec50532ec5a01d07752541561f9a02 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 11 Apr 2022 20:40:26 +0200 Subject: [PATCH 03/21] test(tests for _InValidator): Redo tests properly I thought I could get away with lazyness but it's better to do it the right way. The tests now check the full error signature. --- tests/test_validators.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_validators.py b/tests/test_validators.py index 8ed020c70..3ae962a37 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -471,7 +471,8 @@ def test_fail(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in [1, 2, 3] (got None)",) == e.value.args[0] + assert ("'test' must be in [1, 2, 3] (got None)", a, [1, 2, 3], None) \ + == e.value.args def test_fail_with_string(self): """ @@ -482,7 +483,8 @@ def test_fail_with_string(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in 'abc' (got None)",) == e.value.args[0] + assert ("'test' must be in 'abc' (got None)", a, "abc", None) \ + == e.value.args def test_repr(self): """ From 9fcbee2255f82d0e3af3ad8fa0a1a1b770ce3029 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 11 Apr 2022 20:57:13 +0200 Subject: [PATCH 04/21] docs(Changelog): Add changelog fragment This should sufficiently summarize the changes. --- changelog.d/950.change.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelog.d/950.change.rst diff --git a/changelog.d/950.change.rst b/changelog.d/950.change.rst new file mode 100644 index 000000000..3623d6ea0 --- /dev/null +++ b/changelog.d/950.change.rst @@ -0,0 +1,2 @@ +The error signature of _InValidator has been completed, it now includes +the attribute, options and value as proclaimed in the docstring of _in(). \ No newline at end of file From 2f372b79d73fccf84d935fbe0b1f5d46372fe0a9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Apr 2022 19:16:31 +0000 Subject: [PATCH 05/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog.d/950.change.rst | 2 +- src/attr/validators.py | 2 +- tests/test_validators.py | 16 ++++++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/changelog.d/950.change.rst b/changelog.d/950.change.rst index 3623d6ea0..fd6163c2f 100644 --- a/changelog.d/950.change.rst +++ b/changelog.d/950.change.rst @@ -1,2 +1,2 @@ The error signature of _InValidator has been completed, it now includes -the attribute, options and value as proclaimed in the docstring of _in(). \ No newline at end of file +the attribute, options and value as proclaimed in the docstring of _in(). diff --git a/src/attr/validators.py b/src/attr/validators.py index 6b8641511..3a4e72823 100644 --- a/src/attr/validators.py +++ b/src/attr/validators.py @@ -302,7 +302,7 @@ def __call__(self, inst, attr, value): ), attr, self.options, - value + value, ) def __repr__(self): diff --git a/tests/test_validators.py b/tests/test_validators.py index 3ae962a37..3bec62b03 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -471,8 +471,12 @@ def test_fail(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in [1, 2, 3] (got None)", a, [1, 2, 3], None) \ - == e.value.args + assert ( + "'test' must be in [1, 2, 3] (got None)", + a, + [1, 2, 3], + None, + ) == e.value.args def test_fail_with_string(self): """ @@ -483,8 +487,12 @@ def test_fail_with_string(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in 'abc' (got None)", a, "abc", None) \ - == e.value.args + assert ( + "'test' must be in 'abc' (got None)", + a, + "abc", + None, + ) == e.value.args def test_repr(self): """ From 32cc097f4d6f59ce9cfc825745629a14a89efd55 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Tue, 12 Apr 2022 15:54:17 +0200 Subject: [PATCH 06/21] feat(validators._in()): Add default argument "verbose_value_error=False" This should allow for backwards-compatibility. --- src/attr/validators.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/attr/validators.py b/src/attr/validators.py index 3a4e72823..0e4633d60 100644 --- a/src/attr/validators.py +++ b/src/attr/validators.py @@ -288,6 +288,7 @@ def optional(validator): @attrs(repr=False, slots=True, hash=True) class _InValidator: options = attrib() + verbose_value_error = attrib() def __call__(self, inst, attr, value): try: @@ -296,14 +297,21 @@ def __call__(self, inst, attr, value): in_options = False if not in_options: - raise ValueError( - "'{name}' must be in {options!r} (got {value!r})".format( - name=attr.name, options=self.options, value=value - ), - attr, - self.options, - value, - ) + if not self.verbose_value_error: + raise ValueError( + "'{name}' must be in {options!r} (got {value!r})".format( + name=attr.name, options=self.options, value=value + ), + ) + else: + raise ValueError( + "'{name}' must be in {options!r} (got {value!r})".format( + name=attr.name, options=self.options, value=value + ), + attr, + self.options, + value, + ) def __repr__(self): return "".format( @@ -311,7 +319,7 @@ def __repr__(self): ) -def in_(options): +def in_(options, verbose_value_error=False): """ A validator that raises a `ValueError` if the initializer is called with a value that does not belong in the options provided. The check is @@ -319,14 +327,16 @@ def in_(options): :param options: Allowed options. :type options: list, tuple, `enum.Enum`, ... + :param verbose_value_error: Determines extent of the ValueError signature. + :type verbose_value_error: bool - :raises ValueError: With a human readable error message, the attribute (of - type `attrs.Attribute`), the expected options, and the value it - got. + :raises ValueError: With a human readable error message, and if + verbose_value_error is set to True, the attribute (of type + `attrs.Attribute`), the expected options, and the value it got. .. versionadded:: 17.1.0 """ - return _InValidator(options) + return _InValidator(options, verbose_value_error) @attrs(repr=False, slots=False, hash=True) From 8a0d27e87ad3493ddf694df7a2e2e608ff40f2bc Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Tue, 12 Apr 2022 15:56:09 +0200 Subject: [PATCH 07/21] test(tests for _InValidator): Add tests for call with "verbose_value_error=True" This should sufficiently test the verbose signature. --- tests/test_validators.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_validators.py b/tests/test_validators.py index 3bec62b03..2132a43ca 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -469,6 +469,19 @@ def test_fail(self): """ v = in_([1, 2, 3]) a = simple_attr("test") + with pytest.raises(ValueError) as e: + v(None, a, None) + assert ( + "'test' must be in [1, 2, 3] (got None)", + ) == e.value.args + + def test_fail_verbose(self): + """ + Raise verbose ValueError if the value is outside our options + and verbose_value_error is set to True. + """ + v = in_([1, 2, 3], verbose_value_error=True) + a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) assert ( @@ -485,6 +498,20 @@ def test_fail_with_string(self): """ v = in_("abc") a = simple_attr("test") + with pytest.raises(ValueError) as e: + v(None, a, None) + assert ( + "'test' must be in 'abc' (got None)", + ) == e.value.args + + def test_fail_with_string_verbose(self): + """ + Raise verbose ValueError if the value is outside our options when + the options are specified as a string, verbose_value_error is set + to True and the value is not a string. + """ + v = in_("abc", verbose_value_error=True) + a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) assert ( From 562eb51536797444a658167931b49039e7688d13 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Tue, 12 Apr 2022 15:57:55 +0200 Subject: [PATCH 08/21] docs(Changelog): Adapt changelog fragment I noticed that the number didn't correspond to the PR #, so I renamed the file. Also modified the content to properly reflect the changes. --- changelog.d/950.change.rst | 2 -- changelog.d/951.change.rst | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) delete mode 100644 changelog.d/950.change.rst create mode 100644 changelog.d/951.change.rst diff --git a/changelog.d/950.change.rst b/changelog.d/950.change.rst deleted file mode 100644 index fd6163c2f..000000000 --- a/changelog.d/950.change.rst +++ /dev/null @@ -1,2 +0,0 @@ -The error signature of _InValidator has been completed, it now includes -the attribute, options and value as proclaimed in the docstring of _in(). diff --git a/changelog.d/951.change.rst b/changelog.d/951.change.rst new file mode 100644 index 000000000..f32cb8da7 --- /dev/null +++ b/changelog.d/951.change.rst @@ -0,0 +1,4 @@ +validators._in() now includes the default argument +"verbose_value_error=False". If set to True, the +ValueError raised in _InValidator includes the attribute, +options and value in addition to the error message. \ No newline at end of file From 1b6228abe83ceffd4aff0189716d1bec187538f5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Apr 2022 13:58:38 +0000 Subject: [PATCH 09/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog.d/951.change.rst | 2 +- tests/test_validators.py | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/changelog.d/951.change.rst b/changelog.d/951.change.rst index f32cb8da7..a4864c85d 100644 --- a/changelog.d/951.change.rst +++ b/changelog.d/951.change.rst @@ -1,4 +1,4 @@ validators._in() now includes the default argument "verbose_value_error=False". If set to True, the ValueError raised in _InValidator includes the attribute, -options and value in addition to the error message. \ No newline at end of file +options and value in addition to the error message. diff --git a/tests/test_validators.py b/tests/test_validators.py index 2132a43ca..781222678 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -471,9 +471,7 @@ def test_fail(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ( - "'test' must be in [1, 2, 3] (got None)", - ) == e.value.args + assert ("'test' must be in [1, 2, 3] (got None)",) == e.value.args def test_fail_verbose(self): """ @@ -500,9 +498,7 @@ def test_fail_with_string(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ( - "'test' must be in 'abc' (got None)", - ) == e.value.args + assert ("'test' must be in 'abc' (got None)",) == e.value.args def test_fail_with_string_verbose(self): """ From 4817aca3b593a0ebf73c1540bf1245c8b01f83d6 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 2 May 2022 22:18:15 +0200 Subject: [PATCH 10/21] docs(changelog fragment): Make fragment "breaking" and use semantic newlines --- changelog.d/951.breaking.rst | 6 ++++++ changelog.d/951.change.rst | 4 ---- 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 changelog.d/951.breaking.rst delete mode 100644 changelog.d/951.change.rst diff --git a/changelog.d/951.breaking.rst b/changelog.d/951.breaking.rst new file mode 100644 index 000000000..7003e5598 --- /dev/null +++ b/changelog.d/951.breaking.rst @@ -0,0 +1,6 @@ +validators._in() now includes +the default argument "verbose_value_error=False". +If set to True, +the ValueError raised in _InValidator +includes the attribute, options and value +in addition to the error message. \ No newline at end of file diff --git a/changelog.d/951.change.rst b/changelog.d/951.change.rst deleted file mode 100644 index a4864c85d..000000000 --- a/changelog.d/951.change.rst +++ /dev/null @@ -1,4 +0,0 @@ -validators._in() now includes the default argument -"verbose_value_error=False". If set to True, the -ValueError raised in _InValidator includes the attribute, -options and value in addition to the error message. From d40b6e4cae73ca717886a513b26b3ba67866d594 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 2 May 2022 22:19:15 +0200 Subject: [PATCH 11/21] docs(docstring of _in()): Use semantic newlines --- src/attr/validators.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/attr/validators.py b/src/attr/validators.py index 0e4633d60..9d579bc27 100644 --- a/src/attr/validators.py +++ b/src/attr/validators.py @@ -330,9 +330,11 @@ def in_(options, verbose_value_error=False): :param verbose_value_error: Determines extent of the ValueError signature. :type verbose_value_error: bool - :raises ValueError: With a human readable error message, and if - verbose_value_error is set to True, the attribute (of type - `attrs.Attribute`), the expected options, and the value it got. + :raises ValueError: With a human readable error message, + and if verbose_value_error is set to True, + the attribute (of type `attrs.Attribute`), + the expected options, + and the value it got. .. versionadded:: 17.1.0 """ From d7cfbd3e45dc81379dd65536e28d33cbd8b3406e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 May 2022 20:22:21 +0000 Subject: [PATCH 12/21] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- changelog.d/951.breaking.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/951.breaking.rst b/changelog.d/951.breaking.rst index 7003e5598..02590ef71 100644 --- a/changelog.d/951.breaking.rst +++ b/changelog.d/951.breaking.rst @@ -3,4 +3,4 @@ the default argument "verbose_value_error=False". If set to True, the ValueError raised in _InValidator includes the attribute, options and value -in addition to the error message. \ No newline at end of file +in addition to the error message. From f6bb5c02cf8acab3d96d83e87f13340acf7cd9c2 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 Jun 2022 02:34:03 +0200 Subject: [PATCH 13/21] docs(changelog fragment): Use semantic newlines --- changelog.d/950.change.rst | 2 -- changelog.d/951.change.rst | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) delete mode 100644 changelog.d/950.change.rst create mode 100644 changelog.d/951.change.rst diff --git a/changelog.d/950.change.rst b/changelog.d/950.change.rst deleted file mode 100644 index fd6163c2f..000000000 --- a/changelog.d/950.change.rst +++ /dev/null @@ -1,2 +0,0 @@ -The error signature of _InValidator has been completed, it now includes -the attribute, options and value as proclaimed in the docstring of _in(). diff --git a/changelog.d/951.change.rst b/changelog.d/951.change.rst new file mode 100644 index 000000000..24303f6a9 --- /dev/null +++ b/changelog.d/951.change.rst @@ -0,0 +1,3 @@ +The error signature of _InValidator has been completed, +it now includes the attribute, +options and value as proclaimed in the docstring of _in(). From a2c4f31ade37778c92ac53e275a394d592fb1a45 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 Jun 2022 02:47:05 +0200 Subject: [PATCH 14/21] Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" This reverts commit d7cfbd3e45dc81379dd65536e28d33cbd8b3406e. --- changelog.d/951.breaking.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/951.breaking.rst b/changelog.d/951.breaking.rst index 02590ef71..7003e5598 100644 --- a/changelog.d/951.breaking.rst +++ b/changelog.d/951.breaking.rst @@ -3,4 +3,4 @@ the default argument "verbose_value_error=False". If set to True, the ValueError raised in _InValidator includes the attribute, options and value -in addition to the error message. +in addition to the error message. \ No newline at end of file From eb96ed25e98171cfd2908aa12ef715b15c4a09e1 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 Jun 2022 02:47:23 +0200 Subject: [PATCH 15/21] Revert "docs(docstring of _in()): Use semantic newlines" This reverts commit d40b6e4cae73ca717886a513b26b3ba67866d594. --- src/attr/validators.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/attr/validators.py b/src/attr/validators.py index 9d579bc27..0e4633d60 100644 --- a/src/attr/validators.py +++ b/src/attr/validators.py @@ -330,11 +330,9 @@ def in_(options, verbose_value_error=False): :param verbose_value_error: Determines extent of the ValueError signature. :type verbose_value_error: bool - :raises ValueError: With a human readable error message, - and if verbose_value_error is set to True, - the attribute (of type `attrs.Attribute`), - the expected options, - and the value it got. + :raises ValueError: With a human readable error message, and if + verbose_value_error is set to True, the attribute (of type + `attrs.Attribute`), the expected options, and the value it got. .. versionadded:: 17.1.0 """ From e09d2ca267d4e612cfabbe0c7c75bf7b535fff45 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 Jun 2022 02:50:33 +0200 Subject: [PATCH 16/21] revert(docs(changelog fragment)): Refix changelog fragment --- changelog.d/951.breaking.rst | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 changelog.d/951.breaking.rst diff --git a/changelog.d/951.breaking.rst b/changelog.d/951.breaking.rst deleted file mode 100644 index 7003e5598..000000000 --- a/changelog.d/951.breaking.rst +++ /dev/null @@ -1,6 +0,0 @@ -validators._in() now includes -the default argument "verbose_value_error=False". -If set to True, -the ValueError raised in _InValidator -includes the attribute, options and value -in addition to the error message. \ No newline at end of file From 28cc8bc7e8908c43a64ab275cc40177d8d7082c3 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 Jun 2022 02:51:45 +0200 Subject: [PATCH 17/21] Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" This reverts commit 1b6228ab --- tests/test_validators.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_validators.py b/tests/test_validators.py index 781222678..2132a43ca 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -471,7 +471,9 @@ def test_fail(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in [1, 2, 3] (got None)",) == e.value.args + assert ( + "'test' must be in [1, 2, 3] (got None)", + ) == e.value.args def test_fail_verbose(self): """ @@ -498,7 +500,9 @@ def test_fail_with_string(self): a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) - assert ("'test' must be in 'abc' (got None)",) == e.value.args + assert ( + "'test' must be in 'abc' (got None)", + ) == e.value.args def test_fail_with_string_verbose(self): """ From 26b4234091dbb25e1d99490dadb9b275fc75245a Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 Jun 2022 02:52:11 +0200 Subject: [PATCH 18/21] Revert "docs(Changelog): Adapt changelog fragment" This reverts commit 562eb515 --- changelog.d/950.change.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelog.d/950.change.rst diff --git a/changelog.d/950.change.rst b/changelog.d/950.change.rst new file mode 100644 index 000000000..fd6163c2f --- /dev/null +++ b/changelog.d/950.change.rst @@ -0,0 +1,2 @@ +The error signature of _InValidator has been completed, it now includes +the attribute, options and value as proclaimed in the docstring of _in(). From c4131b5f4ea5121af7768098bd9ed6e3d322c9b0 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 Jun 2022 02:52:17 +0200 Subject: [PATCH 19/21] Revert "test(tests for _InValidator): Add tests for call with "verbose_value_error=True"" This reverts commit 8a0d27e87ad3493ddf694df7a2e2e608ff40f2bc. --- tests/test_validators.py | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/tests/test_validators.py b/tests/test_validators.py index 2132a43ca..3bec62b03 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -469,19 +469,6 @@ def test_fail(self): """ v = in_([1, 2, 3]) a = simple_attr("test") - with pytest.raises(ValueError) as e: - v(None, a, None) - assert ( - "'test' must be in [1, 2, 3] (got None)", - ) == e.value.args - - def test_fail_verbose(self): - """ - Raise verbose ValueError if the value is outside our options - and verbose_value_error is set to True. - """ - v = in_([1, 2, 3], verbose_value_error=True) - a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) assert ( @@ -498,20 +485,6 @@ def test_fail_with_string(self): """ v = in_("abc") a = simple_attr("test") - with pytest.raises(ValueError) as e: - v(None, a, None) - assert ( - "'test' must be in 'abc' (got None)", - ) == e.value.args - - def test_fail_with_string_verbose(self): - """ - Raise verbose ValueError if the value is outside our options when - the options are specified as a string, verbose_value_error is set - to True and the value is not a string. - """ - v = in_("abc", verbose_value_error=True) - a = simple_attr("test") with pytest.raises(ValueError) as e: v(None, a, None) assert ( From 35ac759932d42b574f3dcef1dbf2b893e4fe2b9e Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 Jun 2022 02:52:25 +0200 Subject: [PATCH 20/21] Revert "feat(validators._in()): Add default argument "verbose_value_error=False"" This reverts commit 32cc097f4d6f59ce9cfc825745629a14a89efd55. --- src/attr/validators.py | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/attr/validators.py b/src/attr/validators.py index 0e4633d60..3a4e72823 100644 --- a/src/attr/validators.py +++ b/src/attr/validators.py @@ -288,7 +288,6 @@ def optional(validator): @attrs(repr=False, slots=True, hash=True) class _InValidator: options = attrib() - verbose_value_error = attrib() def __call__(self, inst, attr, value): try: @@ -297,21 +296,14 @@ def __call__(self, inst, attr, value): in_options = False if not in_options: - if not self.verbose_value_error: - raise ValueError( - "'{name}' must be in {options!r} (got {value!r})".format( - name=attr.name, options=self.options, value=value - ), - ) - else: - raise ValueError( - "'{name}' must be in {options!r} (got {value!r})".format( - name=attr.name, options=self.options, value=value - ), - attr, - self.options, - value, - ) + raise ValueError( + "'{name}' must be in {options!r} (got {value!r})".format( + name=attr.name, options=self.options, value=value + ), + attr, + self.options, + value, + ) def __repr__(self): return "".format( @@ -319,7 +311,7 @@ def __repr__(self): ) -def in_(options, verbose_value_error=False): +def in_(options): """ A validator that raises a `ValueError` if the initializer is called with a value that does not belong in the options provided. The check is @@ -327,16 +319,14 @@ def in_(options, verbose_value_error=False): :param options: Allowed options. :type options: list, tuple, `enum.Enum`, ... - :param verbose_value_error: Determines extent of the ValueError signature. - :type verbose_value_error: bool - :raises ValueError: With a human readable error message, and if - verbose_value_error is set to True, the attribute (of type - `attrs.Attribute`), the expected options, and the value it got. + :raises ValueError: With a human readable error message, the attribute (of + type `attrs.Attribute`), the expected options, and the value it + got. .. versionadded:: 17.1.0 """ - return _InValidator(options, verbose_value_error) + return _InValidator(options) @attrs(repr=False, slots=False, hash=True) From 67425fefb86a2c750475653e9ff33450d00913f9 Mon Sep 17 00:00:00 2001 From: iron3oxide Date: Mon, 13 Jun 2022 02:55:58 +0200 Subject: [PATCH 21/21] docs(changelog fragment): Remove old fragment that had the wrong name and somehow reappeared during revert horror --- changelog.d/950.change.rst | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 changelog.d/950.change.rst diff --git a/changelog.d/950.change.rst b/changelog.d/950.change.rst deleted file mode 100644 index fd6163c2f..000000000 --- a/changelog.d/950.change.rst +++ /dev/null @@ -1,2 +0,0 @@ -The error signature of _InValidator has been completed, it now includes -the attribute, options and value as proclaimed in the docstring of _in().