From 885a51648c143a5846da9119104f062902a26a52 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 26 Sep 2024 13:25:43 -0700 Subject: [PATCH 1/3] Handle bool cast for optional NumberVar If the _var_type is optional, then also check that the value is not None --- reflex/vars/number.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/reflex/vars/number.py b/reflex/vars/number.py index 9a899f220d..2798a6913d 100644 --- a/reflex/vars/number.py +++ b/reflex/vars/number.py @@ -21,6 +21,7 @@ from reflex.constants.base import Dirs from reflex.utils.exceptions import PrimitiveUnserializableToJSON, VarTypeError from reflex.utils.imports import ImportDict, ImportVar +from reflex.utils.types import is_optional from .base import ( CustomVarOperationReturn, @@ -524,6 +525,8 @@ def bool(self): Returns: The boolean value of the number. """ + if is_optional(self._var_type): + return (self != 0) & (self != None) # noqa: E711 return self != 0 def _is_strict_float(self) -> bool: From 3c3ff978fe361f15d13ceb1899ac9c1970d7b437 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 26 Sep 2024 13:30:29 -0700 Subject: [PATCH 2/3] boolify the result of `and_operation` --- reflex/vars/number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflex/vars/number.py b/reflex/vars/number.py index 2798a6913d..6eca64b5e7 100644 --- a/reflex/vars/number.py +++ b/reflex/vars/number.py @@ -526,7 +526,7 @@ def bool(self): The boolean value of the number. """ if is_optional(self._var_type): - return (self != 0) & (self != None) # noqa: E711 + return boolify((self != 0) & (self != None)) # noqa: E711 return self != 0 def _is_strict_float(self) -> bool: From 35abb1640f70be620c6652d344f712273c475768 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Thu, 26 Sep 2024 16:04:34 -0700 Subject: [PATCH 3/3] flip order to be more semantically pure --- reflex/vars/number.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflex/vars/number.py b/reflex/vars/number.py index 6eca64b5e7..0aaa7a0685 100644 --- a/reflex/vars/number.py +++ b/reflex/vars/number.py @@ -526,7 +526,7 @@ def bool(self): The boolean value of the number. """ if is_optional(self._var_type): - return boolify((self != 0) & (self != None)) # noqa: E711 + return boolify((self != None) & (self != 0)) # noqa: E711 return self != 0 def _is_strict_float(self) -> bool: