-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Only catch TypeError/ValueError for object lookups #6028
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
Conversation
Needs a test though (https://codecov.io/gh/encode/django-rest-framework/pull/6028/diff). |
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.
Neat route around, yup.
rest_framework/exceptions.py
Outdated
@@ -95,6 +95,12 @@ def __hash__(self): | |||
return hash(str(self)) | |||
|
|||
|
|||
class ObjectValueError(Exception): | |||
""" | |||
A value was not suitable for requesting an object. |
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.
I think this could have a clearer docstring. Mention that we use it to indicate when invalid values are passed to ‘queryset.get()’.
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.
Are the new docstrings more clear? Generally, these exceptions will only be caught internally, so it should be fine?
b8a200e
to
20a0ed3
Compare
Thanks @blueyed - test added. |
@rpkilby See #6050 for making this more accessible via Github's status API - i.e. it would show up with "Show all checks". |
The updated tests should sufficiently cover the changeset. |
What about the following to please coverage? (used elsewhere already) diff --git i/tests/test_relations.py w/tests/test_relations.py
index 3c76f009..9326b565 100644
--- i/tests/test_relations.py
+++ w/tests/test_relations.py
@@ -200,7 +200,7 @@ def test_hyperlinked_related_lookup_does_not_exist(self):
def test_hyperlinked_related_internal_type_error(self):
class Field(serializers.HyperlinkedRelatedField):
def get_object(self, incorrect, signature):
- pass
+ raise NotImplementedError() # pragma: no cover
field = Field(view_name='example', queryset=self.queryset)
with pytest.raises(TypeError): |
Makes sense - raising |
1e5b680
to
1e789d0
Compare
btw, merged the setup.cfg changes in another PR. |
1e789d0
to
43f9df0
Compare
* Only catch TypeError/ValueError for object lookups * Test wrapped TypeError/ValueError handling * Raise NotImplementedError in tests instead of pass
Fixes #6025. Basically,
get_object()
should handle the type/value error from.get()
by reraising as a more specific exception type.cc @tomchristie, @tveastman