Skip to content

Commit 0552810

Browse files
jdufresnecarltongibson
authored andcommitted
Use dict and set literals instead of calls to dict() and set() (#5559)
Set literals are available on all supported Python versions. They are idiomatic and always faster: $ python3 -m timeit '{}' 10000000 loops, best of 3: 0.0357 usec per loop $ python3 -m timeit 'dict()' 10000000 loops, best of 3: 0.104 usec per loop $ python3 -m timeit '{1, 2, 3}' 10000000 loops, best of 3: 0.0754 usec per loop $ python3 -m timeit 'set([1, 2, 3])' 1000000 loops, best of 3: 0.228 usec per loop
1 parent f77e794 commit 0552810

9 files changed

+16
-16
lines changed

Diff for: rest_framework/decorators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def decorator(func):
4646
assert isinstance(http_method_names, (list, tuple)), \
4747
'@api_view expected a list of strings, received %s' % type(http_method_names).__name__
4848

49-
allowed_methods = set(http_method_names) | set(('options',))
49+
allowed_methods = set(http_method_names) | {'options'}
5050
WrappedAPIView.http_method_names = [method.lower() for method in allowed_methods]
5151

5252
def handler(self, *args, **kwargs):

Diff for: rest_framework/schemas/generators.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ def distribute_links(obj):
118118

119119

120120
def is_custom_action(action):
121-
return action not in set([
121+
return action not in {
122122
'retrieve', 'list', 'create', 'update', 'partial_update', 'destroy'
123-
])
123+
}
124124

125125

126126
def endpoint_ordering(endpoint):

Diff for: rest_framework/schemas/inspectors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,11 @@ def get_encoding(self, path, method):
385385
view = self.view
386386

387387
# Core API supports the following request encodings over HTTP...
388-
supported_media_types = set((
388+
supported_media_types = {
389389
'application/json',
390390
'application/x-www-form-urlencoded',
391391
'multipart/form-data',
392-
))
392+
}
393393
parser_classes = getattr(view, 'parser_classes', [])
394394
for parser_class in parser_classes:
395395
media_type = getattr(parser_class, 'media_type', None)

Diff for: rest_framework/serializers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1172,13 +1172,13 @@ def build_standard_field(self, field_name, model_field):
11721172
# Some model fields may introduce kwargs that would not be valid
11731173
# for the choice field. We need to strip these out.
11741174
# Eg. models.DecimalField(max_digits=3, decimal_places=1, choices=DECIMAL_CHOICES)
1175-
valid_kwargs = set((
1175+
valid_kwargs = {
11761176
'read_only', 'write_only',
11771177
'required', 'default', 'initial', 'source',
11781178
'label', 'help_text', 'style',
11791179
'error_messages', 'validators', 'allow_null', 'allow_blank',
11801180
'choices'
1181-
))
1181+
}
11821182
for key in list(field_kwargs.keys()):
11831183
if key not in valid_kwargs:
11841184
field_kwargs.pop(key)

Diff for: tests/test_fields.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1618,15 +1618,15 @@ class TestMultipleChoiceField(FieldValues):
16181618
"""
16191619
valid_inputs = {
16201620
(): set(),
1621-
('aircon',): set(['aircon']),
1622-
('aircon', 'manual'): set(['aircon', 'manual']),
1621+
('aircon',): {'aircon'},
1622+
('aircon', 'manual'): {'aircon', 'manual'},
16231623
}
16241624
invalid_inputs = {
16251625
'abc': ['Expected a list of items but got type "str".'],
16261626
('aircon', 'incorrect'): ['"incorrect" is not a valid choice.']
16271627
}
16281628
outputs = [
1629-
(['aircon', 'manual', 'incorrect'], set(['aircon', 'manual', 'incorrect']))
1629+
(['aircon', 'manual', 'incorrect'], {'aircon', 'manual', 'incorrect'})
16301630
]
16311631
field = serializers.MultipleChoiceField(
16321632
choices=[

Diff for: tests/test_multitable_inheritance.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_multitable_inherited_model_fields_as_expected(self):
4444
"""
4545
child = ChildModel(name1='parent name', name2='child name')
4646
serializer = DerivedModelSerializer(child)
47-
assert set(serializer.data.keys()) == set(['name1', 'name2', 'id'])
47+
assert set(serializer.data.keys()) == {'name1', 'name2', 'id'}
4848

4949
def test_onetoone_primary_key_model_fields_as_expected(self):
5050
"""
@@ -54,7 +54,7 @@ def test_onetoone_primary_key_model_fields_as_expected(self):
5454
parent = ParentModel.objects.create(name1='parent name')
5555
associate = AssociatedModel.objects.create(name='hello', ref=parent)
5656
serializer = AssociatedModelSerializer(associate)
57-
assert set(serializer.data.keys()) == set(['name', 'ref'])
57+
assert set(serializer.data.keys()) == {'name', 'ref'}
5858

5959
def test_data_is_valid_without_parent_ptr(self):
6060
"""

Diff for: tests/test_one_to_one_with_inheritance.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ def test_multitable_inherited_model_fields_as_expected(self):
4141
child = ChildModel(name1='parent name', name2='child name')
4242
serializer = DerivedModelSerializer(child)
4343
self.assertEqual(set(serializer.data.keys()),
44-
set(['name1', 'name2', 'id', 'childassociatedmodel']))
44+
{'name1', 'name2', 'id', 'childassociatedmodel'})

Diff for: tests/test_renderers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def test_render_queryset_values_list(self):
316316
def test_render_dict_abc_obj(self):
317317
class Dict(MutableMapping):
318318
def __init__(self):
319-
self._dict = dict()
319+
self._dict = {}
320320

321321
def __getitem__(self, key):
322322
return self._dict.__getitem__(key)

Diff for: tests/test_serializer_nested.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ def test_nested_serializer_with_list_json(self):
194194
serializer = self.Serializer(data=input_data)
195195

196196
assert serializer.is_valid()
197-
assert serializer.validated_data['nested']['example'] == set([1, 2])
197+
assert serializer.validated_data['nested']['example'] == {1, 2}
198198

199199
def test_nested_serializer_with_list_multipart(self):
200200
input_data = QueryDict('nested.example=1&nested.example=2')
201201
serializer = self.Serializer(data=input_data)
202202

203203
assert serializer.is_valid()
204-
assert serializer.validated_data['nested']['example'] == set([1, 2])
204+
assert serializer.validated_data['nested']['example'] == {1, 2}

0 commit comments

Comments
 (0)