Skip to content

Commit

Permalink
Fix None check order in _tree_layout (ManimCommunity#3421)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Nikhil-42 and pre-commit-ci[bot] authored Nov 2, 2023
1 parent 5d73525 commit 190ae5d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 3 additions & 4 deletions manim/mobject/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,12 @@ def _tree_layout(
vertex_spacing: tuple | None = None,
orientation: str = "down",
):
children = {root_vertex: list(T.neighbors(root_vertex))}

if not nx.is_tree(T):
raise ValueError("The tree layout must be used with trees")
if root_vertex is None:
raise ValueError("The tree layout requires the root_vertex parameter")
if not nx.is_tree(T):
raise ValueError("The tree layout must be used with trees")

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

Expand Down
14 changes: 14 additions & 0 deletions tests/module/mobject/test_graph.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import pytest

from manim import DiGraph, Graph, Scene, Text, tempconfig


Expand Down Expand Up @@ -100,3 +102,15 @@ def test_custom_animation_mobject_list():
scene.play(G.animate.remove_vertices(2))
assert str(G) == "Undirected graph on 3 vertices and 0 edges"
assert scene.mobjects == [G]


def test_tree_layout_no_root_error():
with pytest.raises(ValueError) as excinfo:
G = Graph([1, 2, 3], [(1, 2), (2, 3)], layout="tree")
assert str(excinfo.value) == "The tree layout requires the root_vertex parameter"


def test_tree_layout_not_tree_error():
with pytest.raises(ValueError) as excinfo:
G = Graph([1, 2, 3], [(1, 2), (2, 3), (3, 1)], layout="tree", root_vertex=1)
assert str(excinfo.value) == "The tree layout must be used with trees"

0 comments on commit 190ae5d

Please sign in to comment.