Skip to content

Commit fef10d2

Browse files
PYTHON-5596 Fix return type for distinct methods (#2576) [v4.15] (#2583)
Co-authored-by: Steven Silvester <[email protected]>
1 parent 039c35b commit fef10d2

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

pymongo/asynchronous/collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3150,7 +3150,7 @@ async def distinct(
31503150
comment: Optional[Any] = None,
31513151
hint: Optional[_IndexKeyHint] = None,
31523152
**kwargs: Any,
3153-
) -> list[str]:
3153+
) -> list[Any]:
31543154
"""Get a list of distinct values for `key` among all documents
31553155
in this collection.
31563156

pymongo/asynchronous/cursor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ async def close(self) -> None:
10641064
"""Explicitly close / kill this cursor."""
10651065
await self._die_lock()
10661066

1067-
async def distinct(self, key: str) -> list[str]:
1067+
async def distinct(self, key: str) -> list[Any]:
10681068
"""Get a list of distinct values for `key` among all documents
10691069
in the result set of this query.
10701070

pymongo/synchronous/collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3143,7 +3143,7 @@ def distinct(
31433143
comment: Optional[Any] = None,
31443144
hint: Optional[_IndexKeyHint] = None,
31453145
**kwargs: Any,
3146-
) -> list[str]:
3146+
) -> list[Any]:
31473147
"""Get a list of distinct values for `key` among all documents
31483148
in this collection.
31493149

pymongo/synchronous/cursor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ def close(self) -> None:
10621062
"""Explicitly close / kill this cursor."""
10631063
self._die_lock()
10641064

1065-
def distinct(self, key: str) -> list[str]:
1065+
def distinct(self, key: str) -> list[Any]:
10661066
"""Get a list of distinct values for `key` among all documents
10671067
in the result set of this query.
10681068

test/test_typing.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ImplicitMovie(TypedDict):
6969

7070
from test import IntegrationTest, PyMongoTestCase, client_context
7171

72-
from bson import CodecOptions, decode, decode_all, decode_file_iter, decode_iter, encode
72+
from bson import CodecOptions, ObjectId, decode, decode_all, decode_file_iter, decode_iter, encode
7373
from bson.raw_bson import RawBSONDocument
7474
from bson.son import SON
7575
from pymongo import ASCENDING, MongoClient
@@ -141,6 +141,32 @@ def to_list(iterable: Iterable[Dict[str, Any]]) -> List[Dict[str, Any]]:
141141
docs = to_list(cursor)
142142
self.assertTrue(docs)
143143

144+
def test_distinct(self) -> None:
145+
self.coll.delete_many({})
146+
self.coll.insert_many(
147+
[
148+
{"_id": None},
149+
{"_id": 0},
150+
{"_id": ""},
151+
{"_id": ObjectId()},
152+
{"_id": True},
153+
]
154+
)
155+
156+
def collection_distinct(
157+
collection: Collection,
158+
) -> list[None | int | str | ObjectId | bool]:
159+
return collection.distinct("_id")
160+
161+
def cursor_distinct(
162+
collection: Collection,
163+
) -> list[None | int | str | ObjectId | bool]:
164+
cursor = collection.find()
165+
return cursor.distinct("_id")
166+
167+
collection_distinct(self.coll)
168+
cursor_distinct(self.coll)
169+
144170
@only_type_check
145171
def test_bulk_write(self) -> None:
146172
self.coll.insert_one({})

0 commit comments

Comments
 (0)