Repository navigation
Conversation
…elds _isinstance sent a specialized generic TypedDict (P[str]) down the Mapping branch, which reads the generic's type arguments as key and value types: P[str] raised IndexError and P[int] rejected valid values. Without nesting, as in production, isinstance raised TypeError. Check such types with does_obj_satisfy_typed_dict using the substituted field types, and fall back to a dict check when nesting is off. Signed-off-by: vjymisal0 <misalvijay153@gmail.com>
[Medium impact] The PR appears safe to merge; no actionable new issues were found. SummaryFixes runtime checks for specialized generic TypedDicts such as
The previous-review range was not used because its merge base differs from the current PR’s merge base. The supplied full PR changes were reviewed instead. Reviews (5) · Last reviewed commit: "Merge main into fix/generic-typeddict-is..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 4 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
Merging this PR will improve performance by 4.99%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
|
The Windows Python 3.15 failure is in I also profiled the four flagged CodSpeed callbacks locally on |
All Submissions:
Type of change
Summary
Assigning to a state var typed with a specialized generic TypedDict (
p: P[str]) went through theMappingbranch of_isinstance. That branch reads the generic's own type arguments as key and value types:P[str]:args[1]raisedIndexErrorand crashed the event handler.P[int]: keys were checked againstint, so every valid assignment logged a falseExpected field ... to receive typeerror.The issue doesn't mention one more case. With
nested=0, the depth_type_check_depth()uses in production,isinstance(obj, P)raisedTypeError: TypedDict does not support instance and class checksfor every assignment.Changes in
reflex_base/utils/types.py:Mappingbranch, a TypedDict origin is checked withdoes_obj_satisfy_typed_dict, the same way a plain TypedDict is checked at each depth.does_obj_satisfy_typed_dictresolves a specialization's fields withget_typed_dict_field_types, which substitutes the type arguments (from fix(vars): resolve fields of parametrized generic TypedDict vars #7887). It reads__required_keys__/__closed__/__extra_items__from the origin class, because the alias doesn't forward them.except TypeErrorfallback, a TypedDict origin is checked as a plaindict, which fixes the production depth.Both new checks sit on paths that only Mapping types or the error fallback reach, so
list/tuple/setchecks cost nothing extra.typing_extensions.is_typeddictis used sotyping_extensions.TypedDictis covered too.Fixes #7995
Verification
tests/units/reflex_base/utils/test_types.py:P[...],P[...] | None, a generic subclass (_GenericMiddle[str]), a missing required key and a non-dict value atnested=2; outer-type-only checks atnested=0.tests/units/test_state.py::test_assign_specialized_generic_typed_dict_var: handler-style assignments toP[str]andP[int]vars store the value and log nothing. A non-dict assignment logs the exact expected error.uv run pytest tests/units: 15792 passed, 17 failed. All 17 also fail on unmodified main on my Windows machine. They are symlink tests intests/units/assets/test_assets.py,test_gradient_profile.py::test_create_resolves_shared_assetandtest_utils.py::test_initialize_agents_md_handles_symlinked_claude.uv run ruff check .anduv run ruff format --check .pass.uv run pyright reflex tests packages/reflex-base/srcreports the same errors as main, with no new ones.packages/reflex-base/news/+7995.bugfix.md.