summaryrefslogtreecommitdiff
path: root/gitlab/base.py
diff options
context:
space:
mode:
authorJonathan Piron <jonathan.piron@cybelangel.com>2019-02-18 14:11:30 +0100
committerJonathan Piron <jonathan.piron@cybelangel.com>2019-02-20 08:48:24 +0100
commit3d60850aa42351a0bb0066ef579ade95df5a81ee (patch)
tree8f3e519f05917b80190b1b141846c61d37daa832 /gitlab/base.py
parent31bca2f9ee55ffa69d34f4584e90da01d3f6325e (diff)
downloadgitlab-3d60850aa42351a0bb0066ef579ade95df5a81ee.tar.gz
Implement __eq__ and __hash__ methods
To ease lists and sets manipulations.
Diffstat (limited to 'gitlab/base.py')
-rw-r--r--gitlab/base.py12
1 files changed, 11 insertions, 1 deletions
diff --git a/gitlab/base.py b/gitlab/base.py
index 7324c31..c3da077 100644
--- a/gitlab/base.py
+++ b/gitlab/base.py
@@ -96,6 +96,16 @@ class RESTObject(object):
else:
return '<%s>' % self.__class__.__name__
+ def __eq__(self, other):
+ if self.get_id() and other.get_id():
+ return self.get_id() == other.get_id()
+ return super().__eq__(other)
+
+ def __hash__(self):
+ if not self.get_id():
+ return super().__hash__()
+ return hash(self.get_id())
+
def _create_managers(self):
managers = getattr(self, '_managers', None)
if managers is None:
@@ -112,7 +122,7 @@ class RESTObject(object):
def get_id(self):
"""Returns the id of the resource."""
- if self._id_attr is None:
+ if self._id_attr is None or not hasattr(self, self._id_attr):
return None
return getattr(self, self._id_attr)