Skip to content

Commit ffe3dbb

Browse files
jdufresnetomchristie
authored andcommitted
Perfer iter(dict) over iter(dict.keys()) (#5736)
Calling dict.keys() is unnecessary. The two are functionally equivalent on modern Pythons. Inspired by Lennart Regebro's talk "Prehistoric Patterns in Python" from PyCon 2017. https://www.youtube.com/watch?v=V5-JH23Vk0I
1 parent c1848d7 commit ffe3dbb

19 files changed

+35
-35
lines changed

docs/api-guide/serializers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ For example, if you wanted to be able to set which fields should be used by a se
10831083
if fields is not None:
10841084
# Drop any fields that are not specified in the `fields` argument.
10851085
allowed = set(fields)
1086-
existing = set(self.fields.keys())
1086+
existing = set(self.fields)
10871087
for field_name in existing - allowed:
10881088
self.fields.pop(field_name)
10891089

rest_framework/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ def _set_choices(self, choices):
14201420
# Allows us to deal with eg. integer choices while supporting either
14211421
# integer or string input, but still get the correct datatype out.
14221422
self.choice_strings_to_values = {
1423-
six.text_type(key): key for key in self.choices.keys()
1423+
six.text_type(key): key for key in self.choices
14241424
}
14251425

14261426
choices = property(_get_choices, _set_choices)

rest_framework/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def get_valid_fields(self, queryset, view, context={}):
218218
]
219219
valid_fields += [
220220
(key, key.title().split('__'))
221-
for key in queryset.query.annotations.keys()
221+
for key in queryset.query.annotations
222222
]
223223
else:
224224
valid_fields = [

rest_framework/relations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def many_init(cls, *args, **kwargs):
133133
return CustomManyRelatedField(*args, **kwargs)
134134
"""
135135
list_kwargs = {'child_relation': cls(*args, **kwargs)}
136-
for key in kwargs.keys():
136+
for key in kwargs:
137137
if key in MANY_RELATION_KWARGS:
138138
list_kwargs[key] = kwargs[key]
139139
return ManyRelatedField(**list_kwargs)

rest_framework/renderers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -805,8 +805,8 @@ def get_context(self, data, accepted_media_type, renderer_context):
805805
header = results
806806
style = 'detail'
807807

808-
columns = [key for key in header.keys() if key != 'url']
809-
details = [key for key in header.keys() if key != 'url']
808+
columns = [key for key in header if key != 'url']
809+
details = [key for key in header if key != 'url']
810810

811811
context['style'] = style
812812
context['columns'] = columns

rest_framework/schemas/generators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def get_allowed_methods(self, callback):
228228
Return a list of the valid HTTP methods for this endpoint.
229229
"""
230230
if hasattr(callback, 'actions'):
231-
actions = set(callback.actions.keys())
231+
actions = set(callback.actions)
232232
http_method_names = set(callback.cls.http_method_names)
233233
methods = [method.upper() for method in actions & http_method_names]
234234
else:

rest_framework/schemas/inspectors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ def field_to_schema(field):
5454
return coreschema.String(title=title, description=description)
5555
elif isinstance(field, serializers.MultipleChoiceField):
5656
return coreschema.Array(
57-
items=coreschema.Enum(enum=list(field.choices.keys())),
57+
items=coreschema.Enum(enum=list(field.choices)),
5858
title=title,
5959
description=description
6060
)
6161
elif isinstance(field, serializers.ChoiceField):
6262
return coreschema.Enum(
63-
enum=list(field.choices.keys()),
63+
enum=list(field.choices),
6464
title=title,
6565
description=description
6666
)

rest_framework/serializers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,9 +1136,9 @@ def get_default_field_names(self, declared_fields, model_info):
11361136
"""
11371137
return (
11381138
[model_info.pk.name] +
1139-
list(declared_fields.keys()) +
1140-
list(model_info.fields.keys()) +
1141-
list(model_info.forward_relations.keys())
1139+
list(declared_fields) +
1140+
list(model_info.fields) +
1141+
list(model_info.forward_relations)
11421142
)
11431143

11441144
# Methods for constructing serializer fields...
@@ -1194,7 +1194,7 @@ def build_standard_field(self, field_name, model_field):
11941194
'error_messages', 'validators', 'allow_null', 'allow_blank',
11951195
'choices'
11961196
}
1197-
for key in list(field_kwargs.keys()):
1197+
for key in list(field_kwargs):
11981198
if key not in valid_kwargs:
11991199
field_kwargs.pop(key)
12001200

@@ -1364,7 +1364,7 @@ def get_uniqueness_extra_kwargs(self, field_names, declared_fields, extra_kwargs
13641364

13651365
# Include each of the `unique_together` field names,
13661366
# so long as all the field names are included on the serializer.
1367-
for parent_class in [model] + list(model._meta.parents.keys()):
1367+
for parent_class in [model] + list(model._meta.parents):
13681368
for unique_together_list in parent_class._meta.unique_together:
13691369
if set(field_names).issuperset(set(unique_together_list)):
13701370
unique_constraint_names |= set(unique_together_list)
@@ -1466,7 +1466,7 @@ def get_unique_together_validators(self):
14661466
"""
14671467
model_class_inheritance_tree = (
14681468
[self.Meta.model] +
1469-
list(self.Meta.model._meta.parents.keys())
1469+
list(self.Meta.model._meta.parents)
14701470
)
14711471

14721472
# The field names we're passing though here only include fields
@@ -1566,9 +1566,9 @@ def get_default_field_names(self, declared_fields, model_info):
15661566
"""
15671567
return (
15681568
[self.url_field_name] +
1569-
list(declared_fields.keys()) +
1570-
list(model_info.fields.keys()) +
1571-
list(model_info.forward_relations.keys())
1569+
list(declared_fields) +
1570+
list(model_info.fields) +
1571+
list(model_info.forward_relations)
15721572
)
15731573

15741574
def build_nested_field(self, field_name, relation_info, nested_depth):

rest_framework/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def _encode_data(self, data, format=None, content_type=None):
175175
"Set TEST_REQUEST_RENDERER_CLASSES to enable "
176176
"extra request formats.".format(
177177
format,
178-
', '.join(["'" + fmt + "'" for fmt in self.renderer_classes.keys()])
178+
', '.join(["'" + fmt + "'" for fmt in self.renderer_classes])
179179
)
180180
)
181181

rest_framework/utils/html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def parse_html_list(dictionary, prefix=''):
5959
ret[index][key] = value
6060
else:
6161
ret[index] = MultiValueDict({key: [value]})
62-
return [ret[item] for item in sorted(ret.keys())]
62+
return [ret[item] for item in sorted(ret)]
6363

6464

6565
def parse_html_dict(dictionary, prefix=''):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy