Skip to content

Commit 828497b

Browse files
Fixed docstring formatting of :meth:.Scene.replace and improved its error handling (#3204)
* fix and improve formatting of replace docstring * replace assert statements by ValueError * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed ValueError message --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c98f3c7 commit 828497b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

manim/scene/scene.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -521,21 +521,22 @@ def remove(self, *mobjects: Mobject):
521521
return self
522522

523523
def replace(self, old_mobject: Mobject, new_mobject: Mobject) -> None:
524-
"""Replace one Mobject in the scene with another, preserving draw order.
524+
"""Replace one mobject in the scene with another, preserving draw order.
525525
526-
If old_mobject is a submobject of some other Mobject (e.g. a Group),
527-
the new_mobject will replace it inside the group, without otherwise
528-
changing the parent mobject.
526+
If ``old_mobject`` is a submobject of some other Mobject (e.g. a
527+
:class:`.Group`), the new_mobject will replace it inside the group,
528+
without otherwise changing the parent mobject.
529529
530530
Parameters
531531
----------
532-
old_mobject - A Mobject which must be in the scene. This method asserts if
533-
old_mobject is not in the scene
534-
new_mobject - A Mobject which must not already be in the scene.
532+
old_mobject
533+
The mobject to be replaced. Must be present in the scene.
534+
new_mobject
535+
A mobject which must not already be in the scene.
535536
536537
"""
537-
assert old_mobject is not None
538-
assert new_mobject is not None
538+
if old_mobject is None or new_mobject is None:
539+
raise ValueError("Specified mobjects cannot be None")
539540

540541
def replace_in_list(
541542
mobj_list: list[Mobject], old_m: Mobject, new_m: Mobject
@@ -562,7 +563,9 @@ def replace_in_list(
562563
replaced = replace_in_list(
563564
self.mobjects, old_mobject, new_mobject
564565
) or replace_in_list(self.foreground_mobjects, old_mobject, new_mobject)
565-
assert replaced, "Could not find old_mobject in Scene"
566+
567+
if not replaced:
568+
raise ValueError(f"Could not find {old_mobject} in scene")
566569

567570
def add_updater(self, func: Callable[[float], None]) -> None:
568571
"""Add an update function to the scene.

0 commit comments

Comments
 (0)