Repository navigation
Conversation
is_typed_dict entered a contextlib.suppress block for each of typing and typing_extensions on every call. JSON schema building for TypedDicts calls it many times, so the context managers cost about 15% of the inherited PEP 728 TypedDict schema benchmark. Read both _TypedDictMeta attributes with getattr and a None default and check type(typ) against them in one membership test. The result is the same, also when a module has no _TypedDictMeta. Co-Authored-By: Aiden <aiden@weco.ai>
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.
TL;DR
is_typed_dictinmashumaro/core/meta/helpers.py, which JSON schema building for TypedDicts calls very often.contextlib.suppressblocks withgetattr(..., None)and one membership test ontype(typ).Refs #384
What & Why
Before, each call entered a
suppress(AttributeError)context manager once per module. It also calledtype(typ)again in each pass of the loop. The context manager costs more than the type check itself. Schema building calls this function many times per TypedDict.Now the function reads
_TypedDictMetafromtyping_extensionsandtypingwithgetattrand aNonedefault. Then it checkstype(typ)against both values in one tuple. A missing attribute givesNone, and no type isNone, so the result staysFalse. The function still returns a realbool.Changes
is_typed_dict: usegetattrdefaults and oneincheck instead of a loop withsuppress.Results
Python 3.12.13, typing_extensions 4.16.0, Linux x86_64, best of 7 runs on an isolated core:
is_typed_dicton a typing_extensions TypedDictis_typed_dicton atyping.TypedDictis_typed_dictonint(not a TypedDict)build_json_schemaon an inherited PEP 728 TypedDicttest_jsonschema_for_inherited_pep_728_typed_dictfrom_dicton a plain dataclass (does not call it)Why this is safe and easy to maintain
The change touches one function and makes it shorter. It keeps the same result for every input, also when a module has no
_TypedDictMeta. The JSON schema, data type, dialect and meta tests cover it, and all of them pass.Validation
pytest tests-> 32815 passed, 7 skipped.pytest tests/test_jsonschema/test_jsonschema_generation.py tests/test_data_types.py tests/test_dialect.py tests/test_meta.py-> 32212 passed, 3 skipped.python -m timeit -s "from typing_extensions import TypedDict; from mashumaro.core.meta.helpers import is_typed_dict as f; T = TypedDict('T', {'x': int})" "f(T)"AI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/Ymrt0jrzsPyTgP7VzYqS-B5U_xXZLzb4