mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 00:19:53 +03:00
replace try/except with context managger
If the desire is to simply suppress an error, rather than perform some sort of branching logic, the Python standard library has a paradigm for that: contextlib.suppress()
This commit is contained in:
parent
9e398c59ab
commit
b95b6f0cbb
|
@ -16,6 +16,8 @@ automatically.
|
||||||
router.register(r'users', UserViewSet, 'user')
|
router.register(r'users', UserViewSet, 'user')
|
||||||
urlpatterns = router.urls
|
urlpatterns = router.urls
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from functools import update_wrapper
|
from functools import update_wrapper
|
||||||
from inspect import getmembers
|
from inspect import getmembers
|
||||||
|
@ -196,7 +198,7 @@ class ViewSetMixin:
|
||||||
]
|
]
|
||||||
|
|
||||||
for action in actions:
|
for action in actions:
|
||||||
try:
|
with contextlib.suppress(NoReverseMatch):
|
||||||
url_name = '%s-%s' % (self.basename, action.url_name)
|
url_name = '%s-%s' % (self.basename, action.url_name)
|
||||||
namespace = self.request.resolver_match.namespace
|
namespace = self.request.resolver_match.namespace
|
||||||
if namespace:
|
if namespace:
|
||||||
|
@ -205,9 +207,6 @@ class ViewSetMixin:
|
||||||
url = reverse(url_name, self.args, self.kwargs, request=self.request)
|
url = reverse(url_name, self.args, self.kwargs, request=self.request)
|
||||||
view = self.__class__(**action.kwargs)
|
view = self.__class__(**action.kwargs)
|
||||||
action_urls[view.get_view_name()] = url
|
action_urls[view.get_view_name()] = url
|
||||||
except NoReverseMatch:
|
|
||||||
pass # URL requires additional arguments, ignore
|
|
||||||
|
|
||||||
return action_urls
|
return action_urls
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user