Skip to content

DO NOT MERGE - Codspeed run only - Rel fix pep695 alias runtime perf isinstance - #6988

Closed
benedikt-bartscher wants to merge 10 commits into
reflex-dev:mainfrom
benedikt-bartscher:rel-fix-pep695-alias-runtime-perf-isinstance
Closed

DO NOT MERGE - Codspeed run only - Rel fix pep695 alias runtime perf isinstance#6988
benedikt-bartscher wants to merge 10 commits into
reflex-dev:mainfrom
benedikt-bartscher:rel-fix-pep695-alias-runtime-perf-isinstance

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

this is #6986 and #6862 merged together - just for a codspeed run.

Review in cubic

benedikt-bartscher and others added 10 commits August 9, 2026 18:19
PR reflex-dev#6944 added resolve_type_alias() but wired it only into
Var.guess_type, so alias-annotated state vars compiled while every
runtime path that re-checks the raw annotation still choked on the
TypeAliasType:

- State.__setattr__ validates assignments through _isinstance(), which
  fell through to the bare isinstance() call and raised "TypeError:
  isinstance() arg 2 must be a type" on every assignment to an
  alias-annotated var, aborting the event handler instead of at most
  logging a mismatch (0.9.9a1 pre-release FINDING-002).
- Passing an event handler with an alias-annotated argument uncalled to
  an event trigger crashed page compile: typehint_issubclass() reached
  the bare issubclass() for a plain alias and failed the origin
  comparison for a subscripted one, escalating to a fatal error
  (FINDING-006).

Resolve the alias at the entry of both functions, reusing
resolve_type_alias(), which already handles parameterized aliases
(Items[str]) and aliases nested in unions (Key | None). Alias-annotated
vars now behave exactly like their resolved annotations at runtime.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EMjBXPozsNeQNSBZecNH8x
…ntime' into rel-fix-pep695-alias-runtime-perf-isinstance
@codspeed-hq

codspeed-hq Bot commented Aug 28, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 27 untouched benchmarks
🆕 5 new benchmarks
⏩ 8 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
🆕 Simulation test_isinstance_container[list_dict] N/A 75.9 ms N/A
🆕 Simulation test_isinstance_container[list_int] N/A 76.8 ms N/A
🆕 Simulation test_isinstance_container[list_optional] N/A 328.5 ms N/A
🆕 Simulation test_isinstance_container[list_typeddict] N/A 75.6 ms N/A
🆕 Simulation test_isinstance_scalar N/A 74.5 ms N/A

Comparing benedikt-bartscher:rel-fix-pep695-alias-runtime-perf-isinstance (47d17af) with main (fba9cc5)

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.

@masenf

masenf commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

seems like a better version of #6986

@benedikt-bartscher benedikt-bartscher changed the title Rel fix pep695 alias runtime perf isinstance DO NOT MERGE - Codspeed run only - Rel fix pep695 alias runtime perf isinstance Aug 28, 2026
@greptile-apps

greptile-apps Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds runtime support for PEP 695 aliases in type validation while reducing repeated work on validation and serialization hot paths.

  • Resolves bare, parameterized, and union-nested aliases in _isinstance and typehint_issubclass.
  • Caches typing argument lookup and deferred imports used during repeated validation.
  • Caches the deferred serializer function lookup used by json_dumps.
  • Adds alias regression tests and _isinstance performance benchmarks.

Confidence Score: 5/5

The PR appears safe to merge with no concrete blocking or independently actionable non-blocking issue identified.

Alias resolution is applied before runtime type checks, the optimized origin checks preserve the prior helper semantics, and the deferred lookup caches retain the existing callable behavior.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/utils/types.py Adds alias resolution and memoized helper/import lookups without an identified correctness regression.
packages/reflex-base/src/reflex_base/utils/format.py Caches the existing deferred serializer function while preserving caller-provided JSON defaults.
tests/units/reflex_base/utils/test_types.py Covers bare, literal, generic, union-nested, native, and backported alias behavior.
tests/units/test_state.py Verifies alias-annotated state assignments no longer crash or emit mismatch logs.
tests/benchmarks/test_isinstance.py Adds representative scalar and nested-container benchmarks for the optimized validation paths.

Reviews (1): Last reviewed commit: "Merge remote-tracking branch 'upstream/c..." | Re-trigger Greptile

@benedikt-bartscher

Copy link
Copy Markdown
Contributor Author

This is just 2 PRs (#6986 and #6862) merged together, do you want to merge them separately or combined with this one?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants