Refactor: Replace try/except with contextlib.suppress() (#8676)

This commit is contained in:
Ahzam Ahmed 2022-10-05 15:02:00 +05:00 committed by GitHub
parent 99cf2c415f
commit c10f226622
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 31 additions and 54 deletions

View File

@ -1,3 +1,4 @@
import contextlib
import copy import copy
import datetime import datetime
import decimal import decimal
@ -690,15 +691,13 @@ class BooleanField(Field):
NULL_VALUES = {'null', 'Null', 'NULL', '', None} NULL_VALUES = {'null', 'Null', 'NULL', '', None}
def to_internal_value(self, data): def to_internal_value(self, data):
try: with contextlib.suppress(TypeError):
if data in self.TRUE_VALUES: if data in self.TRUE_VALUES:
return True return True
elif data in self.FALSE_VALUES: elif data in self.FALSE_VALUES:
return False return False
elif data in self.NULL_VALUES and self.allow_null: elif data in self.NULL_VALUES and self.allow_null:
return None return None
except TypeError: # Input is an unhashable type
pass
self.fail('invalid', input=data) self.fail('invalid', input=data)
def to_representation(self, value): def to_representation(self, value):
@ -1158,19 +1157,14 @@ class DateTimeField(Field):
return self.enforce_timezone(value) return self.enforce_timezone(value)
for input_format in input_formats: for input_format in input_formats:
with contextlib.suppress(ValueError, TypeError):
if input_format.lower() == ISO_8601: if input_format.lower() == ISO_8601:
try:
parsed = parse_datetime(value) parsed = parse_datetime(value)
if parsed is not None: if parsed is not None:
return self.enforce_timezone(parsed) return self.enforce_timezone(parsed)
except (ValueError, TypeError):
pass
else:
try:
parsed = self.datetime_parser(value, input_format) parsed = self.datetime_parser(value, input_format)
return self.enforce_timezone(parsed) return self.enforce_timezone(parsed)
except (ValueError, TypeError):
pass
humanized_format = humanize_datetime.datetime_formats(input_formats) humanized_format = humanize_datetime.datetime_formats(input_formats)
self.fail('invalid', format=humanized_format) self.fail('invalid', format=humanized_format)

View File

@ -2,6 +2,8 @@
Pagination serializers determine the structure of the output that should Pagination serializers determine the structure of the output that should
be used for paginated responses. be used for paginated responses.
""" """
import contextlib
from base64 import b64decode, b64encode from base64 import b64decode, b64encode
from collections import OrderedDict, namedtuple from collections import OrderedDict, namedtuple
from urllib import parse from urllib import parse
@ -257,15 +259,12 @@ class PageNumberPagination(BasePagination):
def get_page_size(self, request): def get_page_size(self, request):
if self.page_size_query_param: if self.page_size_query_param:
try: with contextlib.suppress(KeyError, ValueError):
return _positive_int( return _positive_int(
request.query_params[self.page_size_query_param], request.query_params[self.page_size_query_param],
strict=True, strict=True,
cutoff=self.max_page_size cutoff=self.max_page_size
) )
except (KeyError, ValueError):
pass
return self.page_size return self.page_size
def get_next_link(self): def get_next_link(self):
@ -430,15 +429,12 @@ class LimitOffsetPagination(BasePagination):
def get_limit(self, request): def get_limit(self, request):
if self.limit_query_param: if self.limit_query_param:
try: with contextlib.suppress(KeyError, ValueError):
return _positive_int( return _positive_int(
request.query_params[self.limit_query_param], request.query_params[self.limit_query_param],
strict=True, strict=True,
cutoff=self.max_limit cutoff=self.max_limit
) )
except (KeyError, ValueError):
pass
return self.default_limit return self.default_limit
def get_offset(self, request): def get_offset(self, request):
@ -680,15 +676,12 @@ class CursorPagination(BasePagination):
def get_page_size(self, request): def get_page_size(self, request):
if self.page_size_query_param: if self.page_size_query_param:
try: with contextlib.suppress(KeyError, ValueError):
return _positive_int( return _positive_int(
request.query_params[self.page_size_query_param], request.query_params[self.page_size_query_param],
strict=True, strict=True,
cutoff=self.max_page_size cutoff=self.max_page_size
) )
except (KeyError, ValueError):
pass
return self.page_size return self.page_size
def get_next_link(self): def get_next_link(self):

View File

@ -4,7 +4,9 @@ Parsers are used to parse the content of incoming HTTP requests.
They give us a generic way of being able to handle various media types They give us a generic way of being able to handle various media types
on the request, such as form content or json encoded data. on the request, such as form content or json encoded data.
""" """
import codecs import codecs
import contextlib
from django.conf import settings from django.conf import settings
from django.core.files.uploadhandler import StopFutureHandlers from django.core.files.uploadhandler import StopFutureHandlers
@ -193,17 +195,12 @@ class FileUploadParser(BaseParser):
Detects the uploaded file name. First searches a 'filename' url kwarg. Detects the uploaded file name. First searches a 'filename' url kwarg.
Then tries to parse Content-Disposition header. Then tries to parse Content-Disposition header.
""" """
try: with contextlib.suppress(KeyError):
return parser_context['kwargs']['filename'] return parser_context['kwargs']['filename']
except KeyError:
pass
try: with contextlib.suppress(AttributeError, KeyError, ValueError):
meta = parser_context['request'].META meta = parser_context['request'].META
disposition, params = parse_header_parameters(meta['HTTP_CONTENT_DISPOSITION']) disposition, params = parse_header_parameters(meta['HTTP_CONTENT_DISPOSITION'])
if 'filename*' in params: if 'filename*' in params:
return params['filename*'] return params['filename*']
else:
return params['filename'] return params['filename']
except (AttributeError, KeyError, ValueError):
pass

View File

@ -1,3 +1,4 @@
import contextlib
import sys import sys
from collections import OrderedDict from collections import OrderedDict
from urllib import parse from urllib import parse
@ -170,7 +171,7 @@ class RelatedField(Field):
def get_attribute(self, instance): def get_attribute(self, instance):
if self.use_pk_only_optimization() and self.source_attrs: if self.use_pk_only_optimization() and self.source_attrs:
# Optimized case, return a mock object only containing the pk attribute. # Optimized case, return a mock object only containing the pk attribute.
try: with contextlib.suppress(AttributeError):
attribute_instance = get_attribute(instance, self.source_attrs[:-1]) attribute_instance = get_attribute(instance, self.source_attrs[:-1])
value = attribute_instance.serializable_value(self.source_attrs[-1]) value = attribute_instance.serializable_value(self.source_attrs[-1])
if is_simple_callable(value): if is_simple_callable(value):
@ -183,9 +184,6 @@ class RelatedField(Field):
value = getattr(value, 'pk', value) value = getattr(value, 'pk', value)
return PKOnlyObject(pk=value) return PKOnlyObject(pk=value)
except AttributeError:
pass
# Standard case, return the object instance. # Standard case, return the object instance.
return super().get_attribute(instance) return super().get_attribute(instance)

View File

@ -6,7 +6,9 @@ on the response, such as JSON encoded data or HTML output.
REST framework also provides an HTML renderer that renders the browsable API. REST framework also provides an HTML renderer that renders the browsable API.
""" """
import base64 import base64
import contextlib
from collections import OrderedDict from collections import OrderedDict
from urllib import parse from urllib import parse
@ -72,11 +74,8 @@ class JSONRenderer(BaseRenderer):
# then pretty print the result. # then pretty print the result.
# Note that we coerce `indent=0` into `indent=None`. # Note that we coerce `indent=0` into `indent=None`.
base_media_type, params = parse_header_parameters(accepted_media_type) base_media_type, params = parse_header_parameters(accepted_media_type)
try: with contextlib.suppress(KeyError, ValueError, TypeError):
return zero_as_none(max(min(int(params['indent']), 8), 0)) return zero_as_none(max(min(int(params['indent']), 8), 0))
except (KeyError, ValueError, TypeError):
pass
# If 'indent' is provided in the context, then pretty print the result. # If 'indent' is provided in the context, then pretty print the result.
# E.g. If we're being called by the BrowsableAPIRenderer. # E.g. If we're being called by the BrowsableAPIRenderer.
return renderer_context.get('indent', None) return renderer_context.get('indent', None)
@ -488,11 +487,8 @@ class BrowsableAPIRenderer(BaseRenderer):
return return
if existing_serializer is not None: if existing_serializer is not None:
try: with contextlib.suppress(TypeError):
return self.render_form_for_serializer(existing_serializer) return self.render_form_for_serializer(existing_serializer)
except TypeError:
pass
if has_serializer: if has_serializer:
if method in ('PUT', 'PATCH'): if method in ('PUT', 'PATCH'):
serializer = view.get_serializer(instance=instance, **kwargs) serializer = view.get_serializer(instance=instance, **kwargs)

View File

@ -10,6 +10,8 @@ python primitives.
2. The process of marshalling between python primitives and request and 2. The process of marshalling between python primitives and request and
response content is handled by parsers and renderers. response content is handled by parsers and renderers.
""" """
import contextlib
import copy import copy
import inspect import inspect
import traceback import traceback
@ -1496,12 +1498,10 @@ class ModelSerializer(Serializer):
# they can't be nested attribute lookups. # they can't be nested attribute lookups.
continue continue
try: with contextlib.suppress(FieldDoesNotExist):
field = model._meta.get_field(source) field = model._meta.get_field(source)
if isinstance(field, DjangoModelField): if isinstance(field, DjangoModelField):
model_fields[source] = field model_fields[source] = field
except FieldDoesNotExist:
pass
return model_fields return model_fields

View File

@ -1,6 +1,8 @@
""" """
Helper classes for parsers. Helper classes for parsers.
""" """
import contextlib
import datetime import datetime
import decimal import decimal
import json # noqa import json # noqa
@ -58,10 +60,8 @@ class JSONEncoder(json.JSONEncoder):
) )
elif hasattr(obj, '__getitem__'): elif hasattr(obj, '__getitem__'):
cls = (list if isinstance(obj, (list, tuple)) else dict) cls = (list if isinstance(obj, (list, tuple)) else dict)
try: with contextlib.suppress(Exception):
return cls(obj) return cls(obj)
except Exception:
pass
elif hasattr(obj, '__iter__'): elif hasattr(obj, '__iter__'):
return tuple(item for item in obj) return tuple(item for item in obj)
return super().default(obj) return super().default(obj)

View File

@ -1,3 +1,4 @@
import contextlib
import sys import sys
from collections import OrderedDict from collections import OrderedDict
from collections.abc import Mapping, MutableMapping from collections.abc import Mapping, MutableMapping
@ -103,15 +104,13 @@ class JSONBoundField(BoundField):
# When HTML form input is used and the input is not valid # When HTML form input is used and the input is not valid
# value will be a JSONString, rather than a JSON primitive. # value will be a JSONString, rather than a JSON primitive.
if not getattr(value, 'is_json_string', False): if not getattr(value, 'is_json_string', False):
try: with contextlib.suppress(TypeError, ValueError):
value = json.dumps( value = json.dumps(
self.value, self.value,
sort_keys=True, sort_keys=True,
indent=4, indent=4,
separators=(',', ': '), separators=(',', ': '),
) )
except (TypeError, ValueError):
pass
return self.__class__(self._field, value, self.errors, self._prefix) return self.__class__(self._field, value, self.errors, self._prefix)