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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error for in_(a_string) with a non-string value #383
Conversation
Codecov Report
@@ Coverage Diff @@
## master #383 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 9 9
Lines 831 835 +4
Branches 174 174
=====================================
+ Hits 831 835 +4
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please address comments.
src/attr/validators.py
Outdated
if value not in self.options: | ||
try: | ||
in_options = value in self.options | ||
except Exception as e: # e.g. `1 in "abc"` |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
tests/test_validators.py
Outdated
@@ -243,6 +243,18 @@ def test_fail(self): | |||
"'test' must be in [1, 2, 3] (got None)", | |||
) == e.value.args | |||
|
|||
def test_fail_with_string(self): | |||
""" | |||
Same error for string options as for list options, not TypeError. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@wsanchez - done 馃槃 |
changelog.d/383.change.rst
Outdated
@@ -0,0 +1,3 @@ | |||
``in_()`` validators now raise a ValueError with a useful message |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
changelog.d/383.change.rst
Outdated
@@ -0,0 +1,2 @@ | |||
``attr.validators.in_()`` now raises a ``ValueError`` with a useful message even if the options are a string and the value is not a string. | |||
Previously, this raised a ``TypeError`` (matching e.g. ``1 in "abc"``). |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Thanks! |
changelog.d
.Closes #382.