Skip to content

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

Merged
merged 3 commits into from
Jul 6, 2018

Conversation

rpkilby
Copy link
Member

@rpkilby rpkilby commented Jun 13, 2018

Fixes #6025. Basically, get_object() should handle the type/value error from .get() by reraising as a more specific exception type.

cc @tomchristie, @tveastman

@blueyed
Copy link
Contributor

blueyed commented Jun 21, 2018

Copy link
Member

@tomchristie tomchristie left a 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.

@@ -95,6 +95,12 @@ def __hash__(self):
return hash(str(self))


class ObjectValueError(Exception):
"""
A value was not suitable for requesting an object.
Copy link
Member

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()’.

Copy link
Member Author

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?

@rpkilby rpkilby force-pushed the object-lookup-failure branch from b8a200e to 20a0ed3 Compare June 21, 2018 21:34
@rpkilby
Copy link
Member Author

rpkilby commented Jun 21, 2018

Thanks @blueyed - test added.

@blueyed
Copy link
Contributor

blueyed commented Jun 21, 2018

@rpkilby
Not really covered still: https://codecov.io/gh/encode/django-rest-framework/pull/6028/diff.

See #6050 for making this more accessible via Github's status API - i.e. it would show up with "Show all checks".

@rpkilby
Copy link
Member Author

rpkilby commented Jun 22, 2018

The updated tests should sufficiently cover the changeset.

@rpkilby rpkilby added this to the 3.8.3 Release milestone Jun 22, 2018
@blueyed
Copy link
Contributor

blueyed commented Jun 23, 2018

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):

@rpkilby
Copy link
Member Author

rpkilby commented Jun 23, 2018

Makes sense - raising NotImplementedError is more appropriate than just silently passing. I've also added NotImplementedError to the coverage line exclusions.

@rpkilby rpkilby force-pushed the object-lookup-failure branch from 1e5b680 to 1e789d0 Compare June 24, 2018 21:57
@rpkilby
Copy link
Member Author

rpkilby commented Jun 24, 2018

btw, merged the setup.cfg changes in another PR.

@carltongibson carltongibson modified the milestones: 3.8.3 Release, 3.9 Release Jul 6, 2018
@carltongibson carltongibson force-pushed the object-lookup-failure branch from 1e789d0 to 43f9df0 Compare July 6, 2018 10:10
@carltongibson carltongibson merged commit 38b3d01 into encode:master Jul 6, 2018
@rpkilby rpkilby deleted the object-lookup-failure branch July 6, 2018 14:07
pchiquet pushed a commit to pchiquet/django-rest-framework that referenced this pull request Nov 17, 2020
* Only catch TypeError/ValueError for object lookups

* Test wrapped TypeError/ValueError handling

* Raise NotImplementedError in tests instead of pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy