Skip to content

fix(types): check specialized generic TypedDict vars against their fields - #8022

Open
vjymisal0 wants to merge 4 commits into
reflex-dev:mainfrom
vjymisal0:fix/generic-typeddict-isinstance
Open

vjymisal0 wants to merge 4 commits into
reflex-dev:mainfrom
vjymisal0:fix/generic-typeddict-isinstance

Conversation

@vjymisal0

@vjymisal0 vjymisal0 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Summary

Assigning to a state var typed with a specialized generic TypedDict (p: P[str]) went through the Mapping branch of _isinstance. That branch reads the generic's own type arguments as key and value types:

  • P[str]: args[1] raised IndexError and crashed the event handler.
  • P[int]: keys were checked against int, so every valid assignment logged a false Expected field ... to receive type error.

The issue doesn't mention one more case. With nested=0, the depth _type_check_depth() uses in production, isinstance(obj, P) raised TypeError: TypedDict does not support instance and class checks for every assignment.

Changes in reflex_base/utils/types.py:

  • In the Mapping branch, a TypedDict origin is checked with does_obj_satisfy_typed_dict, the same way a plain TypedDict is checked at each depth.
  • does_obj_satisfy_typed_dict resolves a specialization's fields with get_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.
  • In the final except TypeError fallback, a TypedDict origin is checked as a plain dict, which fixes the production depth.

Both new checks sit on paths that only Mapping types or the error fallback reach, so list/tuple/set checks cost nothing extra. typing_extensions.is_typeddict is used so typing_extensions.TypedDict is covered too.

Fixes #7995

Verification

  • Regression tests fail on main (9 failed) and pass with the fix (15 passed):
    • 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 at nested=2; outer-type-only checks at nested=0.
    • tests/units/test_state.py::test_assign_specialized_generic_typed_dict_var: handler-style assignments to P[str] and P[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 in tests/units/assets/test_assets.py, test_gradient_profile.py::test_create_resolves_shared_asset and test_utils.py::test_initialize_agents_md_handles_symlinked_claude.
  • uv run ruff check . and uv run ruff format --check . pass. uv run pyright reflex tests packages/reflex-base/src reports the same errors as main, with no new ones.
  • News fragment: packages/reflex-base/news/+7995.bugfix.md.

View guided diff

…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>
@vjymisal0
vjymisal0 requested a review from a team as a code owner November 26, 2026 07:23
@FarhanAliRaza FarhanAliRaza added the bug Something isn't working label Nov 26, 2026 — with ChatGPT Codex Connector
@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no actionable new issues were found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable new issues were found.

Summary

Fixes runtime checks for specialized generic TypedDicts such as P[str].

  • Specialized generic TypedDict values are checked against their fields.

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

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 4 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

@codspeed

codspeed Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 4.99%

2 warnings

⚠️ 10 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
✅ 145 untouched benchmarks
⏩ 8 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_state_access[get_handler] 73.6 ms 70.1 ms +4.99%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing vjymisal0:fix/generic-typeddict-isinstance (9e628d8) with main (7ee72a1)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@vjymisal0

Copy link
Copy Markdown
Contributor Author

The Windows Python 3.15 failure is in test_garbage_collection_is_not_reported. The same assertion failed on the exact base, 6f5c39ca.

I also profiled the four flagged CodSpeed callbacks locally on 6f5c39ca and dad2dd70. The three route sizes and the 10,000 scalar writes made zero calls into the changed TypedDict validation path on either commit. A TypedDict positive control recorded the expected calls on both. That rules out direct calls from those callbacks in this local run, but leaves the hosted timing difference open. Could a maintainer rerun the Windows job and CodSpeed check when convenient? Thank you.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assigning to a generic TypedDict state var crashes the handler (IndexError) or logs a false type error

2 participants