Skip to content

Commit 190ae5d

Browse files
Fix None check order in _tree_layout (#3421)
* Fix None check order in _tree_layout * add tests to test_graph.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5d73525 commit 190ae5d

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

manim/mobject/graph.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,12 @@ def _tree_layout(
112112
vertex_spacing: tuple | None = None,
113113
orientation: str = "down",
114114
):
115-
children = {root_vertex: list(T.neighbors(root_vertex))}
116-
117-
if not nx.is_tree(T):
118-
raise ValueError("The tree layout must be used with trees")
119115
if root_vertex is None:
120116
raise ValueError("The tree layout requires the root_vertex parameter")
117+
if not nx.is_tree(T):
118+
raise ValueError("The tree layout must be used with trees")
121119

120+
children = {root_vertex: list(T.neighbors(root_vertex))}
122121
# The following code is SageMath's tree layout implementation, taken from
123122
# https://github.com/sagemath/sage/blob/cc60cfebc4576fed8b01f0fc487271bdee3cefed/src/sage/graphs/graph_plot.py#L1447
124123

tests/module/mobject/test_graph.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import pytest
4+
35
from manim import DiGraph, Graph, Scene, Text, tempconfig
46

57

@@ -100,3 +102,15 @@ def test_custom_animation_mobject_list():
100102
scene.play(G.animate.remove_vertices(2))
101103
assert str(G) == "Undirected graph on 3 vertices and 0 edges"
102104
assert scene.mobjects == [G]
105+
106+
107+
def test_tree_layout_no_root_error():
108+
with pytest.raises(ValueError) as excinfo:
109+
G = Graph([1, 2, 3], [(1, 2), (2, 3)], layout="tree")
110+
assert str(excinfo.value) == "The tree layout requires the root_vertex parameter"
111+
112+
113+
def test_tree_layout_not_tree_error():
114+
with pytest.raises(ValueError) as excinfo:
115+
G = Graph([1, 2, 3], [(1, 2), (2, 3), (3, 1)], layout="tree", root_vertex=1)
116+
assert str(excinfo.value) == "The tree layout must be used with trees"

0 commit comments

Comments
 (0)