A community-maintained Python framework for creating mathematical animations.
Fix assertions and improve error messages when adding submobjects (#3756)
Francisco Manríquez Novoa0d67dcdc6eaf
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.pyindex 3369f3a24..0ae2a29da 100644--- a/manim/mobject/mobject.py+++ b/manim/mobject/mobject.py@@ -116,6 +116,63 @@ class Mobject:self.generate_points()self.init_colors()+ def _assert_valid_submobjects(self, submobjects: Iterable[Mobject]) -> Self:+ """Check that all submobjects are actually instances of+ :class:`Mobject`, and that none of them is ``self`` (a+ :class:`Mobject` cannot contain itself).++ This is an auxiliary function called when adding Mobjects to the+ :attr:`submobjects` list.++ This function is intended to be overridden by subclasses such as+ :class:`VMobject`, which should assert that only other VMobjects+ may be added into it.++ Parameters+ ----------+ submobjects+ The list containing values to validate.++ Returns+ -------+ :class:`Mobject`+ The Mobject itself.++ Raises+ ------+ TypeError+ If any of the values in `submobjects` is not a :class:`Mobject`.+ ValueError+ If there was an attempt to add a :class:`Mobject` as its own+ submobject.+ """+ return self._assert_valid_submobjects_internal(submobjects, Mobject)++ def _assert_valid_submobjects_internal(+ self, submobjects: list[Mobject], mob_class: type[Mobject]+ ) -> Self:+ for i, submob in enumerate(submobjects):+ if not isinstance(submob, mob_class):+ error_message = (+ f"Only values of type {mob_class.__name__} can be added "+ f"as submobjects of {type(self).__name__}, but the value "+ f"{submob} (at index {i}) is of type "+ f"{type(submob).__name__}."+ )+ # Intended for subclasses such as VMobject, which+ # cannot have regular Mobjects as submobjects+ if isinstance(submob, Mobject):+ error_message += (+ " You can try adding this value into a Group instead."+ )+ raise TypeError(error_message)+ if submob is self:+ raise ValueError(+ f"Cannot add {type(self).__name__} as a submobject of "+ f"itself (at index {i})."+ )+ return self+@classmethoddef animation_override_for(cls,@@ -414,12 +471,19 @@ class Mobject:>>> len(outer.submobjects)1+ Only Mobjects can be added::++ >>> outer.add(3)+ Traceback (most recent call last):+ ...+ TypeError: Only values of type Mobject can be added as submobjects of Mobject, but the value 3 (at index 0) is of type int.+Adding an object to itself raises an error::>>> outer.add(outer)Traceback (most recent call last):...- ValueError: Mobject cannot contain self+ ValueError: Cannot add Mobject as a submobject of itself (at index 0).A given mobject cannot be added as a submobjecttwice to some parent::@@ -433,12 +497,7 @@ class Mobject:[child]"""- for m in mobjects:- if not isinstance(m, Mobject):- raise TypeError("All submobjects must be of type Mobject")- if m is self:- raise ValueError("Mobject cannot contain self")-+ self._assert_valid_submobjects(mobjects)unique_mobjects = remove_list_redundancies(mobjects)if len(mobjects) != len(unique_mobjects):logger.warning(@@ -464,10 +523,7 @@ class Mobject:mobjectThe mobject to be inserted."""- if not isinstance(mobject, Mobject):- raise TypeError("All submobjects must be of type Mobject")- if mobject is self:- raise ValueError("Mobject cannot contain self")+ self._assert_valid_submobjects([mobject])self.submobjects.insert(index, mobject)def __add__(self, mobject: Mobject):@@ -520,13 +576,7 @@ class Mobject::meth:`add`"""- if self in mobjects:- raise ValueError("A mobject shouldn't contain itself")-- for mobject in mobjects:- if not isinstance(mobject, Mobject):- raise TypeError("All submobjects must be of type Mobject")-+ self._assert_valid_submobjects(mobjects)self.remove(*mobjects)# dict.fromkeys() removes duplicates while maintaining orderself.submobjects = list(dict.fromkeys(mobjects)) + self.submobjectsdiff --git a/manim/mobject/opengl/opengl_mobject.py b/manim/mobject/opengl/opengl_mobject.pyindex 38c65cdc8..3bdbd7c0b 100644--- a/manim/mobject/opengl/opengl_mobject.py+++ b/manim/mobject/opengl/opengl_mobject.py@@ -165,6 +165,64 @@ class OpenGLMobject:self.should_render = should_render+ def _assert_valid_submobjects(self, submobjects: Iterable[OpenGLMobject]) -> Self:+ """Check that all submobjects are actually instances of+ :class:`OpenGLMobject`, and that none of them is+ ``self`` (an :class:`OpenGLMobject` cannot contain itself).++ This is an auxiliary function called when adding OpenGLMobjects to the+ :attr:`submobjects` list.++ This function is intended to be overridden by subclasses such as+ :class:`OpenGLVMobject`, which should assert that only other+ OpenGLVMobjects may be added into it.++ Parameters+ ----------+ submobjects+ The list containing values to validate.++ Returns+ -------+ :class:`OpenGLMobject`+ The OpenGLMobject itself.++ Raises+ ------+ TypeError+ If any of the values in `submobjects` is not an+ :class:`OpenGLMobject`.+ ValueError+ If there was an attempt to add an :class:`OpenGLMobject` as its own+ submobject.+ """+ return self._assert_valid_submobjects_internal(submobjects, OpenGLMobject)++ def _assert_valid_submobjects_internal(+ self, submobjects: list[OpenGLMobject], mob_class: type[OpenGLMobject]+ ) -> Self:+ for i, submob in enumerate(submobjects):+ if not isinstance(submob, mob_class):+ error_message = (+ f"Only values of type {mob_class.__name__} can be added "+ f"as submobjects of {type(self).__name__}, but the value "+ f"{submob} (at index {i}) is of type "+ f"{type(submob).__name__}."+ )+ # Intended for subclasses such as OpenGLVMobject, which+ # cannot have regular OpenGLMobjects as submobjects+ if isinstance(submob, OpenGLMobject):+ error_message += (+ " You can try adding this value into a Group instead."+ )+ raise TypeError(error_message)+ if submob is self:+ raise ValueError(+ f"Cannot add {type(self).__name__} as a submobject of "+ f"itself (at index {i})."+ )+ return self+@classmethoddef __init_subclass__(cls, **kwargs):super().__init_subclass__(**kwargs)@@ -734,28 +792,33 @@ class OpenGLMobject:>>> len(outer.submobjects)1+ Only OpenGLMobjects can be added::++ >>> outer.add(3)+ Traceback (most recent call last):+ ...+ TypeError: Only values of type OpenGLMobject can be added as submobjects of OpenGLMobject, but the value 3 (at index 0) is of type int.+Adding an object to itself raises an error::>>> outer.add(outer)Traceback (most recent call last):...- ValueError: OpenGLMobject cannot contain self+ ValueError: Cannot add OpenGLMobject as a submobject of itself (at index 0)."""if update_parent:assert len(mobjects) == 1, "Can't set multiple parents."mobjects[0].parent = self- if self in mobjects:- raise ValueError("OpenGLMobject cannot contain self")+ self._assert_valid_submobjects(mobjects)+if any(mobjects.count(elem) > 1 for elem in mobjects):logger.warning("Attempted adding some Mobject as a child more than once, ""this is not possible. Repetitions are ignored.",)for mobject in mobjects:- if not isinstance(mobject, OpenGLMobject):- raise TypeError("All submobjects must be of type OpenGLMobject")if mobject not in self.submobjects:self.submobjects.append(mobject)if self not in mobject.parents:@@ -784,11 +847,7 @@ class OpenGLMobject:if update_parent:mobject.parent = self- if mobject is self:- raise ValueError("OpenGLMobject cannot contain self")-- if not isinstance(mobject, OpenGLMobject):- raise TypeError("All submobjects must be of type OpenGLMobject")+ self._assert_valid_submobjects([mobject])if mobject not in self.submobjects:self.submobjects.insert(index, mobject)@@ -880,10 +939,12 @@ class OpenGLMobject::meth:`add`"""+ self._assert_valid_submobjects(mobjects)self.submobjects = list_update(mobjects, self.submobjects)return selfdef replace_submobject(self, index, new_submob):+ self._assert_valid_submobjects([new_submob])old_submob = self.submobjects[index]if self in old_submob.parents:old_submob.parents.remove(self)@@ -2734,8 +2795,6 @@ class OpenGLMobject:class OpenGLGroup(OpenGLMobject):def __init__(self, *mobjects, **kwargs):- if not all(isinstance(m, OpenGLMobject) for m in mobjects):- raise Exception("All submobjects must be of type OpenGLMobject")super().__init__(**kwargs)self.add(*mobjects)diff --git a/manim/mobject/opengl/opengl_vectorized_mobject.py b/manim/mobject/opengl/opengl_vectorized_mobject.pyindex 41d6aac08..b7b5d4ce5 100644--- a/manim/mobject/opengl/opengl_vectorized_mobject.py+++ b/manim/mobject/opengl/opengl_vectorized_mobject.py@@ -25,7 +25,7 @@ from manim.utils.bezier import ()from manim.utils.color import BLACK, WHITE, ManimColor, ParsableManimColorfrom manim.utils.config_ops import _Data-from manim.utils.iterables import listify, make_even, resize_with_interpolation+from manim.utils.iterables import make_even, resize_with_interpolation, tuplifyfrom manim.utils.space_ops import (angle_between_vectors,cross2d,@@ -161,6 +161,9 @@ class OpenGLVMobject(OpenGLMobject):if stroke_color is not None:self.stroke_color = ManimColor.parse(stroke_color)+ def _assert_valid_submobjects(self, submobjects: Iterable[OpenGLVMobject]) -> Self:+ return self._assert_valid_submobjects_internal(submobjects, OpenGLVMobject)+def get_group_class(self):return OpenGLVGroup@@ -266,7 +269,7 @@ class OpenGLVMobject(OpenGLMobject):if width is not None:for mob in self.get_family(recurse):- mob.stroke_width = np.array([[width] for width in listify(width)])+ mob.stroke_width = np.array([[width] for width in tuplify(width)])if background is not None:for mob in self.get_family(recurse):@@ -1659,8 +1662,6 @@ class OpenGLVGroup(OpenGLVMobject):"""def __init__(self, *vmobjects, **kwargs):- if not all(isinstance(m, OpenGLVMobject) for m in vmobjects):- raise Exception("All submobjects must be of type OpenGLVMobject")super().__init__(**kwargs)self.add(*vmobjects)@@ -1726,8 +1727,6 @@ class OpenGLVGroup(OpenGLVMobject):(gr-circle_red).animate.shift(RIGHT))"""- if not all(isinstance(m, OpenGLVMobject) for m in vmobjects):- raise TypeError("All submobjects must be of type OpenGLVMobject")return super().add(*vmobjects)def __add__(self, vmobject):@@ -1773,8 +1772,7 @@ class OpenGLVGroup(OpenGLVMobject):>>> config.renderer = original_renderer"""- if not all(isinstance(m, OpenGLVMobject) for m in value):- raise TypeError("All submobjects must be of type OpenGLVMobject")+ self._assert_valid_submobjects(tuplify(value))self.submobjects[key] = valuediff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.pyindex deb369062..d82346868 100644--- a/manim/mobject/types/vectorized_mobject.py+++ b/manim/mobject/types/vectorized_mobject.py@@ -172,6 +172,9 @@ class VMobject(Mobject):if stroke_color is not None:self.stroke_color = ManimColor.parse(stroke_color)+ def _assert_valid_submobjects(self, submobjects: Iterable[VMobject]) -> Self:+ return self._assert_valid_submobjects_internal(submobjects, VMobject)+# OpenGL compatibility@propertydef n_points_per_curve(self) -> int:@@ -2016,14 +2019,6 @@ class VGroup(VMobject, metaclass=ConvertToOpenGL):(gr-circle_red).animate.shift(RIGHT))"""- for m in vmobjects:- if not isinstance(m, (VMobject, OpenGLVMobject)):- raise TypeError(- f"All submobjects of {self.__class__.__name__} must be of type VMobject. "- f"Got {repr(m)} ({type(m).__name__}) instead. "- "You can try using `Group` instead."- )-return super().add(*vmobjects)def __add__(self, vmobject: VMobject) -> Self:@@ -2061,8 +2056,7 @@ class VGroup(VMobject, metaclass=ConvertToOpenGL):>>> new_obj = VMobject()>>> vgroup[0] = new_obj"""- if not all(isinstance(m, (VMobject, OpenGLVMobject)) for m in value):- raise TypeError("All submobjects must be of type VMobject")+ self._assert_valid_submobjects(tuplify(value))self.submobjects[key] = value@@ -2390,8 +2384,7 @@ class VDict(VMobject, metaclass=ConvertToOpenGL):self.add_key_value_pair('s', square_obj)"""- if not isinstance(value, (VMobject, OpenGLVMobject)):- raise TypeError("All submobjects must be of type VMobject")+ self._assert_valid_submobjects([value])mob = valueif self.show_keys:# This import is here and not at the top to avoid circular importdiff --git a/tests/module/mobject/mobject/test_mobject.py b/tests/module/mobject/mobject/test_mobject.pyindex bb228eb79..89deea93c 100644--- a/tests/module/mobject/mobject/test_mobject.py+++ b/tests/module/mobject/mobject/test_mobject.py@@ -26,15 +26,26 @@ def test_mobject_add():# check that Mobject.add() returns the Mobject (for chained calls)assert obj.add(Mobject()) is obj+ assert len(obj.submobjects) == 13+obj = Mobject()# a Mobject cannot contain itself- with pytest.raises(ValueError):- obj.add(obj)+ with pytest.raises(ValueError) as add_self_info:+ obj.add(Mobject(), obj, Mobject())+ assert str(add_self_info.value) == (+ "Cannot add Mobject as a submobject of itself (at index 1)."+ )+ assert len(obj.submobjects) == 0# can only add Mobjects- with pytest.raises(TypeError):- obj.add("foo")+ with pytest.raises(TypeError) as add_str_info:+ obj.add(Mobject(), Mobject(), "foo")+ assert str(add_str_info.value) == (+ "Only values of type Mobject can be added as submobjects of Mobject, "+ "but the value foo (at index 2) is of type str."+ )+ assert len(obj.submobjects) == 0def test_mobject_remove():diff --git a/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py b/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.pyindex a17383fd9..7e92a68ac 100644--- a/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py+++ b/tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py@@ -18,7 +18,54 @@ from manim import (from manim.constants import PI-def test_vmobject_point_from_propotion():+def test_vmobject_add():+ """Test the VMobject add method."""+ obj = VMobject()+ assert len(obj.submobjects) == 0++ obj.add(VMobject())+ assert len(obj.submobjects) == 1++ # Can't add non-VMobject values to a VMobject.+ with pytest.raises(TypeError) as add_int_info:+ obj.add(3)+ assert str(add_int_info.value) == (+ "Only values of type VMobject can be added as submobjects of VMobject, "+ "but the value 3 (at index 0) is of type int."+ )+ assert len(obj.submobjects) == 1++ # Plain Mobjects can't be added to a VMobject if they're not+ # VMobjects. Suggest adding them into a Group instead.+ with pytest.raises(TypeError) as add_mob_info:+ obj.add(Mobject())+ assert str(add_mob_info.value) == (+ "Only values of type VMobject can be added as submobjects of VMobject, "+ "but the value Mobject (at index 0) is of type Mobject. You can try "+ "adding this value into a Group instead."+ )+ assert len(obj.submobjects) == 1++ with pytest.raises(TypeError) as add_vmob_and_mob_info:+ # If only one of the added objects is not an instance of VMobject, none of them should be added+ obj.add(VMobject(), Mobject())+ assert str(add_vmob_and_mob_info.value) == (+ "Only values of type VMobject can be added as submobjects of VMobject, "+ "but the value Mobject (at index 1) is of type Mobject. You can try "+ "adding this value into a Group instead."+ )+ assert len(obj.submobjects) == 1++ # A VMobject or VGroup cannot contain itself.+ with pytest.raises(ValueError) as add_self_info:+ obj.add(obj)+ assert str(add_self_info.value) == (+ "Cannot add VMobject as a submobject of itself (at index 0)."+ )+ assert len(obj.submobjects) == 1+++def test_vmobject_point_from_proportion():obj = VMobject()# One long line, one short line@@ -79,28 +126,77 @@ def test_vgroup_init():VGroup()VGroup(VMobject())VGroup(VMobject(), VMobject())- with pytest.raises(TypeError):++ # A VGroup cannot contain non-VMobject values.+ with pytest.raises(TypeError) as init_with_float_info:+ VGroup(3.0)+ assert str(init_with_float_info.value) == (+ "Only values of type VMobject can be added as submobjects of VGroup, "+ "but the value 3.0 (at index 0) is of type float."+ )++ with pytest.raises(TypeError) as init_with_mob_info:VGroup(Mobject())- with pytest.raises(TypeError):- VGroup(Mobject(), Mobject())+ assert str(init_with_mob_info.value) == (+ "Only values of type VMobject can be added as submobjects of VGroup, "+ "but the value Mobject (at index 0) is of type Mobject. You can try "+ "adding this value into a Group instead."+ )++ with pytest.raises(TypeError) as init_with_vmob_and_mob_info:+ VGroup(VMobject(), Mobject())+ assert str(init_with_vmob_and_mob_info.value) == (+ "Only values of type VMobject can be added as submobjects of VGroup, "+ "but the value Mobject (at index 1) is of type Mobject. You can try "+ "adding this value into a Group instead."+ )def test_vgroup_add():"""Test the VGroup add method."""obj = VGroup()assert len(obj.submobjects) == 0+obj.add(VMobject())assert len(obj.submobjects) == 1- with pytest.raises(TypeError):++ # Can't add non-VMobject values to a VMobject or VGroup.+ with pytest.raises(TypeError) as add_int_info:+ obj.add(3)+ assert str(add_int_info.value) == (+ "Only values of type VMobject can be added as submobjects of VGroup, "+ "but the value 3 (at index 0) is of type int."+ )+ assert len(obj.submobjects) == 1++ # Plain Mobjects can't be added to a VMobject or VGroup if they're not+ # VMobjects. Suggest adding them into a Group instead.+ with pytest.raises(TypeError) as add_mob_info:obj.add(Mobject())+ assert str(add_mob_info.value) == (+ "Only values of type VMobject can be added as submobjects of VGroup, "+ "but the value Mobject (at index 0) is of type Mobject. You can try "+ "adding this value into a Group instead."+ )assert len(obj.submobjects) == 1- with pytest.raises(TypeError):- # If only one of the added object is not an instance of VMobject, none of them should be added++ with pytest.raises(TypeError) as add_vmob_and_mob_info:+ # If only one of the added objects is not an instance of VMobject, none of them should be addedobj.add(VMobject(), Mobject())+ assert str(add_vmob_and_mob_info.value) == (+ "Only values of type VMobject can be added as submobjects of VGroup, "+ "but the value Mobject (at index 1) is of type Mobject. You can try "+ "adding this value into a Group instead."+ )assert len(obj.submobjects) == 1- with pytest.raises(ValueError):- # a Mobject cannot contain itself++ # A VMobject or VGroup cannot contain itself.+ with pytest.raises(ValueError) as add_self_info:obj.add(obj)+ assert str(add_self_info.value) == (+ "Cannot add VGroup as a submobject of itself (at index 0)."+ )+ assert len(obj.submobjects) == 1def test_vgroup_add_dunder():@@ -234,7 +330,7 @@ def test_vgroup_supports_item_assigment():def test_vgroup_item_assignment_at_correct_position():- """Test VGroup item-assignment adds to correct position for VMObjects"""+ """Test VGroup item-assignment adds to correct position for VMobjects"""n_items = 10vgroup = VGroup()for _i in range(n_items):@@ -248,8 +344,12 @@ def test_vgroup_item_assignment_at_correct_position():def test_vgroup_item_assignment_only_allows_vmobjects():"""Test VGroup item-assignment raises TypeError when invalid type is passed"""vgroup = VGroup(VMobject())- with pytest.raises(TypeError, match="All submobjects must be of type VMobject"):+ with pytest.raises(TypeError) as assign_str_info:vgroup[0] = "invalid object"+ assert str(assign_str_info.value) == (+ "Only values of type VMobject can be added as submobjects of VGroup, "+ "but the value invalid object (at index 0) is of type str."+ )def test_trim_dummy():diff --git a/tests/opengl/test_opengl_mobject.py b/tests/opengl/test_opengl_mobject.pyindex f1a58fa6a..d5bfac97f 100644--- a/tests/opengl/test_opengl_mobject.py+++ b/tests/opengl/test_opengl_mobject.py@@ -25,15 +25,26 @@ def test_opengl_mobject_add(using_opengl_renderer):# check that OpenGLMobject.add() returns the OpenGLMobject (for chained calls)assert obj.add(OpenGLMobject()) is obj+ assert len(obj.submobjects) == 13+obj = OpenGLMobject()- # a OpenGLMobject cannot contain itself- with pytest.raises(ValueError):- obj.add(obj)+ # an OpenGLMobject cannot contain itself+ with pytest.raises(ValueError) as add_self_info:+ obj.add(OpenGLMobject(), obj, OpenGLMobject())+ assert str(add_self_info.value) == (+ "Cannot add OpenGLMobject as a submobject of itself (at index 1)."+ )+ assert len(obj.submobjects) == 0- # can only add OpenGLMobjects- with pytest.raises(TypeError):- obj.add("foo")+ # can only add Mobjects+ with pytest.raises(TypeError) as add_str_info:+ obj.add(OpenGLMobject(), OpenGLMobject(), "foo")+ assert str(add_str_info.value) == (+ "Only values of type OpenGLMobject can be added as submobjects of "+ "OpenGLMobject, but the value foo (at index 2) is of type str."+ )+ assert len(obj.submobjects) == 0def test_opengl_mobject_remove(using_opengl_renderer):diff --git a/tests/opengl/test_opengl_vectorized_mobject.py b/tests/opengl/test_opengl_vectorized_mobject.pyindex 8e0ba2258..6f73ef026 100644--- a/tests/opengl/test_opengl_vectorized_mobject.py+++ b/tests/opengl/test_opengl_vectorized_mobject.py@@ -8,7 +8,54 @@ from manim.mobject.opengl.opengl_mobject import OpenGLMobjectfrom manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject-def test_opengl_vmobject_point_from_propotion(using_opengl_renderer):+def test_opengl_vmobject_add(using_opengl_renderer):+ """Test the OpenGLVMobject add method."""+ obj = OpenGLVMobject()+ assert len(obj.submobjects) == 0++ obj.add(OpenGLVMobject())+ assert len(obj.submobjects) == 1++ # Can't add non-OpenGLVMobject values to a VMobject.+ with pytest.raises(TypeError) as add_int_info:+ obj.add(3)+ assert str(add_int_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "OpenGLVMobject, but the value 3 (at index 0) is of type int."+ )+ assert len(obj.submobjects) == 1++ # Plain OpenGLMobjects can't be added to a OpenGLVMobject if they're not+ # OpenGLVMobjects. Suggest adding them into an OpenGLGroup instead.+ with pytest.raises(TypeError) as add_mob_info:+ obj.add(OpenGLMobject())+ assert str(add_mob_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "OpenGLVMobject, but the value OpenGLMobject (at index 0) is of type "+ "OpenGLMobject. You can try adding this value into a Group instead."+ )+ assert len(obj.submobjects) == 1++ with pytest.raises(TypeError) as add_vmob_and_mob_info:+ # If only one of the added objects is not an instance of VMobject, none of them should be added+ obj.add(OpenGLVMobject(), OpenGLMobject())+ assert str(add_vmob_and_mob_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "OpenGLVMobject, but the value OpenGLMobject (at index 1) is of type "+ "OpenGLMobject. You can try adding this value into a Group instead."+ )+ assert len(obj.submobjects) == 1++ # A VMobject or VGroup cannot contain itself.+ with pytest.raises(ValueError) as add_self_info:+ obj.add(obj)+ assert str(add_self_info.value) == (+ "Cannot add OpenGLVMobject as a submobject of itself (at index 0)."+ )+ assert len(obj.submobjects) == 1+++def test_opengl_vmobject_point_from_proportion(using_opengl_renderer):obj = OpenGLVMobject()# One long line, one short line@@ -37,28 +84,77 @@ def test_vgroup_init(using_opengl_renderer):VGroup()VGroup(OpenGLVMobject())VGroup(OpenGLVMobject(), OpenGLVMobject())- with pytest.raises(TypeError):++ # A VGroup cannot contain non-VMobject values.+ with pytest.raises(TypeError) as init_with_float_info:+ VGroup(3.0)+ assert str(init_with_float_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "VGroup, but the value 3.0 (at index 0) is of type float."+ )++ with pytest.raises(TypeError) as init_with_mob_info:VGroup(OpenGLMobject())- with pytest.raises(TypeError):- VGroup(OpenGLMobject(), OpenGLMobject())+ assert str(init_with_mob_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "VGroup, but the value OpenGLMobject (at index 0) is of type "+ "OpenGLMobject. You can try adding this value into a Group instead."+ )++ with pytest.raises(TypeError) as init_with_vmob_and_mob_info:+ VGroup(OpenGLVMobject(), OpenGLMobject())+ assert str(init_with_vmob_and_mob_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "VGroup, but the value OpenGLMobject (at index 1) is of type "+ "OpenGLMobject. You can try adding this value into a Group instead."+ )def test_vgroup_add(using_opengl_renderer):"""Test the VGroup add method."""obj = VGroup()assert len(obj.submobjects) == 0+obj.add(OpenGLVMobject())assert len(obj.submobjects) == 1- with pytest.raises(TypeError):++ # Can't add non-OpenGLVMobject values to a VMobject.+ with pytest.raises(TypeError) as add_int_info:+ obj.add(3)+ assert str(add_int_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "VGroup, but the value 3 (at index 0) is of type int."+ )+ assert len(obj.submobjects) == 1++ # Plain OpenGLMobjects can't be added to a OpenGLVMobject if they're not+ # OpenGLVMobjects. Suggest adding them into an OpenGLGroup instead.+ with pytest.raises(TypeError) as add_mob_info:obj.add(OpenGLMobject())+ assert str(add_mob_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "VGroup, but the value OpenGLMobject (at index 0) is of type "+ "OpenGLMobject. You can try adding this value into a Group instead."+ )assert len(obj.submobjects) == 1- with pytest.raises(TypeError):- # If only one of the added object is not an instance of OpenGLVMobject, none of them should be added++ with pytest.raises(TypeError) as add_vmob_and_mob_info:+ # If only one of the added objects is not an instance of VMobject, none of them should be addedobj.add(OpenGLVMobject(), OpenGLMobject())+ assert str(add_vmob_and_mob_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "VGroup, but the value OpenGLMobject (at index 1) is of type "+ "OpenGLMobject. You can try adding this value into a Group instead."+ )assert len(obj.submobjects) == 1- with pytest.raises(ValueError):- # a OpenGLMobject cannot contain itself++ # A VMobject or VGroup cannot contain itself.+ with pytest.raises(ValueError) as add_self_info:obj.add(obj)+ assert str(add_self_info.value) == (+ "Cannot add VGroup as a submobject of itself (at index 0)."+ )+ assert len(obj.submobjects) == 1def test_vgroup_add_dunder(using_opengl_renderer):@@ -192,7 +288,7 @@ def test_vgroup_supports_item_assigment(using_opengl_renderer):def test_vgroup_item_assignment_at_correct_position(using_opengl_renderer):- """Test VGroup item-assignment adds to correct position for OpenGLVMObjects"""+ """Test VGroup item-assignment adds to correct position for OpenGLVMobjects"""n_items = 10vgroup = VGroup()for _i in range(n_items):@@ -206,5 +302,9 @@ def test_vgroup_item_assignment_at_correct_position(using_opengl_renderer):def test_vgroup_item_assignment_only_allows_vmobjects(using_opengl_renderer):"""Test VGroup item-assignment raises TypeError when invalid type is passed"""vgroup = VGroup(OpenGLVMobject())- with pytest.raises(TypeError, match="All submobjects must be of type VMobject"):+ with pytest.raises(TypeError) as assign_str_info:vgroup[0] = "invalid object"+ assert str(assign_str_info.value) == (+ "Only values of type OpenGLVMobject can be added as submobjects of "+ "VGroup, but the value invalid object (at index 0) is of type str."+ )