Skip to content

Commit 95f6652

Browse files
Add Boolean support to Math Expression Node (Comfy-Org#13224)
* Add Boolean support to math expressions * Change boolean result test to assert values --------- Co-authored-by: Alexis Rolland <alexisrolland@hotmail.com>
1 parent 20f5e47 commit 95f6652

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

comfy_extras/nodes_math.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class MathExpressionNode(io.ComfyNode):
6363
@classmethod
6464
def define_schema(cls) -> io.Schema:
6565
autogrow = io.Autogrow.TemplateNames(
66-
input=io.MultiType.Input("value", [io.Float, io.Int]),
66+
input=io.MultiType.Input("value", [io.Float, io.Int, io.Boolean]),
6767
names=list(string.ascii_lowercase),
6868
min=1,
6969
)
@@ -82,6 +82,7 @@ def define_schema(cls) -> io.Schema:
8282
outputs=[
8383
io.Float.Output(display_name="FLOAT"),
8484
io.Int.Output(display_name="INT"),
85+
io.Boolean.Output(display_name="BOOL"),
8586
],
8687
)
8788

@@ -97,7 +98,7 @@ def execute(
9798

9899
result = simple_eval(expression, names=context, functions=MATH_FUNCTIONS)
99100
# bool check must come first because bool is a subclass of int in Python
100-
if isinstance(result, bool) or not isinstance(result, (int, float)):
101+
if not isinstance(result, (int, float)):
101102
raise ValueError(
102103
f"Math Expression '{expression}' must evaluate to a numeric result, "
103104
f"got {type(result).__name__}: {result!r}"
@@ -106,7 +107,7 @@ def execute(
106107
raise ValueError(
107108
f"Math Expression '{expression}' produced a non-finite result: {result}"
108109
)
109-
return io.NodeOutput(float(result), int(result))
110+
return io.NodeOutput(float(result), int(result), bool(result))
110111

111112

112113
class MathExtension(ComfyExtension):

tests-unit/comfy_extras_test/nodes_math_test.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,11 @@ def test_undefined_function_raises(self):
124124
with pytest.raises(Exception, match="not defined"):
125125
self._exec("str(a)", a=42)
126126

127-
def test_boolean_result_raises(self):
128-
with pytest.raises(ValueError, match="got bool"):
129-
self._exec("a > b", a=5, b=3)
127+
def test_boolean_result(self):
128+
result = self._exec("a > b", a=5, b=3)
129+
assert result[2] is True
130+
result = self._exec("a > b", a=3, b=5)
131+
assert result[2] is False
130132

131133
def test_empty_expression_raises(self):
132134
with pytest.raises(ValueError, match="Expression cannot be empty"):

0 commit comments

Comments
 (0)