PYTHON-5653: fix - correct return type annotation for find_one_and_* methods to include None#2615
Merged
caseyclements merged 3 commits intomongodb:masterfrom Nov 25, 2025
Conversation
ShaneHarvey
previously requested changes
Nov 14, 2025
Member
ShaneHarvey
left a comment
There was a problem hiding this comment.
Is this also a bug in the other find_one_and_* methods?
ZachOldham
reviewed
Nov 14, 2025
Contributor
Author
I just checked and it is. I'll update this PR to fix the type hints for those methods shortly. |
064dfaf to
8875123
Compare
find_one_and_update to include Nonefind_one_and_* methods to include None
find_one_and_* methods to include Nonefind_one_and_* methods to include None
caseyclements
approved these changes
Nov 25, 2025
Contributor
caseyclements
left a comment
There was a problem hiding this comment.
I've updated the title with a JIRA ticket. Shane is on leave, but this LGTM!
Shane has just begun a sabbatical.
Contributor
Author
|
Thanks Casey! I see there's some failing checks, but I can't seem to tell what actually failed. Is there anything needed from me or is it something wonky (eg: test infrastructure) on your end? |
Contributor
|
Failures are unrelated. |
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.
Summary
find_one_and_update's existing type hint makes the false claim that the method returns a_DocumentType. However, the docstring (correctly) tells us that the method "Returns ``None`` if no document matches the filter."The same is true of other
find_one_and_*methods.Changes in this PR
This PR corrects the type hint of the
find_one_and_*methods to indicate that the function may return either_DocumentTypeorNone(rather than just_DocumentType).I've also updated the docstrings for the methods to make this behavior clearer.
Depending on what you consider to be the "surface area" of your library's API, this might be considered a breaking change.
I can't think of any code that would break at runtime (since the PR doesn't change runtime behavior), but it's possible someone has written code like the following. This PR would cause the
assert_typeto fail at "type checking time".Testing Plan
This change isn't changing any runtime behavior, so I'm not sure how I'd test it. I'm open to suggestions for how to test this if you have any.
Screenshots (optional)
N/A
Checklist
Checklist for Author
Checklist for Reviewer {@primary_reviewer}
Focus Areas for Reviewer (optional)