Kaydet (Commit) cfa26f29 authored tarafından Brad Walker's avatar Brad Walker Kaydeden (comit) Tim Graham

Reduced reduce() usage; refs #23796.

django.core.exceptions.ValidationError.messages() and
django.db.backends.schema.BaseDatabaseSchemaEditor._alter_field():
Replaced reduce(operator.add, ...) w/uncoupled, explicit sum()
üst f273cedc
""" """
Global Django exception and warning classes. Global Django exception and warning classes.
""" """
from functools import reduce
import operator
from django.utils import six from django.utils import six
from django.utils.encoding import force_text from django.utils.encoding import force_text
...@@ -137,7 +134,7 @@ class ValidationError(Exception): ...@@ -137,7 +134,7 @@ class ValidationError(Exception):
@property @property
def messages(self): def messages(self):
if hasattr(self, 'error_dict'): if hasattr(self, 'error_dict'):
return reduce(operator.add, dict(self).values()) return sum(dict(self).values(), [])
return list(self) return list(self)
def update_error_dict(self, error_dict): def update_error_dict(self, error_dict):
......
import hashlib import hashlib
import operator
from django.db.backends.creation import BaseDatabaseCreation from django.db.backends.creation import BaseDatabaseCreation
from django.db.backends.utils import truncate_name from django.db.backends.utils import truncate_name
...@@ -7,7 +6,6 @@ from django.db.models.fields.related import ManyToManyField ...@@ -7,7 +6,6 @@ from django.db.models.fields.related import ManyToManyField
from django.db.transaction import atomic from django.db.transaction import atomic
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
from django.utils.log import getLogger from django.utils.log import getLogger
from django.utils.six.moves import reduce
from django.utils import six from django.utils import six
logger = getLogger('django.db.backends.schema') logger = getLogger('django.db.backends.schema')
...@@ -609,7 +607,7 @@ class BaseDatabaseSchemaEditor(object): ...@@ -609,7 +607,7 @@ class BaseDatabaseSchemaEditor(object):
# Combine actions together if we can (e.g. postgres) # Combine actions together if we can (e.g. postgres)
if self.connection.features.supports_combined_alters and actions: if self.connection.features.supports_combined_alters and actions:
sql, params = tuple(zip(*actions)) sql, params = tuple(zip(*actions))
actions = [(", ".join(sql), reduce(operator.add, params))] actions = [(", ".join(sql), sum(params, []))]
# Apply those actions # Apply those actions
for sql, params in actions: for sql, params in actions:
self.execute( self.execute(
......
...@@ -6,8 +6,8 @@ from django.core.exceptions import ValidationError ...@@ -6,8 +6,8 @@ from django.core.exceptions import ValidationError
class TestValidationError(unittest.TestCase): class TestValidationError(unittest.TestCase):
def test_messages_concatenates_error_dict_values(self): def test_messages_concatenates_error_dict_values(self):
message_dict = {} message_dict = {}
with self.assertRaises(TypeError): exception = ValidationError(message_dict)
ValidationError(message_dict).messages self.assertEqual(sorted(exception.messages), [])
message_dict['field1'] = ['E1', 'E2'] message_dict['field1'] = ['E1', 'E2']
exception = ValidationError(message_dict) exception = ValidationError(message_dict)
self.assertEqual(sorted(exception.messages), ['E1', 'E2']) self.assertEqual(sorted(exception.messages), ['E1', 'E2'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment