mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-05-10 17:22:36 +08:00
Add Boolean support to Math Expression Node (#13224)
Some checks are pending
Python Linting / Run Ruff (push) Waiting to run
Python Linting / Run Pylint (push) Waiting to run
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.10, [self-hosted Linux], stable) (push) Waiting to run
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.11, [self-hosted Linux], stable) (push) Waiting to run
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.12, [self-hosted Linux], stable) (push) Waiting to run
Full Comfy CI Workflow Runs / test-unix-nightly (12.1, , linux, 3.11, [self-hosted Linux], nightly) (push) Waiting to run
Execution Tests / test (macos-latest) (push) Waiting to run
Execution Tests / test (ubuntu-latest) (push) Waiting to run
Execution Tests / test (windows-latest) (push) Waiting to run
Test server launches without errors / test (push) Waiting to run
Unit Tests / test (macos-latest) (push) Waiting to run
Unit Tests / test (ubuntu-latest) (push) Waiting to run
Unit Tests / test (windows-2022) (push) Waiting to run
Some checks are pending
Python Linting / Run Ruff (push) Waiting to run
Python Linting / Run Pylint (push) Waiting to run
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.10, [self-hosted Linux], stable) (push) Waiting to run
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.11, [self-hosted Linux], stable) (push) Waiting to run
Full Comfy CI Workflow Runs / test-stable (12.1, , linux, 3.12, [self-hosted Linux], stable) (push) Waiting to run
Full Comfy CI Workflow Runs / test-unix-nightly (12.1, , linux, 3.11, [self-hosted Linux], nightly) (push) Waiting to run
Execution Tests / test (macos-latest) (push) Waiting to run
Execution Tests / test (ubuntu-latest) (push) Waiting to run
Execution Tests / test (windows-latest) (push) Waiting to run
Test server launches without errors / test (push) Waiting to run
Unit Tests / test (macos-latest) (push) Waiting to run
Unit Tests / test (ubuntu-latest) (push) Waiting to run
Unit Tests / test (windows-2022) (push) Waiting to run
* Add Boolean support to math expressions * Change boolean result test to assert values --------- Co-authored-by: Alexis Rolland <alexisrolland@hotmail.com>
This commit is contained in:
parent
20f5e474da
commit
95f6652ef5
@ -63,7 +63,7 @@ class MathExpressionNode(io.ComfyNode):
|
||||
@classmethod
|
||||
def define_schema(cls) -> io.Schema:
|
||||
autogrow = io.Autogrow.TemplateNames(
|
||||
input=io.MultiType.Input("value", [io.Float, io.Int]),
|
||||
input=io.MultiType.Input("value", [io.Float, io.Int, io.Boolean]),
|
||||
names=list(string.ascii_lowercase),
|
||||
min=1,
|
||||
)
|
||||
@ -82,6 +82,7 @@ class MathExpressionNode(io.ComfyNode):
|
||||
outputs=[
|
||||
io.Float.Output(display_name="FLOAT"),
|
||||
io.Int.Output(display_name="INT"),
|
||||
io.Boolean.Output(display_name="BOOL"),
|
||||
],
|
||||
)
|
||||
|
||||
@ -97,7 +98,7 @@ class MathExpressionNode(io.ComfyNode):
|
||||
|
||||
result = simple_eval(expression, names=context, functions=MATH_FUNCTIONS)
|
||||
# bool check must come first because bool is a subclass of int in Python
|
||||
if isinstance(result, bool) or not isinstance(result, (int, float)):
|
||||
if not isinstance(result, (int, float)):
|
||||
raise ValueError(
|
||||
f"Math Expression '{expression}' must evaluate to a numeric result, "
|
||||
f"got {type(result).__name__}: {result!r}"
|
||||
@ -106,7 +107,7 @@ class MathExpressionNode(io.ComfyNode):
|
||||
raise ValueError(
|
||||
f"Math Expression '{expression}' produced a non-finite result: {result}"
|
||||
)
|
||||
return io.NodeOutput(float(result), int(result))
|
||||
return io.NodeOutput(float(result), int(result), bool(result))
|
||||
|
||||
|
||||
class MathExtension(ComfyExtension):
|
||||
|
||||
@ -124,9 +124,11 @@ class TestMathExpressionExecute:
|
||||
with pytest.raises(Exception, match="not defined"):
|
||||
self._exec("str(a)", a=42)
|
||||
|
||||
def test_boolean_result_raises(self):
|
||||
with pytest.raises(ValueError, match="got bool"):
|
||||
self._exec("a > b", a=5, b=3)
|
||||
def test_boolean_result(self):
|
||||
result = self._exec("a > b", a=5, b=3)
|
||||
assert result[2] is True
|
||||
result = self._exec("a > b", a=3, b=5)
|
||||
assert result[2] is False
|
||||
|
||||
def test_empty_expression_raises(self):
|
||||
with pytest.raises(ValueError, match="Expression cannot be empty"):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user