diff options
| author | ElizabethU <elizabeth.uselton@gmail.com> | 2019-09-02 19:09:31 -0700 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-10-01 17:58:19 +0200 |
| commit | 54ea290e5bbd19d87bd8dba807738eeeaf01a362 (patch) | |
| tree | d83c186bde9f50faa13840e6ee227e3bb1e02ad6 /django | |
| parent | 6475e6318c970359a2f02798910a917229ee17d7 (diff) | |
Fixed #30651 -- Made __eq__() methods return NotImplemented for not implemented comparisons.
Changed __eq__ to return NotImplemented instead of False if compared to
an object of the same type, as is recommended by the Python data model
reference. Now these models can be compared to ANY (or other objects
with __eq__ overwritten) without returning False automatically.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/messages/storage/base.py | 5 | ||||
| -rw-r--r-- | django/contrib/postgres/constraints.py | 15 | ||||
| -rw-r--r-- | django/core/validators.py | 3 | ||||
| -rw-r--r-- | django/db/models/base.py | 2 | ||||
| -rw-r--r-- | django/db/models/constraints.py | 21 | ||||
| -rw-r--r-- | django/db/models/expressions.py | 4 | ||||
| -rw-r--r-- | django/db/models/indexes.py | 4 | ||||
| -rw-r--r-- | django/db/models/query.py | 4 | ||||
| -rw-r--r-- | django/db/models/query_utils.py | 3 | ||||
| -rw-r--r-- | django/template/context.py | 10 |
10 files changed, 39 insertions, 32 deletions
diff --git a/django/contrib/messages/storage/base.py b/django/contrib/messages/storage/base.py index fd5d0c24aa..b2eeac77f4 100644 --- a/django/contrib/messages/storage/base.py +++ b/django/contrib/messages/storage/base.py @@ -25,8 +25,9 @@ class Message: self.extra_tags = str(self.extra_tags) if self.extra_tags is not None else None def __eq__(self, other): - return isinstance(other, Message) and self.level == other.level and \ - self.message == other.message + if not isinstance(other, Message): + return NotImplemented + return self.level == other.level and self.message == other.message def __str__(self): return str(self.message) diff --git a/django/contrib/postgres/constraints.py b/django/contrib/postgres/constraints.py index 2fcb076ecf..67e415ddcf 100644 --- a/django/contrib/postgres/constraints.py +++ b/django/contrib/postgres/constraints.py @@ -89,13 +89,14 @@ class ExclusionConstraint(BaseConstraint): return path, args, kwargs def __eq__(self, other): - return ( - isinstance(other, self.__class__) and - self.name == other.name and - self.index_type == other.index_type and - self.expressions == other.expressions and - self.condition == other.condition - ) + if isinstance(other, self.__class__): + return ( + self.name == other.name and + self.index_type == other.index_type and + self.expressions == other.expressions and + self.condition == other.condition + ) + return super().__eq__(other) def __repr__(self): return '<%s: index_type=%s, expressions=%s%s>' % ( diff --git a/django/core/validators.py b/django/core/validators.py index 2e00ca3ff3..38345a844f 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -324,8 +324,9 @@ class BaseValidator: raise ValidationError(self.message, code=self.code, params=params) def __eq__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented return ( - isinstance(other, self.__class__) and self.limit_value == other.limit_value and self.message == other.message and self.code == other.code diff --git a/django/db/models/base.py b/django/db/models/base.py index 0b8425aa85..0a5e5ff673 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -522,7 +522,7 @@ class Model(metaclass=ModelBase): def __eq__(self, other): if not isinstance(other, Model): - return False + return NotImplemented if self._meta.concrete_model != other._meta.concrete_model: return False my_pk = self.pk diff --git a/django/db/models/constraints.py b/django/db/models/constraints.py index e7f81d3ee9..fe0d42a168 100644 --- a/django/db/models/constraints.py +++ b/django/db/models/constraints.py @@ -54,11 +54,9 @@ class CheckConstraint(BaseConstraint): return "<%s: check='%s' name=%r>" % (self.__class__.__name__, self.check, self.name) def __eq__(self, other): - return ( - isinstance(other, CheckConstraint) and - self.name == other.name and - self.check == other.check - ) + if isinstance(other, CheckConstraint): + return self.name == other.name and self.check == other.check + return super().__eq__(other) def deconstruct(self): path, args, kwargs = super().deconstruct() @@ -106,12 +104,13 @@ class UniqueConstraint(BaseConstraint): ) def __eq__(self, other): - return ( - isinstance(other, UniqueConstraint) and - self.name == other.name and - self.fields == other.fields and - self.condition == other.condition - ) + if isinstance(other, UniqueConstraint): + return ( + self.name == other.name and + self.fields == other.fields and + self.condition == other.condition + ) + return super().__eq__(other) def deconstruct(self): path, args, kwargs = super().deconstruct() diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 2b59dd301a..5df765b626 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -401,7 +401,9 @@ class BaseExpression: return tuple(identity) def __eq__(self, other): - return isinstance(other, BaseExpression) and other.identity == self.identity + if not isinstance(other, BaseExpression): + return NotImplemented + return other.identity == self.identity def __hash__(self): return hash(self.identity) diff --git a/django/db/models/indexes.py b/django/db/models/indexes.py index b156366764..49f4989462 100644 --- a/django/db/models/indexes.py +++ b/django/db/models/indexes.py @@ -112,4 +112,6 @@ class Index: ) def __eq__(self, other): - return (self.__class__ == other.__class__) and (self.deconstruct() == other.deconstruct()) + if self.__class__ == other.__class__: + return self.deconstruct() == other.deconstruct() + return NotImplemented diff --git a/django/db/models/query.py b/django/db/models/query.py index 4417c17592..794e0faae7 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1543,7 +1543,9 @@ class Prefetch: return None def __eq__(self, other): - return isinstance(other, Prefetch) and self.prefetch_to == other.prefetch_to + if not isinstance(other, Prefetch): + return NotImplemented + return self.prefetch_to == other.prefetch_to def __hash__(self): return hash((self.__class__, self.prefetch_to)) diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 7a667814f4..189fb4fa44 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -309,8 +309,9 @@ class FilteredRelation: self.path = [] def __eq__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented return ( - isinstance(other, self.__class__) and self.relation_name == other.relation_name and self.alias == other.alias and self.condition == other.condition diff --git a/django/template/context.py b/django/template/context.py index 8f349a3a96..f0a0cf2a00 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -124,12 +124,10 @@ class BaseContext: """ Compare two contexts by comparing theirs 'dicts' attributes. """ - return ( - isinstance(other, BaseContext) and - # because dictionaries can be put in different order - # we have to flatten them like in templates - self.flatten() == other.flatten() - ) + if not isinstance(other, BaseContext): + return NotImplemented + # flatten dictionaries because they can be put in a different order. + return self.flatten() == other.flatten() class Context(BaseContext): |
