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 <khaleel.aladhami@gmail.com>
This commit is contained in:
Masen Furer 2024-09-26 21:52:31 -07:00 committed by GitHub
parent 299f842756
commit ae0f3f820e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,6 +21,7 @@ from typing import (
from reflex.constants.base import Dirs from reflex.constants.base import Dirs
from reflex.utils.exceptions import PrimitiveUnserializableToJSON, VarTypeError from reflex.utils.exceptions import PrimitiveUnserializableToJSON, VarTypeError
from reflex.utils.imports import ImportDict, ImportVar from reflex.utils.imports import ImportDict, ImportVar
from reflex.utils.types import is_optional
from .base import ( from .base import (
CustomVarOperationReturn, CustomVarOperationReturn,
@ -524,6 +525,8 @@ class NumberVar(Var[NUMBER_T]):
Returns: Returns:
The boolean value of the number. The boolean value of the number.
""" """
if is_optional(self._var_type):
return boolify((self != None) & (self != 0)) # noqa: E711
return self != 0 return self != 0
def _is_strict_float(self) -> bool: def _is_strict_float(self) -> bool: