Skip to content

tentative migrate pyright to ty - #6465

Open
benedikt-bartscher wants to merge 79 commits into
reflex-dev:mainfrom
benedikt-bartscher:migrate-pyright-to-ty
Open

benedikt-bartscher wants to merge 79 commits into
reflex-dev:mainfrom
benedikt-bartscher:migrate-pyright-to-ty

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented May 6, 2026 •

Copy link
Copy Markdown
Contributor
time ty check
...
ty check  6.93s user 0.22s system 1415% cpu 0.505 total

time pyright
...
pyright  23.59s user 0.74s system 168% cpu 14.470 total

@codspeed

codspeed Bot commented May 6, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 16 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

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing benedikt-bartscher:migrate-pyright-to-ty (b97e890) with main (df7f157)

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. ↩

Resolve conflicts from upstream's memo refactor (reflex-dev#6517, promote rx._x.memo
to rx.memo): memo implementation moved to reflex_base.components.memo and
CustomComponent was removed. Re-apply the ty migration changes onto the new
locations:

- reflex/experimental/memo.py: take upstream's deprecated alias
- reflex_base/components/memo.py: type fn params as FunctionType and add
  ty:ignore for dynamic __signature__ assignment
- tests: drop CustomComponent tests removed upstream; carry ty:ignore
  comments onto the relocated memo/telemetry tests
- pyproject.toml / uv.lock: keep both towncrier (upstream) and ty (HEAD)
- pyi_hashes.json: regenerated

ty check, ruff, and the unit suite pass.
…ents

- Cast redis srem the way the other redis calls in the file are cast, so the
  lock cleanup type-checks against the declared redis floor as well.
- Type the alembic configure kwargs as Any; ty ignores the pyright comment.
- Floor reflex-components-dataeditor on the reflex-base with the union
  passthrough_event_spec overloads.
- Return FIELD_TYPE from the component field() again, so a default of the
  wrong type for a prop is a type error.
- Declare ToOperation's fields and constructor for type checkers only instead
  of making the mixin a runtime dataclass.
- Copy a mixin's computed var with a cast instead of skipping a non-function
  fget, and drop the unreachable branch in Match._validate_return_types.
- Add news fragments for every package the migration touches.
Comment thread reflex/compiler/compiler.py Resolved
The calls in test_model.py that must fail type checking were held to that by
pyright's unnecessary-ignore report, which no longer ran. ty catches only some
of them: it does not compare an attribute retyped in a subclass, binds an
unannotated self in a step to Unknown, and solves wait_for's contravariant
workflow to an intersection.

Report unused ty ignores in that file, put the pyright ignores back beside the
ty ones, and run pyright over the file in pre-commit for the calls only it
catches.
ty keeps the calls it catches failing through the unused-ignore rule on this
file. The ones it misses (a column retyped in a subclass, astral-sh/ty#2158; a
step or wait of another workflow) are rejected by the engine at runtime and
covered by the tests of resolve and of the engine's columns, so no second type
checker runs in CI. Their pyright ignores stay for anyone who runs pyright.
ty infers no return type for an unannotated function, so the step returning a
number was a check of pyright's inference only. Annotated, it checks the type of
step itself, and both checkers reject it.

Say why the remaining pyright-only calls are: a step or wait mixing two workflows
is well-typed under the contravariant workflow parameter, and the redeclared
columns need a ty with attribute override checks.
- Rename the rules ty renamed, in the config and in ignore comments, and drop
  the ignores ty no longer needs.
- Spell the event triggers of the bundled components Annotated[EventHandler,
  spec], the object EventHandler[spec] already evaluates to: ty rejects
  subscripting a class that is not generic in a type expression
  (astral-sh/ty#3388). Tests keep the public EventHandler[spec] spelling, with
  an ignore, as do the pyi generator's fixtures. This lets the repo drop its
  global not-subscriptable and unsupported-base ignores.
- Put HybridProperty.var's callable overload first: ty reads a @classmethod in a
  class body as a function, and solved the classmethod overload to Unknown. The
  staticmethod overload is covered by the callable one.
- Annotate generator-based context managers as Generator/AsyncGenerator, which
  typeshed now asks for.
- Fix the overrides ty now checks where the fix is local: plotly tags assigned
  like every other component's, Element.__eq__ named like its base, the model
  config built as SQLModelConfig. Ignore the rest precisely.
- ty now checks attribute overrides, so the workflow model's redeclared engine
  columns are ty ignores too, kept failing by the unused-ignore rule.
- ty fails on warnings now; drop a hosting CLI guard that was always true.
Comment thread pyproject.toml Resolved
test_check_min_deps still asserted the pre-rename rule name. SQLModelConfig is
defined per pydantic version at the declared sqlmodel minimum, so calling it does
not type-check there; keep the plain dict with an ignore, as before.
@FarhanAliRaza

Copy link
Copy Markdown
Contributor

What's still open after my pushes:

  • The docs show EventHandler[spec] for custom component triggers on 16 pages. ty rejects that form on purpose (SomeClass[x] on class with __class_getitem__() impl gives @todo astral-sh/ty#3388), so anyone who copies it into a ty-checked project gets invalid-type-form. The components in this repo now use Annotated[EventHandler, spec]. We either update the docs or make the subscript form something ty accepts.
  • 3 of the 7 must-fail calls in tests/units/reflex_workflow/test_model.py are caught by pyright only: the ones that mix two workflows. With W contravariant, ty is right to accept them, and the shared-base usage in the same file needs W that way. The runtime resolve tests still reject them. The other 4 fail under ty, and unused-ignore-comment = "error" on that file keeps them failing.
  • tests/type_checking no longer gets a pyright run (it ran on 3.11 and 3.14 before). Some test files also have pyright errors now, where pyright ignores were swapped for ty ones (about 20 in test_hybrid_property.py). This only matters if we still care about pyright on tests.
  • The ToastProps = ... defaults come from this PR's stub generator change, but the hatch hook builds stubs with whatever reflex_base is in the build env. Until a reflex-base with this change is released, built sonner stubs can still mark every ToastProps field as required. Locally, a wheel build (check_min_deps, uv sync) rewrites toast.pyi and ty then fails. scripts/make_pyi.py fixes it.
  • ty 0.0.107 exits 1 on warnings, so a new warning after any ty bump fails pre-commit.
  • The message of eeec9cc says get_var_value failed on main for a cast literal. It doesn't. Fixing that needs a history rewrite.
  • Two ty bugs I hit and haven't filed yet: conflicting constraints from a callable argument silently solve to Unknown instead of erroring, and @classmethod in a class body is read as a plain function. The second one is worked around in HybridProperty.var by putting the Callable overload first.

Style keeps keys whose values are dicts, so rx.Style({rx.el.div: {...}})
as App.style carries per-component styles that the early return skipped.
_request is called with unions such as `ManagedDatabase | _NoDatabase`,
which are type forms but not instances of `type`, so `cast: type[T]`
matched no overload and every such call needed a ty:ignore. Annotating
it as `TypeForm[T]` (PEP 747, imported only for type checking since the
SDK has no runtime dependencies) lets ty infer the decoded union and
drops all 16 ignores.
CachedVarOperation is a plain class, so its cached_property attribute is
writable as far as a type checker can tell, while every class mixing it
in is a frozen dataclass whose override is read-only. ty rightly reported
each override as a read-only attribute overriding a writable one. A
type-checking-only property declaration states the attribute's real,
read-only type, keeps the runtime body checked and drops all 10 ignores.
Read the handler function from event_handlers and install the partial
with monkeypatch.setattr instead of assigning over the class attribute
under an ignore, so this test matches the port in reflex-dev#6722 and the change
is restored after the test.
The ty hook already checks tests/type_checking under Python 3.11: the
project's ty check includes the directory and takes its Python version
from the requires-python floor, not the running interpreter, and it runs
on every commit that changes a Python file. Keep only the 3.14 re-check
and describe the remaining hooks in the type_checking README.
A commit that changes no .py file, like a dependency bump, skipped the
full type check, and with it the Python 3.11 check of tests/type_checking.
ty narrows str | dict | Var to dict on the dict check alone now, so the
extra isinstance call per responsive item only cost time.
The handlers must accept str | list[str], and ty keeps list[str] after
the membership check (astral-sh/ty#5082), so the assignment is ignored
instead of narrowed by an extra isinstance call.
The wrapper called callable_name on every var operation it built.
@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review November 22, 2026 13:33
@benedikt-bartscher
benedikt-bartscher requested review from a team and Alek99 as code owners November 22, 2026 13:33

@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.

1 issue found across 404 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/reflex-components-internal/pyproject.toml">

<violation number="1" location="packages/reflex-components-internal/pyproject.toml:20">
P2: This dev floor cannot pass the internal package's release path: `reflex-components-internal` is auto-published, but its publish workflow does not materialize dependency pins and it has no lockstep sibling to rewrite this requirement. Re-pin to a published `reflex-base` version before publishing, or add equivalent release-time rewriting for this package.</violation>
</file>

Note: This PR contains a large number of files. cubic selects up to 200 of the highest-priority eligible files for this review, so some files may not have been reviewed.

View guided diff | Re-trigger cubic

]
dependencies = [
"reflex >= 0.11.0",
"reflex-base >= 0.11.1.dev0",

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.

P2: This dev floor cannot pass the internal package's release path: reflex-components-internal is auto-published, but its publish workflow does not materialize dependency pins and it has no lockstep sibling to rewrite this requirement. Re-pin to a published reflex-base version before publishing, or add equivalent release-time rewriting for this package.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/reflex-components-internal/pyproject.toml, line 20:

<comment>This dev floor cannot pass the internal package's release path: `reflex-components-internal` is auto-published, but its publish workflow does not materialize dependency pins and it has no lockstep sibling to rewrite this requirement. Re-pin to a published `reflex-base` version before publishing, or add equivalent release-time rewriting for this package.</comment>

<file context>
@@ -17,6 +17,7 @@ classifiers = [
 ]
 dependencies = [
     "reflex >= 0.11.0",
+    "reflex-base >= 0.11.1.dev0",
 ]
 
</file context>

Comment thread reflex/app.py Resolved
Comment thread pyproject.toml Resolved
A page callable without a __name__ (a functools.partial, an instance with
__call__) took its repr as the route name, and failed route validation
with that repr in the message. It now takes the no-route path and raises
RouteValueError asking for an explicit route.

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

enhancement Anything you want improved perf Performance-improving changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants