PYTHON-5596 Fix return type for distinct methods (#2576)

This commit is contained in:
Steven Silvester 2025-10-07 11:04:16 -05:00 committed by GitHub
parent 46974363b4
commit a2e39ada00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 32 additions and 6 deletions

View File

@ -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

View File

@ -3143,7 +3143,7 @@ class AsyncCollection(common.BaseObject, Generic[_DocumentType]):
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.

View File

@ -1063,7 +1063,7 @@ class AsyncCursor(Generic[_DocumentType]):
"""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.

View File

@ -3136,7 +3136,7 @@ class Collection(common.BaseObject, Generic[_DocumentType]):
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.

View File

@ -1061,7 +1061,7 @@ class Cursor(Generic[_DocumentType]):
"""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.

View File

@ -69,7 +69,7 @@ sys.path[0:0] = [""]
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 @@ class TestPymongo(IntegrationTest):
docs = to_list(cursor)
self.assertTrue(docs)
def test_distinct(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({})