fix: type Var.to(SomeVarSubclass) as that subclass under ty
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 avar_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 firsttooverload and theVar.createoverloads. - Locks ty 0.0.104, the newest release the 7-day
exclude-newerwindow 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 ofuv.lockchanges. - Adds
assert_typeexamples totests/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