From 1cfd42d46c13bc2ad7639cc8e642ee79e8e30af7 Mon Sep 17 00:00:00 2001 From: Christian Kreuzberger Date: Tue, 17 Apr 2018 16:06:17 +0200 Subject: [PATCH] Avoid using a mutable as a default parameter --- rest_framework/fields.py | 2 +- rest_framework/utils/html.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 38aac3640..39050ff87 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1623,7 +1623,7 @@ class ListField(Field): List of dicts of native values <- List of dicts of primitive datatypes. """ if html.is_html_input(data): - data = html.parse_html_list(data) + data = html.parse_html_list(data, default=[]) if isinstance(data, type('')) or isinstance(data, collections.Mapping) or not hasattr(data, '__iter__'): self.fail('not_a_list', input_type=type(data).__name__) if not self.allow_empty and len(data) == 0: diff --git a/rest_framework/utils/html.py b/rest_framework/utils/html.py index 8fd6af567..c7ede7803 100644 --- a/rest_framework/utils/html.py +++ b/rest_framework/utils/html.py @@ -12,7 +12,7 @@ def is_html_input(dictionary): return hasattr(dictionary, 'getlist') -def parse_html_list(dictionary, prefix='', default=[]): +def parse_html_list(dictionary, prefix='', default=None): """ Used to support list values in HTML forms. Supports lists of primitives and/or dictionaries. @@ -61,7 +61,9 @@ def parse_html_list(dictionary, prefix='', default=[]): ret[index][key] = value else: ret[index] = MultiValueDict({key: [value]}) - return [ret[item] for item in sorted(ret)] if len(ret.keys()) > 0 else default + + # return the items of the ``ret`` dict, sorted by key, or ``default`` if the dict is empty + return [ret[item] for item in sorted(ret)] if ret else default def parse_html_dict(dictionary, prefix=''):