Skip to content

Commit a2e39ad

Browse files
authored
PYTHON-5596 Fix return type for distinct methods (#2576)
1 parent 4697436 commit a2e39ad

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ lint-manual *args="": && resync
5858

5959
[group('test')]
6060
test *args="-v --durations=5 --maxfail=10": && resync
61-
uvx --extra test pytest {{args}}
61+
uv run --extra test pytest {{args}}
6262

6363
[group('test')]
6464
run-tests *args: && resync

pymongo/asynchronous/collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3143,7 +3143,7 @@ async 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/asynchronous/cursor.py

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

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

pymongo/synchronous/collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3136,7 +3136,7 @@ def distinct(
31363136
comment: Optional[Any] = None,
31373137
hint: Optional[_IndexKeyHint] = None,
31383138
**kwargs: Any,
3139-
) -> list[str]:
3139+
) -> list[Any]:
31403140
"""Get a list of distinct values for `key` among all documents
31413141
in this collection.
31423142

pymongo/synchronous/cursor.py

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

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

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)