Skip to content

Commit 733ab25

Browse files
authored
PYTHON-2466 Make pymongo client, database and collection objects hashable. (#533)
1 parent eb5bd9c commit 733ab25

File tree

6 files changed

+19
-0
lines changed

6 files changed

+19
-0
lines changed

pymongo/collection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ def __eq__(self, other):
303303
def __ne__(self, other):
304304
return not self == other
305305

306+
def __hash__(self):
307+
return hash((self.__database, self.__name))
308+
306309
@property
307310
def full_name(self):
308311
"""The full name of this :class:`Collection`.

pymongo/database.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ def __eq__(self, other):
272272
def __ne__(self, other):
273273
return not self == other
274274

275+
def __hash__(self):
276+
return hash((self.__client, self.__name))
277+
275278
def __repr__(self):
276279
return "Database(%r, %r)" % (self.__client, self.__name)
277280

pymongo/mongo_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,9 @@ def __eq__(self, other):
15091509
def __ne__(self, other):
15101510
return not self == other
15111511

1512+
def __hash__(self):
1513+
return hash(self.address)
1514+
15121515
def _repr_helper(self):
15131516
def option_repr(option, value):
15141517
"""Fix options whose __repr__ isn't usable in a constructor."""

test/test_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,10 @@ def test_equality(self):
627627
# Explicitly test inequality
628628
self.assertFalse(client_context.client != c)
629629

630+
def test_hashable(self):
631+
c = connected(rs_or_single_client())
632+
self.assertIn(c, {client_context.client})
633+
630634
def test_host_w_port(self):
631635
with self.assertRaises(ValueError):
632636
connected(MongoClient("%s:1234567" % (client_context.host,),

test/test_collection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ def test_equality(self):
154154
self.assertEqual(self.db.test.mike, self.db["test.mike"])
155155
self.assertEqual(self.db.test["mike"], self.db["test.mike"])
156156

157+
def test_hashable(self):
158+
self.assertIn(self.db.test.mike, {self.db["test.mike"]})
159+
157160
@client_context.require_version_min(3, 3, 9)
158161
def test_create(self):
159162
# No Exception.

test/test_database.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def test_equality(self):
126126
self.assertFalse(Database(self.client, "test") !=
127127
Database(self.client, "test"))
128128

129+
def test_hashable(self):
130+
self.assertIn(self.client.test, {Database(self.client, "test")})
131+
129132
def test_get_coll(self):
130133
db = Database(self.client, "pymongo_test")
131134
self.assertEqual(db.test, db["test"])

0 commit comments

Comments
 (0)