Skip to content

Commit cb24492

Browse files
authored
fix boolean to boolen comparisons (#4620)
* fix boolean to boolen comparisons * fixes #4618 * fix tests
1 parent b50b769 commit cb24492

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

reflex/vars/number.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from reflex.constants.base import Dirs
2121
from reflex.utils.exceptions import PrimitiveUnserializableToJSON, VarTypeError
2222
from reflex.utils.imports import ImportDict, ImportVar
23-
from reflex.utils.types import is_optional
2423

2524
from .base import (
2625
CustomVarOperationReturn,
@@ -431,7 +430,7 @@ def __lt__(self, other: Any):
431430
"""
432431
if not isinstance(other, NUMBER_TYPES):
433432
raise_unsupported_operand_types("<", (type(self), type(other)))
434-
return less_than_operation(self, +other)
433+
return less_than_operation(+self, +other)
435434

436435
@overload
437436
def __le__(self, other: number_types) -> BooleanVar: ...
@@ -450,7 +449,7 @@ def __le__(self, other: Any):
450449
"""
451450
if not isinstance(other, NUMBER_TYPES):
452451
raise_unsupported_operand_types("<=", (type(self), type(other)))
453-
return less_than_or_equal_operation(self, +other)
452+
return less_than_or_equal_operation(+self, +other)
454453

455454
def __eq__(self, other: Any):
456455
"""Equal comparison.
@@ -462,7 +461,7 @@ def __eq__(self, other: Any):
462461
The result of the comparison.
463462
"""
464463
if isinstance(other, NUMBER_TYPES):
465-
return equal_operation(self, +other)
464+
return equal_operation(+self, +other)
466465
return equal_operation(self, other)
467466

468467
def __ne__(self, other: Any):
@@ -475,7 +474,7 @@ def __ne__(self, other: Any):
475474
The result of the comparison.
476475
"""
477476
if isinstance(other, NUMBER_TYPES):
478-
return not_equal_operation(self, +other)
477+
return not_equal_operation(+self, +other)
479478
return not_equal_operation(self, other)
480479

481480
@overload
@@ -495,7 +494,7 @@ def __gt__(self, other: Any):
495494
"""
496495
if not isinstance(other, NUMBER_TYPES):
497496
raise_unsupported_operand_types(">", (type(self), type(other)))
498-
return greater_than_operation(self, +other)
497+
return greater_than_operation(+self, +other)
499498

500499
@overload
501500
def __ge__(self, other: number_types) -> BooleanVar: ...
@@ -514,17 +513,7 @@ def __ge__(self, other: Any):
514513
"""
515514
if not isinstance(other, NUMBER_TYPES):
516515
raise_unsupported_operand_types(">=", (type(self), type(other)))
517-
return greater_than_or_equal_operation(self, +other)
518-
519-
def bool(self):
520-
"""Boolean conversion.
521-
522-
Returns:
523-
The boolean value of the number.
524-
"""
525-
if is_optional(self._var_type):
526-
return boolify((self != None) & (self != 0)) # noqa: E711
527-
return self != 0
516+
return greater_than_or_equal_operation(+self, +other)
528517

529518
def _is_strict_float(self) -> bool:
530519
"""Check if the number is a float.

tests/units/test_var.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ def test_all_number_operations():
10041004

10051005
assert (
10061006
str(even_more_complicated_number)
1007-
== "!(((Math.abs(Math.floor(((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2))) || (2 && Math.round(((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2)))) !== 0))"
1007+
== "!(isTrue((Math.abs(Math.floor(((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2))) || (2 && Math.round(((Math.floor(((-((-5.4 + 1)) * 2) / 3) / 2) % 3) ** 2))))))"
10081008
)
10091009

10101010
assert str(LiteralNumberVar.create(5) > False) == "(5 > 0)"

0 commit comments

Comments
 (0)