Repository navigation
Raise the document_class error message for non-class values - #3260
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
validate_document_class guards the first issubclass() call against TypeError and then makes a second issubclass(value, RawBSONDocument) call outside that guard. The second call is only reached when value is not a mapping, which includes not being a class at all, so it raises "issubclass() arg 1 must be a class" from abc and the descriptive TypeError below it is unreachable. Fold both checks into the guarded try so a non-class value reports the option name and the accepted values instead.
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.
validate_document_class()has aTypeErrorguard for exactly this case, and then callsissubclass()a second time outside it:The second call is only reached when
is_mappingis false, which is precisely whenvalueis not a class — so it raises from<frozen abc>before the descriptiveTypeErrortwo lines below can run, and that message is unreachable for the inputs it was written for:Same for an instance instead of a class, a string, or
list[int]:The traceback points at
abcrather than at the option, so anyone passingdocument_class="SON"in a config file gets no indication of which setting is wrong or what the accepted values are. The comment above the first check — "issubclass() raises TypeError for such aliases, so check the origin proactively instead of relying on catching the error" — is describing this hazard, and the second call defeats it.Fix
Fold both
issubclass()calls into the guardedtry, so a non-class value takes the same path as a generic alias:Note
orrather thanand:RawBSONDocumentsubclasses are notMutableMapping, so the second check still has to be able to make the call succeed on its own. Short-circuiting means theissubclass(value, ...)arm only runs when the first isFalse, i.e. exactly in the cases where the guard matters.After the change:
Tests
New
test/test_common_validation.py(pureunittest, no server needed) covering the validator directly: accepted values (dict,SON,RawBSONDocument, aRawBSONDocumentsubclass, and the generic aliasesdict[str, Any]/SON[str, Any]that the existing__origin__handling exists for), and rejected values (42,"dict",list[int], an instance) asserting the descriptive message.mainwith only the test file added:9 failed, 3 passed(the rejected-value cases getissubclass() arg 1 must be a class)4 passed, 19 subtests passedmongodhere): 121 modules before, 122 after;comm -3of the failing sets betweenmainand the patched tree is empty. The 10 pre-existing failures are identical on both trees and are environment-only:test_kms_connect×4 (nopymongocrypt),test_ocsp_support×3 (no OCSP responder),test_sfp×3 (noSFP_ATLAS_X509_URI).dict,SON,RawBSONDocument, a subclass, an instance,42,"dict",list[int],dict[str, int], …) shows only the four illegal inputs changing exception, and the seven legal inputs returning byte-identical values.ruff==0.17.3(the[dependency-groups] lintpin):ruff check .andruff format --check .(466 files) clean on both trees.Ticket
CONTRIBUTING.mdasks for a PYTHON- ticket up front. I searched JIRA and there isn't one for this (validate_document_classhits only PYTHON-4192, which is a different, blocked feature); creating one needs a JIRA account I don't have. Happy to rename the branch and commit, and to add the ticket reference to the changelog, as soon as a number exists — or to close this if you'd rather have it filed first.AI disclosure
Written with an AI coding assistant (Claude), which located the unreachable branch and ran the before/after comparison. The change and the test expectations were reviewed by hand against unpatched
main.