Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(graph): reject mismatched collector items for union collection inputs
unmarco295548928287
diff --git a/invokeai/app/services/shared/graph_validation.py b/invokeai/app/services/shared/graph_validation.pyindex 801b97b83e..0ccb15e11f 100644--- a/invokeai/app/services/shared/graph_validation.py+++ b/invokeai/app/services/shared/graph_validation.py@@ -2315,19 +2315,31 @@ class Graph(BaseModel):if not all(is_list_or_contains_list(t) or is_any(t) for t in output_field_types):return "Collector output must connect to a collection input"+ # Read item types from the list branch: the first type argument of `Union[list[X], X]` or+ # `Optional[list[X]]` is `list[X]` itself, which issubclass() rejects with a TypeError.if input_root_type is not None:if not all(is_any(t)- or is_union_subtype(input_root_type, get_args(t)[0])- or issubclass(input_root_type, get_args(t)[0])+ or any(+ self._collector_item_matches(input_root_type, item_type)+ for item_type in extract_collection_item_types(t)+ )for t in output_field_types):return "Collector outputs must connect to a collection input with a matching type"- elif any(not is_any(t) and get_args(t)[0] != Any for t in output_field_types):+ elif any(not is_any(t) and Any not in extract_collection_item_types(t) for t in output_field_types):return "Collector outputs must connect to a collection input with a matching type"return None+ @staticmethod+ def _collector_item_matches(input_root_type: Any, item_type: Any) -> bool:+ if is_any(item_type) or is_union_subtype(input_root_type, item_type):+ return True+ return (+ isinstance(input_root_type, type) and isinstance(item_type, type) and issubclass(input_root_type, item_type)+ )+def _validate_downstream_collector_outputs(self,outputs: list[EdgeConnection],diff --git a/tests/test_node_graph.py b/tests/test_node_graph.pyindex d06a1905cb..d0e343a1fa 100644--- a/tests/test_node_graph.py+++ b/tests/test_node_graph.py@@ -21,6 +21,7 @@ from invokeai.app.invocations.fields import InputField, OutputField, OutputScopefrom invokeai.app.invocations.logic import IfInvocationfrom invokeai.app.invocations.loops import ForInvocation, ForReturnInvocationfrom invokeai.app.invocations.math import AddInvocation+from invokeai.app.invocations.metadata import MetadataInvocation, MetadataItemInvocationfrom invokeai.app.invocations.primitives import (ColorInvocation,FloatCollectionInvocation,@@ -50,6 +51,7 @@ from tests.test_nodes import (AnyTypeTestInvocation,ImageToImageTestInvocation,ListPassThroughInvocation,+ OptionalCollectionTestInvocation,PolymorphicStringTestInvocation,PromptCollectionTestInvocation,PromptTestInvocation,@@ -2199,6 +2201,49 @@ def test_graph_collector_rejects_mismatched_item_with_union_collection_input():g.add_edge(create_edge("3", "color", "2", "item"))+# A collection input whose annotation is not a bare list: the item type lives in a union branch.+COLLECTION_TARGETS_WITH_LIST_BRANCH = [+ pytest.param(lambda: MetadataInvocation(id="target"), "items", id="list-first-union"),+ pytest.param(lambda: OptionalCollectionTestInvocation(id="target"), "value", id="optional-list"),+]++[email protected](("make_target", "field"), COLLECTION_TARGETS_WITH_LIST_BRANCH)+def test_graph_collector_rejects_mismatched_items_for_collection_union_input(make_target, field: str):+ g = Graph()+ g.add_node(IntegerInvocation(id="1", value=1))+ g.add_node(IntegerInvocation(id="2", value=2))+ g.add_node(CollectInvocation(id="collect"))+ g.add_node(make_target())+ g.add_edge(create_edge("1", "value", "collect", "item"))+ g.add_edge(create_edge("2", "value", "collect", "item"))++ with pytest.raises(InvalidEdgeError, match="matching type"):+ g.add_edge(create_edge("collect", "collection", "target", field))+++def test_graph_collector_accepts_matching_items_for_list_first_union_input():+ g = Graph()+ g.add_node(MetadataItemInvocation(id="1", label="a", value=1))+ g.add_node(MetadataItemInvocation(id="2", label="b", value=2))+ g.add_node(CollectInvocation(id="collect"))+ g.add_node(MetadataInvocation(id="target"))+ g.add_edge(create_edge("1", "item", "collect", "item"))+ g.add_edge(create_edge("2", "item", "collect", "item"))++ g.add_edge(create_edge("collect", "collection", "target", "items"))+++def test_graph_collector_accepts_matching_items_for_optional_list_input():+ g = Graph()+ g.add_node(StringInvocation(id="1", value="a"))+ g.add_node(CollectInvocation(id="collect"))+ g.add_node(OptionalCollectionTestInvocation(id="target"))+ g.add_edge(create_edge("1", "value", "collect", "item"))++ g.add_edge(create_edge("collect", "collection", "target", "value"))++def test_graph_connects_iterator():g = Graph()n1 = ListPassThroughInvocation(id="1")diff --git a/tests/test_nodes.py b/tests/test_nodes.pyindex 6368e416c1..aaf50f35fc 100644--- a/tests/test_nodes.py+++ b/tests/test_nodes.py@@ -142,6 +142,14 @@ class UnionCollectionTestInvocation(BaseInvocation):return UnionCollectionTestInvocationOutput(value=self.value)+@invocation("test_optional_collection", version="1.0.0")+class OptionalCollectionTestInvocation(BaseInvocation):+ value: Union[list[str], None] = InputField(default=None)++ def invoke(self, context: InvocationContext) -> PromptCollectionTestInvocationOutput:+ return PromptCollectionTestInvocationOutput(collection=self.value or [])++# Importing these must happen after test invocations are defined or they won't registerfrom invokeai.app.services.events.events_base import EventServiceBase # noqa: E402from invokeai.app.services.shared.graph import Edge, EdgeConnection, GraphExecutionState # noqa: E402