From 4508fafe16e37960769bbe170fd9729910d7d2ce Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Thu, 28 Sep 2017 11:39:12 +0200 Subject: [PATCH] Simplified various __eq__() methods. --- django/contrib/gis/db/backends/base/adapter.py | 7 ++++--- django/contrib/gis/db/backends/postgis/adapter.py | 4 +--- django/contrib/gis/gdal/geometries.py | 5 +---- django/db/migrations/migration.py | 8 +++++--- django/db/models/query.py | 4 +--- django/template/base.py | 4 +--- django/template/context.py | 9 ++++----- django/utils/tree.py | 10 +++++----- 8 files changed, 22 insertions(+), 29 deletions(-) diff --git a/django/contrib/gis/db/backends/base/adapter.py b/django/contrib/gis/db/backends/base/adapter.py index 8f35909d28..604711eb6a 100644 --- a/django/contrib/gis/db/backends/base/adapter.py +++ b/django/contrib/gis/db/backends/base/adapter.py @@ -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)) diff --git a/django/contrib/gis/db/backends/postgis/adapter.py b/django/contrib/gis/db/backends/postgis/adapter.py index 39867ebc61..84c19c7e3b 100644 --- a/django/contrib/gis/db/backends/postgis/adapter.py +++ b/django/contrib/gis/db/backends/postgis/adapter.py @@ -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) diff --git a/django/contrib/gis/gdal/geometries.py b/django/contrib/gis/gdal/geometries.py index 35ce8d5157..d111f8bc44 100644 --- a/django/contrib/gis/gdal/geometries.py +++ b/django/contrib/gis/gdal/geometries.py @@ -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." diff --git a/django/db/migrations/migration.py b/django/db/migrations/migration.py index ffe0b1fb3d..fe5e228c6c 100644 --- a/django/db/migrations/migration.py +++ b/django/db/migrations/migration.py @@ -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 "" % (self.app_label, self.name) diff --git a/django/db/models/query.py b/django/db/models/query.py index 3bfe0a6fb4..03de96ab2f 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -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) diff --git a/django/template/base.py b/django/template/base.py index 6572eae8ab..10a1f11464 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -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 ) diff --git a/django/template/context.py b/django/template/context.py index 5292a0bb19..ec9abf7655 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -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): diff --git a/django/utils/tree.py b/django/utils/tree.py index 74612736f1..392838b482 100644 --- a/django/utils/tree.py +++ b/django/utils/tree.py @@ -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): """