Repository navigation
PYTHON-6145 Fix the type annotation of the let argument of Cursor - #3138
Open
GoldenEmperor1177 wants to merge 2 commits into
Open
GoldenEmperor1177 wants to merge 2 commits into
GoldenEmperor1177 wants to merge 2 commits into
Conversation
The let argument of Cursor and AsyncCursor was annotated as Optional[bool], but the code requires a document, as for Collection.find. Annotate it as Optional[Mapping[str, Any]] and add a type checking test.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PYTHON-6145
Changes in this PR
The
letargument ofCursorandAsyncCursorwas annotated asOptional[bool], but the code requires a document (validate_is_document_type), the same asCollection.find. A document was a mypy error, and a bool, which raisesTypeErrorat runtime, was accepted. The annotation is nowOptional[Mapping[str, Any]]. It is one line inpymongo/cursor_shared.py, shared by both cursor classes, so there is no runtime change. Added a changelog entry and a type checking test.AI disclosure: I used an AI assistant (Claude Code) to help find the cause, write the fix and the tests. I read the change and I understand it.
Test Plan
Added
test_cursor_let(anonly_type_checktest) totest/test_typing.py. With the old annotation, mypy 1.19.1 and pyright 1.1.411 both report an error for it, and with the new one both pass ontest_typing.pyandtest_typing_strict.py. mypy overbson,gridfs,toolsandpymongogives the same 41 errors with and without the change, all from optional packages missing in my environment. Against a local MongoDB 7.0,test_typing,test_cursor,test_collection,test_objectidand theletand cursor tests intest_crud_unified(sync and async) pass.sphinx -W,ruff,ruff formatand the pre-commit hooks pass.Checklist
Checklist for Author
Checklist for Reviewer