Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with float Typing and Operations #4686

Closed
sorousch9 opened this issue Jan 24, 2025 · 2 comments · Fixed by #4701
Closed

Problems with float Typing and Operations #4686

sorousch9 opened this issue Jan 24, 2025 · 2 comments · Fixed by #4701
Labels
enhancement Anything you want improved

Comments

@sorousch9
Copy link

Describe the bug
It seems like there are some serious issues with how Reflex handles float types. Here's what I've run into so far:

  1. reveal_type doesn’t correctly identify the type when using rx.Var.create or NumberVar.create with a float.
  2. String formatting, like using rx.text(f"{var:.2f}"), doesn’t round floats properly.
  3. Arithmetic operations, such as dividing a float variable by a number, don’t work as expected.
  4. Comparisons (>, <) fail outright, though equality (==) works fine.

To Reproduce:
Here’s a snippet that highlights the issues:

Here’s a snippet that highlights the issues:

import reflex as rx

# Type issues
reveal_type(rx.Var.create(0.0))  # broken
reveal_type(NumberVar.create(0.0))  # broken

# Operations
var = rx.Var.create(0.0)
rx.text(f"{var:.2f}")  # does not round

print(var / 2)  # broken

manual_float: rx.Var[float] = rx.Var.create(0.0)
print(manual_float / 2)  # broken

print(manual_float > manual_float)  # broken
print(manual_float < manual_float)  # broken
print(manual_float == manual_float)  # works fine

To Reproduce
Steps to reproduce the behavior:

  • Code/Link to Repo:

Expected behavior

  • reveal_type should return the correct type for float variables created using rx.Var or NumberVar.
  • String formatting like f"{var:.2f}" should round the float correctly.
  • Arithmetic operations, like division, should work without issue.
  • Comparisons (>, <) should also work as expected.

Specifics (please complete the following information):

  • Python Version: 3.13.1
  • Reflex Version: 0.6.8
  • OS: wsl2

Additional context
Add any other context about the problem here.

Copy link

linear bot commented Jan 24, 2025

@Lendemor Lendemor added the enhancement Anything you want improved label Jan 24, 2025
@adhami3310
Copy link
Member

reveal_type doesn’t correctly identify the type when using rx.Var.create or NumberVar.create with a float.

You are not supposed to use NumberVar.create, use LiteralNumberVar.create or Var.create. I made a PR to improve typing of Vars created through Var.create

String formatting, like using rx.text(f"{var:.2f}"), doesn’t round floats properly.

This is a duplicate of #2148

Arithmetic operations, such as dividing a float variable by a number, don’t work as expected.

They work, they are just not typed as such. With the PR this should improve as it would report NumberVar[float] instead of Var[float] when using Var.create. However, this is mostly a typing limitation of python itself. You should be able to call .guess_type() to move from Var[float] into NumberVar[float]

Comparisons (>, <) fail outright, though equality (==) works fine.

same as above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Anything you want improved
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants