Skip to content

tentative migrate pyright to ty - #6465

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

benedikt-bartscher wants to merge 72 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

⚠️ 11 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 (bf6e35a) 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.
Older sqlmodel defines SQLModelConfig twice behind IS_PYDANTIC_V2, so
calling it fails ty at the minimum versions although it works at runtime.
The lock keeps ty at 0.0.73. The branch lists ty in backend_pinned, so it
is bumped on its own cadence; upstream's dependency refresh moved it to
0.0.88, which reports several hundred new diagnostics, among them every
EventHandler[...] annotation now as invalid-type-form.

The pydantic Field overload is split into optional-first and required
overloads like upstream's other object overloads, so an optional pydantic
field reads as ObjectVar[Model]; tests/type_checking/fields.py covers it.
The restored set literal in figure_out_type and a new ForwardRef test
parameter get invalid-type-form ignores, two new pyright ignores that ty
flags become ty ignores, and ignores made unused by the optional object
field fix are dropped.
Comment thread reflex/utils/telemetry_accounting.py Resolved
…ainst

The published reflex-base 0.10.1a1 alpha, from a release branch not merged
here, lacks this PR's UnionType overloads of passthrough_event_spec, so the
min-deps check resolved to it.
_STORAGE_FEATURE is typed dict[type, FeatureName], so the lookup needs no
runtime narrowing.
A computed var whose deps hold both the current-state placeholder and its explicit full name, as add_dependency on its own state produces, kept only one of the two sets, so a dependency map rebuilt on reload stopped refreshing on the other.
The connection toaster builds ToastProps with only some fields, which
type-checks only against the sonner that gives the rest None defaults.
@FarhanAliRaza FarhanAliRaza added the enhancement Anything you want improved label Nov 12, 2026 — with ChatGPT Codex Connector
The lock keeps ty at 0.0.73, as in the previous merge, and drops pyright
again. Upstream's pyright hooks over tests/type_checking at Python 3.11 and
3.14 become a ty hook at 3.11 beside the existing 3.14 one.

Upstream's new pyright ignores are dropped, or become ty ignores where ty
flags the same line; the alembic env.py template keeps its pyright ignore,
since it is copied into user projects. The optional Field overloads for
sequences and sets are split like the list ones, so ty reads
rx.Field[set[str] | None] as a SetVar. ClientStateVar.retrieve, now in
reflex_base, takes EventType[Any] as the experimental one did, and its test
handler is decorated with @rx.event, as is the backend-error handler of the
migrated exception handler test. sqlalchemy.inspect passes raiseerr=True so
ty reads a mapper, redundant casts are dropped, and the new set operations,
the empty-array guard and figure_out_type's set and mapping forms get ignores
like their neighbours.

reflex-components-internal floors reflex-base on 0.11.1.dev0: the released
0.11.0 still lacks this PR's UnionType overloads of passthrough_event_spec.
@FarhanAliRaza

Copy link
Copy Markdown
Contributor

Hi thank you for this.
What remains in this to be able to make it mergeable. I would like to bring it in.

# Conflicts:
#	packages/reflex-base/src/reflex_base/vars/base.py
#	tests/units/test_state.py
…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.
item_var_datas = []
for responsive_item in responsive_list:
if isinstance(responsive_item, dict):
if not isinstance(responsive_item, Var) and isinstance(responsive_item, dict):

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 Styles add a needless check

convert_list() now checks every item against Var before checking dict. The supported Var classes are not dictionaries, so this adds work without changing which branch runs. The repository guide forbids runtime checks that work around type-only problems. Restore the dictionary-only check before merging to satisfy that requirement.

Suggested change
if not isinstance(responsive_item, Var) and isinstance(responsive_item, dict):
if isinstance(responsive_item, dict):

Context Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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

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