Skip to content

Commit

Permalink
Reduced reduce() usage; refs #23796.
Browse files Browse the repository at this point in the history
django.core.exceptions.ValidationError.messages() and
django.db.backends.schema.BaseDatabaseSchemaEditor._alter_field():
Replaced reduce(operator.add, ...) w/uncoupled, explicit sum()
  • Loading branch information
Brad Walker authored and timgraham committed Nov 20, 2014
1 parent f273ced commit cfa26f2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 9 deletions.
5 changes: 1 addition & 4 deletions django/core/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"""
Global Django exception and warning classes.
"""
from functools import reduce
import operator

from django.utils import six
from django.utils.encoding import force_text

Expand Down Expand Up @@ -137,7 +134,7 @@ def message_dict(self):
@property
def messages(self):
if hasattr(self, 'error_dict'):
return reduce(operator.add, dict(self).values())
return sum(dict(self).values(), [])
return list(self)

def update_error_dict(self, error_dict):
Expand Down
4 changes: 1 addition & 3 deletions django/db/backends/schema.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import hashlib
import operator

from django.db.backends.creation import BaseDatabaseCreation
from django.db.backends.utils import truncate_name
from django.db.models.fields.related import ManyToManyField
from django.db.transaction import atomic
from django.utils.encoding import force_bytes
from django.utils.log import getLogger
from django.utils.six.moves import reduce
from django.utils import six

logger = getLogger('django.db.backends.schema')
Expand Down Expand Up @@ -609,7 +607,7 @@ def _alter_field(self, model, old_field, new_field, old_type, new_type,
# Combine actions together if we can (e.g. postgres)
if self.connection.features.supports_combined_alters and actions:
sql, params = tuple(zip(*actions))
actions = [(", ".join(sql), reduce(operator.add, params))]
actions = [(", ".join(sql), sum(params, []))]
# Apply those actions
for sql, params in actions:
self.execute(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_exceptions/test_validation_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class TestValidationError(unittest.TestCase):
def test_messages_concatenates_error_dict_values(self):
message_dict = {}
with self.assertRaises(TypeError):
ValidationError(message_dict).messages
exception = ValidationError(message_dict)
self.assertEqual(sorted(exception.messages), [])
message_dict['field1'] = ['E1', 'E2']
exception = ValidationError(message_dict)
self.assertEqual(sorted(exception.messages), ['E1', 'E2'])
Expand Down

0 comments on commit cfa26f2

Please sign in to comment.