1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Simplified various __eq__() methods.

This commit is contained in:
Mads Jensen 2017-09-28 11:39:12 +02:00 committed by Tim Graham
parent 129f4900be
commit 4508fafe16
8 changed files with 22 additions and 29 deletions

View File

@ -7,9 +7,10 @@ class WKTAdapter:
self.srid = geom.srid
def __eq__(self, other):
if not isinstance(other, WKTAdapter):
return False
return self.wkt == other.wkt and self.srid == other.srid
return (
isinstance(other, WKTAdapter) and
self.wkt == other.wkt and self.srid == other.srid
)
def __hash__(self):
return hash((self.wkt, self.srid))

View File

@ -34,9 +34,7 @@ class PostGISAdapter:
raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?')
def __eq__(self, other):
if not isinstance(other, PostGISAdapter):
return False
return self.ewkb == other.ewkb
return isinstance(other, PostGISAdapter) and self.ewkb == other.ewkb
def __hash__(self):
return hash(self.ewkb)

View File

@ -190,10 +190,7 @@ class OGRGeometry(GDALBase):
def __eq__(self, other):
"Is this Geometry equal to the other?"
if isinstance(other, OGRGeometry):
return self.equals(other)
else:
return False
return isinstance(other, OGRGeometry) and self.equals(other)
def __str__(self):
"WKT is used for the string representation."

View File

@ -58,9 +58,11 @@ class Migration:
self.replaces = list(self.__class__.replaces)
def __eq__(self, other):
if not isinstance(other, Migration):
return False
return (self.name == other.name) and (self.app_label == other.app_label)
return (
isinstance(other, Migration) and
self.name == other.name and
self.app_label == other.app_label
)
def __repr__(self):
return "<Migration %s.%s>" % (self.app_label, self.name)

View File

@ -1413,9 +1413,7 @@ class Prefetch:
return None
def __eq__(self, other):
if isinstance(other, Prefetch):
return self.prefetch_to == other.prefetch_to
return False
return isinstance(other, Prefetch) and self.prefetch_to == other.prefetch_to
def __hash__(self):
return hash(self.__class__) ^ hash(self.prefetch_to)

View File

@ -126,10 +126,8 @@ class Origin:
return self.name
def __eq__(self, other):
if not isinstance(other, Origin):
return False
return (
isinstance(other, Origin) and
self.name == other.name and
self.loader == other.loader
)

View File

@ -127,13 +127,12 @@ class BaseContext:
"""
Compare two contexts by comparing theirs 'dicts' attributes.
"""
if isinstance(other, BaseContext):
return (
isinstance(other, BaseContext) and
# because dictionaries can be put in different order
# we have to flatten them like in templates
return self.flatten() == other.flatten()
# if it's not comparable return false
return False
self.flatten() == other.flatten()
)
class Context(BaseContext):

View File

@ -64,11 +64,11 @@ class Node:
return other in self.children
def __eq__(self, other):
if self.__class__ != other.__class__:
return False
if (self.connector, self.negated) == (other.connector, other.negated):
return self.children == other.children
return False
return (
self.__class__ == other.__class__ and
(self.connector, self.negated) == (other.connector, other.negated) and
self.children == other.children
)
def add(self, data, conn_type, squash=True):
"""