Skip to content

fix: type Var.to(SomeVarSubclass) as that subclass under ty - #7942

Open
benedikt-bartscher wants to merge 5 commits into
reflex-dev:mainfrom
benedikt-bartscher:fix/var-to-var-subclass-overload
Open

benedikt-bartscher wants to merge 5 commits into
reflex-dev:mainfrom
benedikt-bartscher:fix/var-to-var-subclass-overload

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

The Var.to overload meant for a Var subclass as the output type takes an instance of that subclass, not the class:

@overload
def to(self, output: VAR_SUBCLASS, var_type: None = None) -> VAR_SUBCLASS: ...

var.to(StringVar) passes the class, so it never matches and falls through to the catch-all overload:

@overload
def to(self, output: type[OUTPUT] | types.GenericType, var_type: types.GenericType | None = None) -> OUTPUT: ...

pyright solves OUTPUT from that overload, and so did ty up to 0.0.91. ty 0.0.92 and later infer Unknown instead. rx.cond(cond, var.to(StringVar), other) then matches its c1: Component overload, and the result is typed as a Component. In our apps, that showed up as errors like Component has no attribute to and expected Var[str], found Component.

This PR:

  • Makes the overload take type[VAR_SUBCLASS] and accept a var_type, so .to(StringVar, str) and .to(FunctionVar, EventChain) resolve too. pyright then reports the catch-all overload as unreachable (reportOverlappingOverload). It is not: pyright still resolves calls like .to(SomeDataclass) through it. So the catch-all gets the same ignore as the first to overload and the Var.create overloads.
  • Locks ty 0.0.104, the newest release the 7-day exclude-newer window allows. The previously locked ty 0.0.88 inferred the right types with or without the fix, so the new examples gated nothing in CI. Only the ty entry of uv.lock changes.
  • Adds assert_type examples to tests/type_checking/vars.py.
_any_var.to(...) ty ≥ 0.0.92 before ty ≥ 0.0.92 after pyright
StringVar Unknown StringVar[str] StringVar[str]
BooleanVar Unknown BooleanVar BooleanVar
StringVar, str Unknown StringVar[str] StringVar[str]
NumberVar, int Unknown NumberVar[float | Decimal] NumberVar[int | float | Decimal] (unchanged)

With the locked ty 0.0.104, the three new assertions fail without the change and pass with it. They also pass under ty 0.0.107 and both pyright-type-checking hooks (3.11 and 3.14). Full-repo pyright reports the same diagnostics as main, and infers the same types as before for every .to(...) call shape I checked. tests/units/test_var.py and tests/units/reflex_base/vars pass, as do all pre-commit hooks and reflex-release changelog-check.

🤖 Generated with Claude Code

@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; no actionable issues remain.

Summary

Fixes the inferred return type of Var.to when callers pass a Var subclass.

  • Passing a Var subclass to Var.to keeps its specific return type.

Reviews (6) · Last reviewed commit: "Type Var.to(SomeVarSubclass, var_type) u..." · Reviewed by Greptile

benedikt-bartscher and others added 2 commits November 19, 2026 21:42
The overload meant for `var.to(StringVar)` took an instance of the Var
subclass (`output: VAR_SUBCLASS`) rather than the class, so a class
argument fell through to the catch-all `type[OUTPUT] | GenericType`
overload. pyright and ty 0.0.88 solve OUTPUT from it, ty 0.0.107 infers
`Unknown`, and rx.cond then picks its Component overload for such vars.
Taking `type[VAR_SUBCLASS]` resolves it for both checkers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@benedikt-bartscher
benedikt-bartscher force-pushed the fix/var-to-var-subclass-overload branch from cea10fc to c0e277c Compare November 19, 2026 20:42
@codspeed

codspeed Bot commented Nov 19, 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:fix/var-to-var-subclass-overload (77a950e) 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. ↩

@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review November 19, 2026 21:00
@benedikt-bartscher
benedikt-bartscher requested a review from a team as a code owner November 19, 2026 21:00

@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 3 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-base/src/reflex_base/vars/base.py">

<violation number="1" location="packages/reflex-base/src/reflex_base/vars/base.py:1152">
P2: This overload promises the exact type for every `Var` subclass, but `Var.to` only converts registered subclasses and returns the original `Var` for unregistered ones. Restrict this overload to supported conversion targets or update the runtime path so the advertised subclass is actually returned; otherwise downstream code can use subclass members on a value that is still the original `Var`.</violation>
</file>

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

View guided diff | Re-trigger cubic

benedikt-bartscher and others added 3 commits November 20, 2026 09:48
The new assert_type examples did not fail in CI without the fix: the
locked ty 0.0.88 and pyright solve the catch-all overload either way.
ty 0.0.92 is the first version that infers `Unknown` there. Lock ty
0.0.104, the newest release the 7-day exclude-newer window allows, so the
ty hook gates the examples. Only the ty entry of uv.lock changes.

`.to(StringVar, str)` and the other calls that pass a `var_type` still
went through the catch-all overload and were `Unknown` under ty 0.0.92
and later. The subclass overload now accepts a `var_type`. pyright then
reports the catch-all overload as unreachable, which it is not: it still
resolves calls such as `.to(SomeDataclass)`. The catch-all overload gets
the same reportOverlappingOverload ignore as the first `to` overload and
the `Var.create` overloads. pyright infers the same types as before for
every call shape checked.

The news fragment names the ty versions affected and leaves the
background to the PR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant