From 07e453efeac41a90bad9af05b9bf22c0125eccca Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 6 Oct 2025 05:32:55 -0500 Subject: [PATCH 1/2] PYTHON-5596 Fix return type for distinct methods --- justfile | 2 +- pymongo/asynchronous/collection.py | 2 +- pymongo/asynchronous/cursor.py | 2 +- pymongo/synchronous/collection.py | 2 +- pymongo/synchronous/cursor.py | 2 +- test/test_typing.py | 28 +++++++++++++++++++++++++++- 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/justfile b/justfile index f235346160..17b95e87b7 100644 --- a/justfile +++ b/justfile @@ -58,7 +58,7 @@ lint-manual *args="": && resync [group('test')] test *args="-v --durations=5 --maxfail=10": && resync - uvx --extra test pytest {{args}} + uv run --extra test pytest {{args}} [group('test')] run-tests *args: && resync diff --git a/pymongo/asynchronous/collection.py b/pymongo/asynchronous/collection.py index 6af1f4f782..e7e2f58031 100644 --- a/pymongo/asynchronous/collection.py +++ b/pymongo/asynchronous/collection.py @@ -3143,7 +3143,7 @@ async def distinct( comment: Optional[Any] = None, hint: Optional[_IndexKeyHint] = None, **kwargs: Any, - ) -> list[str]: + ) -> list[Any]: """Get a list of distinct values for `key` among all documents in this collection. diff --git a/pymongo/asynchronous/cursor.py b/pymongo/asynchronous/cursor.py index cf3a5372b4..f19d3f6cee 100644 --- a/pymongo/asynchronous/cursor.py +++ b/pymongo/asynchronous/cursor.py @@ -1063,7 +1063,7 @@ async def close(self) -> None: """Explicitly close / kill this cursor.""" await self._die_lock() - async def distinct(self, key: str) -> list[str]: + async def distinct(self, key: str) -> list[Any]: """Get a list of distinct values for `key` among all documents in the result set of this query. diff --git a/pymongo/synchronous/collection.py b/pymongo/synchronous/collection.py index b68e4befed..4e5f7d08fb 100644 --- a/pymongo/synchronous/collection.py +++ b/pymongo/synchronous/collection.py @@ -3136,7 +3136,7 @@ def distinct( comment: Optional[Any] = None, hint: Optional[_IndexKeyHint] = None, **kwargs: Any, - ) -> list[str]: + ) -> list[Any]: """Get a list of distinct values for `key` among all documents in this collection. diff --git a/pymongo/synchronous/cursor.py b/pymongo/synchronous/cursor.py index 12e2863bc6..fcd8ebeb1d 100644 --- a/pymongo/synchronous/cursor.py +++ b/pymongo/synchronous/cursor.py @@ -1061,7 +1061,7 @@ def close(self) -> None: """Explicitly close / kill this cursor.""" self._die_lock() - def distinct(self, key: str) -> list[str]: + def distinct(self, key: str) -> list[Any]: """Get a list of distinct values for `key` among all documents in the result set of this query. diff --git a/test/test_typing.py b/test/test_typing.py index 7240e59c06..3b687092b3 100644 --- a/test/test_typing.py +++ b/test/test_typing.py @@ -69,7 +69,7 @@ class ImplicitMovie(TypedDict): from test import IntegrationTest, PyMongoTestCase, client_context -from bson import CodecOptions, decode, decode_all, decode_file_iter, decode_iter, encode +from bson import CodecOptions, ObjectId, decode, decode_all, decode_file_iter, decode_iter, encode from bson.raw_bson import RawBSONDocument from bson.son import SON from pymongo import ASCENDING, MongoClient @@ -141,6 +141,32 @@ def to_list(iterable: Iterable[Dict[str, Any]]) -> List[Dict[str, Any]]: docs = to_list(cursor) self.assertTrue(docs) + def test_district(self) -> None: + self.coll.delete_many({}) + self.coll.insert_many( + [ + {"_id": None}, + {"_id": 0}, + {"_id": ""}, + {"_id": ObjectId()}, + {"_id": True}, + ] + ) + + def collection_distinct( + collection: Collection, + ) -> list[None | int | str | ObjectId | bool]: + return collection.distinct("_id") + + def cursor_distinct( + collection: Collection, + ) -> list[None | int | str | ObjectId | bool]: + cursor = collection.find() + return cursor.distinct("_id") + + collection_distinct(self.coll) + cursor_distinct(self.coll) + @only_type_check def test_bulk_write(self) -> None: self.coll.insert_one({}) From 7575f7e8b55872ce410a625961939655c2d6a521 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 7 Oct 2025 09:28:37 -0500 Subject: [PATCH 2/2] address review --- test/test_typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_typing.py b/test/test_typing.py index 3b687092b3..17dc21b4e0 100644 --- a/test/test_typing.py +++ b/test/test_typing.py @@ -141,7 +141,7 @@ def to_list(iterable: Iterable[Dict[str, Any]]) -> List[Dict[str, Any]]: docs = to_list(cursor) self.assertTrue(docs) - def test_district(self) -> None: + def test_distinct(self) -> None: self.coll.delete_many({}) self.coll.insert_many( [