Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Simplify dynamic error messages for invalid `VMobject` operations (#5065)

Indiana-Wilsona29ceb5a2e8e
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index ac6f43ee5..49a55b772 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -11,7 +11,6 @@ import itertools as it
import math
import operator as op
import random
-import sys
import types
import warnings
from collections.abc import Callable, Iterable, Iterator, MutableSet, Sequence
@@ -2515,12 +2514,12 @@ class Mobject:
def get_start(self) -> Point3D:
"""Returns the point, where the stroke that surrounds the :class:`~.Mobject` starts."""
- self.throw_error_if_no_points()
+ self._throw_error_if_no_points()
return np.array(self.points[0])
def get_end(self) -> Point3D:
"""Returns the point, where the stroke that surrounds the :class:`~.Mobject` ends."""
- self.throw_error_if_no_points()
+ self._throw_error_if_no_points()
return np.array(self.points[-1])
def get_start_and_end(self) -> tuple[Point3D, Point3D]:
@@ -3472,20 +3471,19 @@ class Mobject:
sm1.points = np.array(sm2.points)
return self
- # Errors
- def throw_error_if_no_points(self) -> None:
+ def _throw_error_if_no_points(self) -> None:
if self.has_no_points():
- caller_name = sys._getframe(1).f_code.co_name
- cls = type(self).__name__
- message = f"Cannot call {cls}.{caller_name} because {self!r} has no points."
+ error_reason = (
+ f"{self!r} has no points, but a method which requires points was called"
+ )
pointful_family_members = self.family_members_with_points()
if pointful_family_members:
count = len(pointful_family_members)
- message += (
- f" Its family contains {count} "
+ error_reason += (
+ f". Its family contains {count} "
f"mobject{'' if count == 1 else 's'} with points."
)
- raise ValueError(message)
+ raise ValueError(error_reason)
# About z-index
def set_z_index(
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index a7bb5ac4f..13781c194 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -950,7 +950,7 @@ class VMobject(Mobject):
:class:`VMobject`
``self``
"""
- self.throw_error_if_no_points()
+ self._throw_error_if_no_points()
new_points = [handle1, handle2, anchor]
if self.has_new_path_started():
self.append_points(new_points)
@@ -1038,7 +1038,7 @@ class VMobject(Mobject):
if self.has_new_path_started():
self.add_line_to(new_anchor)
else:
- self.throw_error_if_no_points()
+ self._throw_error_if_no_points()
last_h2, last_a2 = self.points[-2:]
last_tangent = last_a2 - last_h2
handle1 = last_a2 + last_tangent
@@ -1084,7 +1084,7 @@ class VMobject(Mobject):
The VMobject itself, after appending the straight lines to its
path.
"""
- self.throw_error_if_no_points()
+ self._throw_error_if_no_points()
points = np.asarray(points).reshape(-1, self.dim)
num_points = points.shape[0]
@@ -1659,7 +1659,7 @@ class VMobject(Mobject):
if alpha < 0 or alpha > 1:
raise ValueError(f"Alpha {alpha} not between 0 and 1.")
- self.throw_error_if_no_points()
+ self._throw_error_if_no_points()
if alpha == 1:
return self.points[-1]
@@ -1706,7 +1706,7 @@ class VMobject(Mobject):
:exc:`Exception`
If the :class:`VMobject` has no points.
"""
- self.throw_error_if_no_points()
+ self._throw_error_if_no_points()
# Iterate over each bezier curve that the ``VMobject`` is composed of, checking
# if the point lies on that curve. If it does not lie on that curve, add
@@ -2831,8 +2831,12 @@ class CurvesAsSubmobjects(VGroup):
if alpha < 0 or alpha > 1:
raise ValueError(f"Alpha {alpha} not between 0 and 1.")
- self._throw_error_if_no_submobjects()
- submobjs_with_pts = self._get_submobjects_with_points()
+ submobjs_with_pts = tuple(
+ part for part in self.submobjects if part.has_points()
+ )
+
+ if len(submobjs_with_pts) == 0:
+ raise ValueError(f"{self!r} does not have any submobjects with points")
if alpha == 1:
return submobjs_with_pts[-1].points[-1]
@@ -2853,24 +2857,6 @@ class CurvesAsSubmobjects(VGroup):
current_length += part_length
- def _throw_error_if_no_submobjects(self):
- if len(self.submobjects) == 0:
- caller_name = sys._getframe(1).f_code.co_name
- raise Exception(
- f"Cannot call CurvesAsSubmobjects. {caller_name} for a CurvesAsSubmobject with no submobjects"
- )
-
- def _get_submobjects_with_points(self):
- submobjs_with_pts = tuple(
- part for part in self.submobjects if len(part.points) > 0
- )
- if len(submobjs_with_pts) == 0:
- caller_name = sys._getframe(1).f_code.co_name
- raise Exception(
- f"Cannot call CurvesAsSubmobjects. {caller_name} for a CurvesAsSubmobject whose submobjects have no points"
- )
- return submobjs_with_pts
-
class DashedVMobject(VMobject, metaclass=ConvertToOpenGL):
"""A :class:`VMobject` composed of dashes instead of lines.
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 4e083611a..82ab5373f 100644
--- a/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py
+++ b/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py
@@ -141,7 +141,7 @@ def test_vmobject_point_from_proportion():
obj.clear_points()
with pytest.raises(
ValueError,
- match=r"Cannot call VMobject\.point_from_proportion because VMobject has no points\.",
+ match=rf"{obj!r} has no points",
):
obj.point_from_proportion(0)
@@ -155,7 +155,7 @@ def test_no_points_error_reports_pointful_family_members():
message = str(error.value)
assert message.startswith(
- "Cannot call VGroup.point_from_proportion because VGroup(",
+ f"{group!r} has no points",
)
assert message.endswith("Its family contains 1 mobject with points.")
@@ -165,11 +165,11 @@ def test_curves_as_submobjects_point_from_proportion():
with pytest.raises(ValueError, match="between 0 and 1"):
obj.point_from_proportion(2)
- with pytest.raises(Exception, match="with no submobjects"):
+ with pytest.raises(ValueError, match="does not have any submobjects with points"):
obj.point_from_proportion(0)
obj.add(VMobject())
- with pytest.raises(Exception, match="have no points"):
+ with pytest.raises(ValueError, match="does not have any submobjects with points"):
obj.point_from_proportion(0)
# submobject[0] is a line of length 4