Repository navigation
fix: type Var.to(SomeVarSubclass) as that subclass under ty - #7942
benedikt-bartscher wants to merge 5 commits into
Conversation
[Low impact] The PR appears safe to merge; no actionable issues remain. SummaryFixes the inferred return type of
Reviews (6) · Last reviewed commit: "Type Var.to(SomeVarSubclass, var_type) u..." · Reviewed by Greptile |
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>
cea10fc to
c0e277c
Compare
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
There was a problem hiding this comment.
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
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>
The
Var.tooverload meant for aVarsubclass as the output type takes an instance of that subclass, not the class:var.to(StringVar)passes the class, so it never matches and falls through to the catch-all overload:pyright solves
OUTPUTfrom that overload, and so did ty up to 0.0.91. ty 0.0.92 and later inferUnknowninstead.rx.cond(cond, var.to(StringVar), other)then matches itsc1: Componentoverload, and the result is typed as aComponent. In our apps, that showed up as errors likeComponent has no attribute toandexpected Var[str], found Component.This PR:
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.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.assert_typeexamples totests/type_checking/vars.py._any_var.to(...)StringVarUnknownStringVar[str]StringVar[str]BooleanVarUnknownBooleanVarBooleanVarStringVar, 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-checkinghooks (3.11 and 3.14). Full-repo pyright reports the same diagnostics asmain, and infers the same types as before for every.to(...)call shape I checked.tests/units/test_var.pyandtests/units/reflex_base/varspass, as do all pre-commit hooks andreflex-release changelog-check.🤖 Generated with Claude Code