core[patch]: support single-node subgraphs and put subgraph nodes under the respective subgraphs (#30234)

This commit is contained in:
Vadym Barda
2025-03-11 18:55:45 -04:00
committed by GitHub
parent 81d1653a30
commit c7842730ef
3 changed files with 105 additions and 39 deletions

View File

@@ -5,9 +5,6 @@
graph TD;
__start__([<p>__start__</p>]):::first
parent_1(parent_1)
child_child_1_grandchild_1(grandchild_1)
child_child_1_grandchild_2(grandchild_2<hr/><small><em>__interrupt = before</em></small>)
child_child_2(child_2)
parent_2(parent_2)
__end__([<p>__end__</p>]):::last
__start__ --> parent_1;
@@ -15,8 +12,11 @@
parent_1 --> child_child_1_grandchild_1;
parent_2 --> __end__;
subgraph child
child_child_2(child_2)
child_child_1_grandchild_2 --> child_child_2;
subgraph child_1
child_child_1_grandchild_1(grandchild_1)
child_child_1_grandchild_2(grandchild_2<hr/><small><em>__interrupt = before</em></small>)
child_child_1_grandchild_1 --> child_child_1_grandchild_2;
end
end
@@ -32,10 +32,6 @@
graph TD;
__start__([<p>__start__</p>]):::first
parent_1(parent_1)
child_child_1_grandchild_1(grandchild_1)
child_child_1_grandchild_1_greatgrandchild(greatgrandchild)
child_child_1_grandchild_2(grandchild_2<hr/><small><em>__interrupt = before</em></small>)
child_child_2(child_2)
parent_2(parent_2)
__end__([<p>__end__</p>]):::last
__start__ --> parent_1;
@@ -43,10 +39,14 @@
parent_1 --> child_child_1_grandchild_1;
parent_2 --> __end__;
subgraph child
child_child_2(child_2)
child_child_1_grandchild_2 --> child_child_2;
subgraph child_1
child_child_1_grandchild_1(grandchild_1)
child_child_1_grandchild_2(grandchild_2<hr/><small><em>__interrupt = before</em></small>)
child_child_1_grandchild_1_greatgrandchild --> child_child_1_grandchild_2;
subgraph grandchild_1
child_child_1_grandchild_1_greatgrandchild(greatgrandchild)
child_child_1_grandchild_1 --> child_child_1_grandchild_1_greatgrandchild;
end
end
@@ -1996,10 +1996,6 @@
graph TD;
__start__([<p>__start__</p>]):::first
outer_1(outer_1)
inner_1_inner_1(inner_1)
inner_1_inner_2(inner_2<hr/><small><em>__interrupt = before</em></small>)
inner_2_inner_1(inner_1)
inner_2_inner_2(inner_2)
outer_2(outer_2)
__end__([<p>__end__</p>]):::last
__start__ --> outer_1;
@@ -2009,9 +2005,13 @@
outer_1 --> inner_2_inner_1;
outer_2 --> __end__;
subgraph inner_1
inner_1_inner_1(inner_1)
inner_1_inner_2(inner_2<hr/><small><em>__interrupt = before</em></small>)
inner_1_inner_1 --> inner_1_inner_2;
end
subgraph inner_2
inner_2_inner_1(inner_1)
inner_2_inner_2(inner_2)
inner_2_inner_1 --> inner_2_inner_2;
end
classDef default fill:#f2f0ff,line-height:1.2
@@ -2020,6 +2020,23 @@
'''
# ---
# name: test_single_node_subgraph_mermaid[mermaid]
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
__start__([<p>__start__</p>]):::first
__end__([<p>__end__</p>]):::last
__start__ --> sub_meow;
sub_meow --> __end__;
subgraph sub
sub_meow(meow)
end
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc
'''
# ---
# name: test_trim
dict({
'edges': list([

View File

@@ -448,6 +448,23 @@ def test_triple_nested_subgraph_mermaid(snapshot: SnapshotAssertion) -> None:
assert graph.draw_mermaid() == snapshot(name="mermaid")
def test_single_node_subgraph_mermaid(snapshot: SnapshotAssertion) -> None:
empty_data = BaseModel
nodes = {
"__start__": Node(
id="__start__", name="__start__", data=empty_data, metadata=None
),
"sub:meow": Node(id="sub:meow", name="meow", data=empty_data, metadata=None),
"__end__": Node(id="__end__", name="__end__", data=empty_data, metadata=None),
}
edges = [
Edge(source="__start__", target="sub:meow", data=None, conditional=False),
Edge(source="sub:meow", target="__end__", data=None, conditional=False),
]
graph = Graph(nodes, edges)
assert graph.draw_mermaid() == snapshot(name="mermaid")
def test_runnable_get_graph_with_invalid_input_type() -> None:
"""Test that error isn't raised when getting graph with invalid input type."""