Skip to content

PYTHON-6145 Fix the type annotation of the let argument of Cursor - #3138

Open
GoldenEmperor1177 wants to merge 2 commits into
mongodb:mainfrom
GoldenEmperor1177:PYTHON-6145
Open

GoldenEmperor1177 wants to merge 2 commits into
mongodb:mainfrom
GoldenEmperor1177:PYTHON-6145

Conversation

@GoldenEmperor1177

@GoldenEmperor1177 GoldenEmperor1177 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

PYTHON-6145

Changes in this PR

The let argument of Cursor and AsyncCursor was annotated as Optional[bool], but the code requires a document (validate_is_document_type), the same as Collection.find. A document was a mypy error, and a bool, which raises TypeError at runtime, was accepted. The annotation is now Optional[Mapping[str, Any]]. It is one line in pymongo/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 (an only_type_check test) to test/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 on test_typing.py and test_typing_strict.py. mypy over bson, gridfs, tools and pymongo gives 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_objectid and the let and cursor tests in test_crud_unified (sync and async) pass. sphinx -W, ruff, ruff format and the pre-commit hooks pass.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

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.
@GoldenEmperor1177
GoldenEmperor1177 requested a review from a team as a code owner October 22, 2026 16:46

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants