Use comprehensions

This commit is contained in:
Christian Clauss 2024-03-15 12:43:00 +01:00
parent a677b09729
commit c383cf2947
8 changed files with 16 additions and 24 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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']

View File

@ -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, {})

View File

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

View File

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

View File

@ -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()