Selaa lähdekoodia

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>
Masen Furer 7 kuukautta sitten
vanhempi
säilyke
ae0f3f820e
1 muutettua tiedostoa jossa 3 lisäystä ja 0 poistoa
  1. 3 0
      reflex/vars/number.py

+ 3 - 0
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: