Skip to content

Commit

Permalink
add type conversion for int,float in built-in setters (reflex-dev#1660)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lendemor authored Aug 23, 2023
1 parent 6c80a0f commit 57855f5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions reflex/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from reflex import constants
from reflex.base import Base
from reflex.utils import format, types
from reflex.utils import console, format, types

if TYPE_CHECKING:
from reflex.state import State
Expand Down Expand Up @@ -831,7 +831,16 @@ def setter(state: State, value: Any):
state: The state within which we add the setter function.
value: The value to set.
"""
setattr(state, self.name, value)
if self.type_ in [int, float]:
try:
value = self.type_(value)
setattr(state, self.name, value)
except ValueError:
console.warn(
f"{self.name}: Failed conversion of {value} to '{self.type_.__name__}'. Value not set.",
)
else:
setattr(state, self.name, value)

setter.__qualname__ = self.get_setter_name()

Expand Down

0 comments on commit 57855f5

Please sign in to comment.