Skip to content

PYTHON-2466 Make pymongo client, database and collection objects hashable. #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pymongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __hash__(self):
return hash((self.__database, self.__name))

@property
def full_name(self):
"""The full name of this :class:`Collection`.
Expand Down
3 changes: 3 additions & 0 deletions pymongo/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __hash__(self):
return hash((self.__client, self.__name))

def __repr__(self):
return "Database(%r, %r)" % (self.__client, self.__name)

Expand Down
3 changes: 3 additions & 0 deletions pymongo/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,9 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __hash__(self):
return hash(self.address)

def _repr_helper(self):
def option_repr(option, value):
"""Fix options whose __repr__ isn't usable in a constructor."""
Expand Down
4 changes: 4 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,10 @@ def test_equality(self):
# Explicitly test inequality
self.assertFalse(client_context.client != c)

def test_hashable(self):
c = connected(rs_or_single_client())
self.assertIn(c, {client_context.client})

def test_host_w_port(self):
with self.assertRaises(ValueError):
connected(MongoClient("%s:1234567" % (client_context.host,),
Expand Down
3 changes: 3 additions & 0 deletions test/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ def test_equality(self):
self.assertEqual(self.db.test.mike, self.db["test.mike"])
self.assertEqual(self.db.test["mike"], self.db["test.mike"])

def test_hashable(self):
self.assertIn(self.db.test.mike, {self.db["test.mike"]})

@client_context.require_version_min(3, 3, 9)
def test_create(self):
# No Exception.
Expand Down
3 changes: 3 additions & 0 deletions test/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def test_equality(self):
self.assertFalse(Database(self.client, "test") !=
Database(self.client, "test"))

def test_hashable(self):
self.assertIn(self.client.test, {Database(self.client, "test")})

def test_get_coll(self):
db = Database(self.client, "pymongo_test")
self.assertEqual(db.test, db["test"])
Expand Down