Avoid using a mutable as a default parameter

This commit is contained in:
Christian Kreuzberger 2018-04-17 16:06:17 +02:00
parent 823454da39
commit 1cfd42d46c
2 changed files with 5 additions and 3 deletions

View File

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

View File

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