diff options
| author | David Smith <smithdc@gmail.com> | 2020-07-21 20:05:33 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-07-29 12:04:13 +0200 |
| commit | 95da207bdb7550922781bbdb3a7b993db0dfc4cf (patch) | |
| tree | c48490f9c8f42d096df4c29a4cc3bfaf25a4a895 /django | |
| parent | 16218c20606d8cd89c5393970c83da04598a3e04 (diff) | |
Fixed #28507 -- Made ValidationError.__eq__() ignore messages and params ordering.
Co-authored-by: caleb logan <clogan202@gmail.com>
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/exceptions.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/django/core/exceptions.py b/django/core/exceptions.py index dc084b8692..2b3e55fe49 100644 --- a/django/core/exceptions.py +++ b/django/core/exceptions.py @@ -1,6 +1,9 @@ """ Global Django exception and warning classes. """ +import operator + +from django.utils.hashable import make_hashable class FieldDoesNotExist(Exception): @@ -182,6 +185,23 @@ class ValidationError(Exception): def __repr__(self): return 'ValidationError(%s)' % self + def __eq__(self, other): + if not isinstance(other, ValidationError): + return NotImplemented + return hash(self) == hash(other) + + def __hash__(self): + # Ignore params and messages ordering. + if hasattr(self, 'message'): + return hash(( + self.message, + self.code, + tuple(sorted(make_hashable(self.params))) if self.params else None, + )) + if hasattr(self, 'error_dict'): + return hash(tuple(sorted(make_hashable(self.error_dict)))) + return hash(tuple(sorted(self.error_list, key=operator.attrgetter('message')))) + class EmptyResultSet(Exception): """A database query predicate is impossible.""" |
