Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Enhance `throw_error_if_no_points` error message (#4927)

Stimmie5a9062ac687e
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index 6b61aadf9..8a4540234 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -3336,9 +3336,16 @@ class Mobject:
def throw_error_if_no_points(self) -> None:
if self.has_no_points():
caller_name = sys._getframe(1).f_code.co_name
- raise Exception(
- f"Cannot call Mobject.{caller_name} for a Mobject with no points",
- )
+ cls = type(self).__name__
+ message = f"Cannot call {cls}.{caller_name} because {self!r} has no points."
+ pointful_family_members = self.family_members_with_points()
+ if pointful_family_members:
+ count = len(pointful_family_members)
+ message += (
+ f" Its family contains {count} "
+ f"mobject{'' if count == 1 else 's'} with points."
+ )
+ raise ValueError(message)
# About z-index
def set_z_index(
diff --git a/manim/mobject/opengl/opengl_mobject.py b/manim/mobject/opengl/opengl_mobject.py
index 32f073697..a99b6aff2 100644
--- a/manim/mobject/opengl/opengl_mobject.py
+++ b/manim/mobject/opengl/opengl_mobject.py
@@ -3014,11 +3014,17 @@ class OpenGLMobject:
def throw_error_if_no_points(self) -> None:
if not self.has_points():
- message = (
- "Cannot call OpenGLMobject.{} " + "for a OpenGLMobject with no points"
- )
caller_name = sys._getframe(1).f_code.co_name
- raise Exception(message.format(caller_name))
+ cls = type(self).__name__
+ message = f"Cannot call {cls}.{caller_name} because {self!r} has no points."
+ pointful_family_members = self.family_members_with_points()
+ if pointful_family_members:
+ count = len(pointful_family_members)
+ message += (
+ f" Its family contains {count} "
+ f"mobject{'' if count == 1 else 's'} with points."
+ )
+ raise ValueError(message)
class OpenGLGroup(OpenGLMobject):
diff --git a/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py b/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py
index 8881a26d9..4e083611a 100644
--- a/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py
+++ b/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py
@@ -139,10 +139,27 @@ def test_vmobject_point_from_proportion():
obj.point_from_proportion(2)
obj.clear_points()
- with pytest.raises(Exception, match="with no points"):
+ with pytest.raises(
+ ValueError,
+ match=r"Cannot call VMobject\.point_from_proportion because VMobject has no points\.",
+ ):
obj.point_from_proportion(0)
+def test_no_points_error_reports_pointful_family_members():
+ child = VMobject().set_points_as_corners([[0, 0, 0], [1, 0, 0]])
+ group = VGroup(VGroup(child))
+
+ with pytest.raises(ValueError) as error:
+ group.point_from_proportion(0)
+
+ message = str(error.value)
+ assert message.startswith(
+ "Cannot call VGroup.point_from_proportion because VGroup(",
+ )
+ assert message.endswith("Its family contains 1 mobject with points.")
+
+
def test_curves_as_submobjects_point_from_proportion():
obj = CurvesAsSubmobjects(VGroup())
diff --git a/tests/opengl/test_opengl_vectorized_mobject.py b/tests/opengl/test_opengl_vectorized_mobject.py
index 6cdc3d3c2..796abbdc1 100644
--- a/tests/opengl/test_opengl_vectorized_mobject.py
+++ b/tests/opengl/test_opengl_vectorized_mobject.py
@@ -5,7 +5,10 @@ import pytest
from manim import Circle, Line, Square, VDict, VGroup, VMobject
from manim.mobject.opengl.opengl_mobject import OpenGLMobject
-from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject
+from manim.mobject.opengl.opengl_vectorized_mobject import (
+ OpenGLVGroup,
+ OpenGLVMobject,
+)
def test_opengl_vmobject_add(using_opengl_renderer):
@@ -75,10 +78,32 @@ def test_opengl_vmobject_point_from_proportion(using_opengl_renderer):
obj.point_from_proportion(2)
obj.clear_points()
- with pytest.raises(Exception, match="with no points"):
+ with pytest.raises(
+ ValueError,
+ match=(
+ r"Cannot call OpenGLVMobject\.point_from_proportion because "
+ r"OpenGLVMobject has no points\."
+ ),
+ ):
obj.point_from_proportion(0)
+def test_opengl_no_points_error_reports_pointful_family_members(
+ using_opengl_renderer,
+):
+ child = OpenGLVMobject().set_points_as_corners([[0, 0, 0], [1, 0, 0]])
+ group = OpenGLVGroup(OpenGLVGroup(child))
+
+ with pytest.raises(ValueError) as error:
+ group.point_from_proportion(0)
+
+ message = str(error.value)
+ assert message.startswith(
+ "Cannot call OpenGLVGroup.point_from_proportion because OpenGLVGroup(",
+ )
+ assert message.endswith("Its family contains 1 mobject with points.")
+
+
def test_vgroup_init(using_opengl_renderer):
"""Test the VGroup instantiation."""
VGroup()