Repository navigation
Avoid exceptions when resolving hooks on schema class creation - #3111
Open
deadstrobe5 wants to merge 2 commits into
Open
deadstrobe5 wants to merge 2 commits into
deadstrobe5 wants to merge 2 commits into
Conversation
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.
SchemaMeta.resolve_hookslooks at every attribute name indir(cls). For each one it finds the defining class withtry: parent.__dict__[attr_name] except KeyErrorand then checksattr.__marshmallow_hook__withtry/except AttributeError. Almost no attribute is a hook, so creating a schema class with 2 fields raises and catches about 145 exceptions. This usesattr_name in parent.__dict__andgetattr(attr, "__marshmallow_hook__", None)instead. The hooks found and their order are the same, and the existing decorator tests cover them.Class creation is usually a one-time cost, but
Schema.from_dictcreates a class on every call. webargs does that for each request that uses a dict argmap (@use_args({...})).CPU time per call, median of 11 alternating rounds in one process, CPython 3.14 on Linux.
Schema.from_dict(argmap)().load(query), 4 fieldsA second run gave 1.50x and 1.71x.
I used Claude Code (Claude Opus 5.5) to profile, write this change and its test, and run the old-vs-new checks and benchmarks above.