From ae0f3f820ed44a791be9325db2986a6016e6d74b Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 26 Sep 2024 21:52:31 -0700 Subject: [PATCH] Handle bool cast for optional NumberVar (#4010) * Handle bool cast for optional NumberVar If the _var_type is optional, then also check that the value is not None * boolify the result of `and_operation` * flip order to be more semantically pure --------- Co-authored-by: Khaleel Al-Adhami --- reflex/vars/number.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/reflex/vars/number.py b/reflex/vars/number.py index 9a899f220..0aaa7a068 100644 --- a/reflex/vars/number.py +++ b/reflex/vars/number.py @@ -21,6 +21,7 @@ from typing import ( 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 @@ class NumberVar(Var[NUMBER_T]): Returns: The boolean value of the number. """ + if is_optional(self._var_type): + return boolify((self != None) & (self != 0)) # noqa: E711 return self != 0 def _is_strict_float(self) -> bool: