From c383cf2947f9cf19a3ef62839496679ea910b38b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 15 Mar 2024 12:43:00 +0100 Subject: [PATCH] Use comprehensions --- rest_framework/schemas/coreapi.py | 2 +- rest_framework/utils/field_mapping.py | 2 +- rest_framework/views.py | 8 ++++---- tests/schemas/test_openapi.py | 6 +++--- tests/test_model_serializer.py | 4 +--- tests/test_requests_client.py | 8 ++------ tests/test_validators.py | 4 ++-- tests/utils.py | 6 ++---- 8 files changed, 16 insertions(+), 24 deletions(-) diff --git a/rest_framework/schemas/coreapi.py b/rest_framework/schemas/coreapi.py index 582aba196..e7d83604c 100644 --- a/rest_framework/schemas/coreapi.py +++ b/rest_framework/schemas/coreapi.py @@ -380,7 +380,7 @@ class AutoSchema(ViewInspector): manual_fields = self.get_manual_fields(path, method) fields = self.update_fields(fields, manual_fields) - if fields and any([field.location in ('form', 'body') for field in fields]): + if fields and any(field.location in ('form', 'body') for field in fields): encoding = self.get_encoding(path, method) else: encoding = None diff --git a/rest_framework/utils/field_mapping.py b/rest_framework/utils/field_mapping.py index 30bb65e0c..989c733a9 100644 --- a/rest_framework/utils/field_mapping.py +++ b/rest_framework/utils/field_mapping.py @@ -67,7 +67,7 @@ def get_unique_validators(field_name, model_field): """ Returns a list of UniqueValidators that should be applied to the field. """ - field_set = set([field_name]) + field_set = {field_name} conditions = { c.condition for c in model_field.model._meta.constraints diff --git a/rest_framework/views.py b/rest_framework/views.py index 4c30029fd..75c6943b6 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -354,10 +354,10 @@ class APIView(View): Check if request should be throttled. Raises an appropriate exception if the request is throttled. """ - throttle_durations = [] - for throttle in self.get_throttles(): - if not throttle.allow_request(request, self): - throttle_durations.append(throttle.wait()) + throttle_durations = [ + throttle.wait() for throttle in self.get_throttles() + if not throttle.allow_request(request, self) + ] if throttle_durations: # Filter out `None` values which may happen in case of config / rate diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index a168cb466..f3da95307 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -262,7 +262,7 @@ class TestOperationIntrospection(TestCase): components = inspector.get_components(path, method) assert components['Item']['required'] == ['text'] - assert sorted(list(components['Item']['properties'].keys())) == ['read_only', 'text'] + assert sorted(components['Item']['properties'].keys()) == ['read_only', 'text'] def test_invalid_serializer_class_name(self): path = '/' @@ -366,7 +366,7 @@ class TestOperationIntrospection(TestCase): components = inspector.get_components(path, method) assert sorted(components['Item']['required']) == ['text', 'write_only'] - assert sorted(list(components['Item']['properties'].keys())) == ['text', 'write_only'] + assert sorted(components['Item']['properties'].keys()) == ['text', 'write_only'] assert 'description' in responses['201'] def test_response_body_nested_serializer(self): @@ -398,7 +398,7 @@ class TestOperationIntrospection(TestCase): schema = components['Item'] assert sorted(schema['required']) == ['nested', 'text'] - assert sorted(list(schema['properties'].keys())) == ['nested', 'text'] + assert sorted(schema['properties'].keys()) == ['nested', 'text'] assert schema['properties']['nested']['type'] == 'object' assert list(schema['properties']['nested']['properties'].keys()) == ['number'] assert schema['properties']['nested']['required'] == ['number'] diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 5b6551a98..9d241cd86 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -232,9 +232,7 @@ class TestRegularFieldMappings(TestCase): model = NullableBooleanChoicesModel fields = ['field'] - serializer = NullableBooleanChoicesSerializer(data=dict( - field=None, - )) + serializer = NullableBooleanChoicesSerializer(data={'field': None}) self.assertTrue(serializer.is_valid()) self.assertEqual(serializer.errors, {}) diff --git a/tests/test_requests_client.py b/tests/test_requests_client.py index c8e7be6ee..4933ae55f 100644 --- a/tests/test_requests_client.py +++ b/tests/test_requests_client.py @@ -55,16 +55,12 @@ class HeadersView(APIView): class SessionView(APIView): def get(self, request): - return Response({ - key: value for key, value in request.session.items() - }) + return Response(dict(request.session.items())) def post(self, request): for key, value in request.data.items(): request.session[key] = value - return Response({ - key: value for key, value in request.session.items() - }) + return Response(dict(request.session.items())) class AuthView(APIView): diff --git a/tests/test_validators.py b/tests/test_validators.py index c38dc1134..5066bfd83 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -115,13 +115,13 @@ class TestUniquenessValidation(TestCase): instance = AnotherUniquenessModel.objects.create(code='100') serializer = AnotherUniquenessSerializer(instance) assert all( - ["Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators] + "Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators ) # Accessing data shouldn't effect validators on the model serializer.data assert all( - ["Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators] + "Unique" not in repr(v) for v in AnotherUniquenessModel._meta.get_field('code').validators ) def test_related_model_is_unique(self): diff --git a/tests/utils.py b/tests/utils.py index 4ceb35309..230e9dd3f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -27,10 +27,8 @@ class MockQueryset: def get(self, **lookup): for item in self.items: - if all([ - attrgetter(key.replace('__', '.'))(item) == value - for key, value in lookup.items() - ]): + if all(attrgetter(key.replace('__', '.'))(item) == value + for key, value in lookup.items()): return item raise ObjectDoesNotExist()