From 0323d6f8955f987771269506ca5da461e2e7a248 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 26 Mar 2021 12:27:10 +0000 Subject: [PATCH] Linting fixes (#7874) * Fixed code quality issues - Added '.deepsource.toml' file for continuous analysis on bug risk - Remove `return` from `__init__()` method - Remove duplicate dictionary key(s) - Use `max` built-in to get the maximum of two values - Remove redundant `None` default - Remove unnecessary comprehension Signed-off-by: ankitdobhal * Delete .deepsource.toml * Delete test_fields.py * Reintroduce file from accidental deletion Co-authored-by: ankitdobhal --- rest_framework/pagination.py | 3 +-- rest_framework/relations.py | 8 ++++---- rest_framework/request.py | 2 +- rest_framework/routers.py | 2 +- rest_framework/templatetags/rest_framework.py | 4 ++-- rest_framework/test.py | 2 +- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 91da73de6..dc120d8e8 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -488,8 +488,7 @@ class LimitOffsetPagination(BasePagination): _divide_with_ceil(self.offset, self.limit) ) - if final < 1: - final = 1 + final = max(final, 1) else: current = 1 final = 1 diff --git a/rest_framework/relations.py b/rest_framework/relations.py index cbdf23369..c98700784 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -104,11 +104,11 @@ class RelatedField(Field): self.html_cutoff_text or _(api_settings.HTML_SELECT_CUTOFF_TEXT) ) if not method_overridden('get_queryset', RelatedField, self): - assert self.queryset is not None or kwargs.get('read_only', None), ( + assert self.queryset is not None or kwargs.get('read_only'), ( 'Relational field must provide a `queryset` argument, ' 'override `get_queryset`, or set read_only=`True`.' ) - assert not (self.queryset is not None and kwargs.get('read_only', None)), ( + assert not (self.queryset is not None and kwargs.get('read_only')), ( 'Relational fields should not provide a `queryset` argument, ' 'when setting read_only=`True`.' ) @@ -339,7 +339,7 @@ class HyperlinkedRelatedField(RelatedField): return self.reverse(view_name, kwargs=kwargs, request=request, format=format) def to_internal_value(self, data): - request = self.context.get('request', None) + request = self.context.get('request') try: http_prefix = data.startswith(('http:', 'https:')) except AttributeError: @@ -382,7 +382,7 @@ class HyperlinkedRelatedField(RelatedField): ) request = self.context['request'] - format = self.context.get('format', None) + format = self.context.get('format') # By default use whatever format is given for the current context # unless the target is a different type to the source. diff --git a/rest_framework/request.py b/rest_framework/request.py index 2a007cd2b..17ceadb08 100644 --- a/rest_framework/request.py +++ b/rest_framework/request.py @@ -316,7 +316,7 @@ class Request: 'application/x-www-form-urlencoded', 'multipart/form-data' ) - return any([parser.media_type in form_media for parser in self.parsers]) + return any(parser.media_type in form_media for parser in self.parsers) def _parse(self): """ diff --git a/rest_framework/routers.py b/rest_framework/routers.py index e2afa573f..e0ae24b95 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -290,7 +290,7 @@ class APIRootView(views.APIView): args=args, kwargs=kwargs, request=request, - format=kwargs.get('format', None) + format=kwargs.get('format') ) except NoReverseMatch: # Don't bail out if eg. no list routes exist, only detail routes. diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py index 7bfa8f599..db0e9c95c 100644 --- a/rest_framework/templatetags/rest_framework.py +++ b/rest_framework/templatetags/rest_framework.py @@ -206,7 +206,7 @@ def format_value(value): if value is None or isinstance(value, bool): return mark_safe('%s' % {True: 'true', False: 'false', None: 'null'}[value]) elif isinstance(value, list): - if any([isinstance(item, (list, dict)) for item in value]): + if any(isinstance(item, (list, dict)) for item in value): template = loader.get_template('rest_framework/admin/list_value.html') else: template = loader.get_template('rest_framework/admin/simple_list_value.html') @@ -285,7 +285,7 @@ def schema_links(section, sec_key=None): def add_nested_class(value): if isinstance(value, dict): return 'class=nested' - if isinstance(value, list) and any([isinstance(item, (list, dict)) for item in value]): + if isinstance(value, list) and any(isinstance(item, (list, dict)) for item in value): return 'class=nested' return '' diff --git a/rest_framework/test.py b/rest_framework/test.py index 8ab0f2de1..e934eff55 100644 --- a/rest_framework/test.py +++ b/rest_framework/test.py @@ -124,7 +124,7 @@ if coreapi is not None: def __init__(self, *args, **kwargs): self._session = RequestsClient() kwargs['transports'] = [coreapi.transports.HTTPTransport(session=self.session)] - return super().__init__(*args, **kwargs) + super().__init__(*args, **kwargs) @property def session(self):