Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dict and set literals instead of calls to dict() and set() #5559

Merged
merged 1 commit into from Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion rest_framework/decorators.py
Expand Up @@ -46,7 +46,7 @@ def decorator(func):
assert isinstance(http_method_names, (list, tuple)), \
'@api_view expected a list of strings, received %s' % type(http_method_names).__name__

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

def handler(self, *args, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions rest_framework/schemas/generators.py
Expand Up @@ -118,9 +118,9 @@ def distribute_links(obj):


def is_custom_action(action):
return action not in set([
return action not in {
'retrieve', 'list', 'create', 'update', 'partial_update', 'destroy'
])
}


def endpoint_ordering(endpoint):
Expand Down
4 changes: 2 additions & 2 deletions rest_framework/schemas/inspectors.py
Expand Up @@ -385,11 +385,11 @@ def get_encoding(self, path, method):
view = self.view

# Core API supports the following request encodings over HTTP...
supported_media_types = set((
supported_media_types = {
'application/json',
'application/x-www-form-urlencoded',
'multipart/form-data',
))
}
parser_classes = getattr(view, 'parser_classes', [])
for parser_class in parser_classes:
media_type = getattr(parser_class, 'media_type', None)
Expand Down
4 changes: 2 additions & 2 deletions rest_framework/serializers.py
Expand Up @@ -1172,13 +1172,13 @@ def build_standard_field(self, field_name, model_field):
# Some model fields may introduce kwargs that would not be valid
# for the choice field. We need to strip these out.
# Eg. models.DecimalField(max_digits=3, decimal_places=1, choices=DECIMAL_CHOICES)
valid_kwargs = set((
valid_kwargs = {
'read_only', 'write_only',
'required', 'default', 'initial', 'source',
'label', 'help_text', 'style',
'error_messages', 'validators', 'allow_null', 'allow_blank',
'choices'
))
}
for key in list(field_kwargs.keys()):
if key not in valid_kwargs:
field_kwargs.pop(key)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_fields.py
Expand Up @@ -1605,15 +1605,15 @@ class TestMultipleChoiceField(FieldValues):
"""
valid_inputs = {
(): set(),
('aircon',): set(['aircon']),
('aircon', 'manual'): set(['aircon', 'manual']),
('aircon',): {'aircon'},
('aircon', 'manual'): {'aircon', 'manual'},
}
invalid_inputs = {
'abc': ['Expected a list of items but got type "str".'],
('aircon', 'incorrect'): ['"incorrect" is not a valid choice.']
}
outputs = [
(['aircon', 'manual', 'incorrect'], set(['aircon', 'manual', 'incorrect']))
(['aircon', 'manual', 'incorrect'], {'aircon', 'manual', 'incorrect'})
]
field = serializers.MultipleChoiceField(
choices=[
Expand Down
4 changes: 2 additions & 2 deletions tests/test_multitable_inheritance.py
Expand Up @@ -44,7 +44,7 @@ def test_multitable_inherited_model_fields_as_expected(self):
"""
child = ChildModel(name1='parent name', name2='child name')
serializer = DerivedModelSerializer(child)
assert set(serializer.data.keys()) == set(['name1', 'name2', 'id'])
assert set(serializer.data.keys()) == {'name1', 'name2', 'id'}

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

def test_data_is_valid_without_parent_ptr(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_one_to_one_with_inheritance.py
Expand Up @@ -41,4 +41,4 @@ def test_multitable_inherited_model_fields_as_expected(self):
child = ChildModel(name1='parent name', name2='child name')
serializer = DerivedModelSerializer(child)
self.assertEqual(set(serializer.data.keys()),
set(['name1', 'name2', 'id', 'childassociatedmodel']))
{'name1', 'name2', 'id', 'childassociatedmodel'})
2 changes: 1 addition & 1 deletion tests/test_renderers.py
Expand Up @@ -316,7 +316,7 @@ def test_render_queryset_values_list(self):
def test_render_dict_abc_obj(self):
class Dict(MutableMapping):
def __init__(self):
self._dict = dict()
self._dict = {}

def __getitem__(self, key):
return self._dict.__getitem__(key)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_serializer_nested.py
Expand Up @@ -194,11 +194,11 @@ def test_nested_serializer_with_list_json(self):
serializer = self.Serializer(data=input_data)

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

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

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