Add and use mixins for Status and User
authorHarmon <Harmon758@gmail.com>
Thu, 31 Dec 2020 04:27:27 +0000 (22:27 -0600)
committerHarmon <Harmon758@gmail.com>
Thu, 31 Dec 2020 04:27:27 +0000 (22:27 -0600)
tweepy/mixins.py [new file with mode: 0644]
tweepy/models.py

diff --git a/tweepy/mixins.py b/tweepy/mixins.py
new file mode 100644 (file)
index 0000000..1c3eef6
--- /dev/null
@@ -0,0 +1,19 @@
+# Tweepy
+# Copyright 2009-2020 Joshua Roesslein
+# See LICENSE for details.
+
+class EqualityComparable:
+    __slots__ = ()
+
+    def __eq__(self, other):
+        if isinstance(other, self.__class__):
+            return self.id == other.id
+
+        return NotImplemented
+
+
+class Hashable(EqualityComparable):
+    __slots__ = ()
+
+    def __hash__(self):
+        return self.id
index f29059437bc0b39448c58098afd0256d84e30871..296c162bd71d5ff3f7e08b448500b5b738257c79 100644 (file)
@@ -2,6 +2,7 @@
 # Copyright 2009-2020 Joshua Roesslein
 # See LICENSE for details.
 
+from tweepy.mixins import Hashable
 from tweepy.utils import parse_a_href, parse_datetime, parse_html_value
 
 
@@ -82,7 +83,7 @@ class Model:
         return '%s(%s)' % (self.__class__.__name__, ', '.join(state))
 
 
-class Status(Model):
+class Status(Model, Hashable):
 
     @classmethod
     def parse(cls, api, json):
@@ -128,14 +129,8 @@ class Status(Model):
     def favorite(self):
         return self._api.create_favorite(self.id)
 
-    def __eq__(self, other):
-        if isinstance(other, Status):
-            return self.id == other.id
 
-        return NotImplemented
-
-
-class User(Model):
+class User(Model, Hashable):
 
     @classmethod
     def parse(cls, api, json):
@@ -205,15 +200,6 @@ class User(Model):
                                        *args,
                                        **kwargs)
 
-    def __eq__(self, other):
-        if isinstance(other, User):
-            return self.id == other.id
-
-        return NotImplemented
-
-    def __hash__(self):
-        return self.id
-
 
 class DirectMessage(Model):