Skip to content

Commit

Permalink
Add __eq__ and __hash__ to NamedUser (#706)
Browse files Browse the repository at this point in the history
* Add __eq__ and __hash__ to NamedUser

* Remove extra outer brackets from __eq__ in NamedUser

* Add unit tests for __eq__ and __hash__

* Remove redundant brackets in __eq__ : NamedUser
  • Loading branch information
namratachaudhary authored and sfdye committed Feb 21, 2018
1 parent 0e5a1d1 commit 8a13b27
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions github/NamedUser.py
Expand Up @@ -46,6 +46,12 @@ class NamedUser(github.GithubObject.CompletableGithubObject):
def __repr__(self):
return self.get__repr__({"login": self._login.value})

def __hash__(self):
return hash((self.id, self.login))

def __eq__(self, other):
return isinstance(other, type(self)) and self.login == other.login and self.id == other.id

@property
def avatar_url(self):
"""
Expand Down
7 changes: 7 additions & 0 deletions github/tests/NamedUser.py
Expand Up @@ -147,3 +147,10 @@ def testGetReceivedEvents(self):

def testGetKeys(self):
self.assertListKeyEqual(self.user.get_keys(), lambda k: k.id, [3557894, 3791954, 3937333, 4051357, 4051492])

def testUserEquality(self):
u1 = self.g.get_user("nvie")
u2 = self.g.get_user("nvie")
self.assertTrue(u1 == u2)
self.assertEqual(u1, u2)
self.assertEqual(u1.__hash__(), u2.__hash__())
21 changes: 21 additions & 0 deletions github/tests/ReplayData/NamedUser.testUserEquality.txt
@@ -0,0 +1,21 @@
https
GET
api.github.com
None
/users/nvie
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('status', '200 OK'), ('x-ratelimit-remaining', '4994'), ('content-length', '598'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"66a516a2007fb7df8bbb3f9cc7cb2da8"'), ('date', 'Fri, 18 May 2012 19:46:37 GMT'), ('content-type', 'application/json; charset=utf-8')]
{"public_gists":16,"type":"User","hireable":false,"company":"3rd Cloud","url":"https://api.github.com/users/nvie","gravatar_id":"c5a7f21b46df698f3db31c37ed0cf55a","bio":null,"followers":296,"blog":"http://nvie.com","avatar_url":"https://secure.gravatar.com/avatar/c5a7f21b46df698f3db31c37ed0cf55a?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"nvie","email":"vincent@3rdcloud.com","public_repos":61,"html_url":"https://github.com/nvie","name":"Vincent Driessen","created_at":"2009-05-12T21:19:38Z","location":"Netherlands","id":83844,"following":41}

https
GET
api.github.com
None
/users/nvie
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('status', '200 OK'), ('x-ratelimit-remaining', '4994'), ('content-length', '598'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"66a516a2007fb7df8bbb3f9cc7cb2da8"'), ('date', 'Fri, 18 May 2012 19:46:37 GMT'), ('content-type', 'application/json; charset=utf-8')]
{"public_gists":16,"type":"User","hireable":false,"company":"3rd Cloud","url":"https://api.github.com/users/nvie","gravatar_id":"c5a7f21b46df698f3db31c37ed0cf55a","bio":null,"followers":296,"blog":"http://nvie.com","avatar_url":"https://secure.gravatar.com/avatar/c5a7f21b46df698f3db31c37ed0cf55a?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","login":"nvie","email":"vincent@3rdcloud.com","public_repos":61,"html_url":"https://github.com/nvie","name":"Vincent Driessen","created_at":"2009-05-12T21:19:38Z","location":"Netherlands","id":83844,"following":41}

0 comments on commit 8a13b27

Please sign in to comment.