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

BE
3h ago
+33-23

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 beforety ≄ 0.0.92 afterpyright
StringVarUnknownStringVar[str]StringVar[str]
BooleanVarUnknownBooleanVarBooleanVar
StringVar, strUnknownStringVar[str]StringVar[str]
NumberVar, intUnknownNumberVar[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