Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix 305: mobject.add should do input validation (#750)

Leo Torres8a4f2c17effc
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index 74c7008bb..904fb50a2 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -96,6 +96,9 @@ class Mobject(Container):
------
:class:`ValueError`
When a mobject tries to add itself.
+ :class:`TypeError`
+ When trying to add an object that is not an instance of :class:`Mobject`.
+
Notes
-----
@@ -130,8 +133,11 @@ class Mobject(Container):
ValueError: Mobject cannot contain self
"""
- if self in mobjects:
- raise ValueError("Mobject cannot contain self")
+ 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.submobjects = list_update(self.submobjects, mobjects)
return self
@@ -1242,7 +1248,5 @@ class Group(Mobject):
"""Groups together multiple Mobjects."""
def __init__(self, *mobjects, **kwargs):
- if not all([isinstance(m, Mobject) for m in mobjects]):
- raise TypeError("All submobjects must be of type Mobject")
Mobject.__init__(self, **kwargs)
self.add(*mobjects)
diff --git a/tests/test_container.py b/tests/test_container.py
index 27aca4262..579ccf32f 100644
--- a/tests/test_container.py
+++ b/tests/test_container.py
@@ -58,6 +58,10 @@ def test_mobject_add():
with pytest.raises(ValueError):
obj.add(obj)
+ # can only add Mobjects
+ with pytest.raises(TypeError):
+ obj.add("foo")
+
def test_mobject_remove():
"""Test Mobject.remove()."""