A community-maintained Python framework for creating mathematical animations.
Remove recursive remove_list_redundancies in get_family (#4947)
GniLudio1dc796e96522
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.pyindex 8a4540234..5d52899c7 100644--- a/manim/mobject/mobject.py+++ b/manim/mobject/mobject.py@@ -14,7 +14,7 @@ import randomimport sysimport typesimport warnings-from collections.abc import Callable, Iterable, Iterator, Sequence+from collections.abc import Callable, Iterable, Iterator, MutableSet, Sequencefrom functools import partialmethod, reducefrom pathlib import Pathfrom typing import TYPE_CHECKING, Any, cast@@ -2565,9 +2565,19 @@ class Mobject::meth:`~.Mobject.family_members_with_points`, :meth:`~.Mobject.align_data`"""- sub_families = [x.get_family() for x in self.submobjects]- all_mobjects = [self] + list(it.chain(*sub_families))- return remove_list_redundancies(all_mobjects)+ return remove_list_redundancies(list(self._iter_family(set())))++ def _iter_family(self, active_path: MutableSet[Mobject]) -> Iterator[Mobject]:+ if self in active_path:+ return++ active_path.add(self)+ try:+ yield self+ for submobject in self.submobjects:+ yield from submobject._iter_family(active_path)+ finally:+ active_path.remove(self)def family_members_with_points(self) -> list[Mobject]:"""Filters the list of family members (generated by :meth:`.get_family`) to include only mobjects with points.diff --git a/tests/module/mobject/mobject/test_family.py b/tests/module/mobject/mobject/test_family.pyindex 08856b653..c48c3ef4b 100644--- a/tests/module/mobject/mobject/test_family.py+++ b/tests/module/mobject/mobject/test_family.py@@ -5,7 +5,7 @@ import numpy as npfrom manim import RIGHT, Circle, Mobject-def test_family():+def test_family() -> None:"""Check that the family is gathered correctly."""# Check that an empty mobject's family only contains itselfmob = Mobject()@@ -38,7 +38,7 @@ def test_family():assert gc in family-def test_overlapping_family():+def test_overlapping_family() -> None:"""Check that each member of the family is only gathered once."""(mob,@@ -59,7 +59,7 @@ def test_overlapping_family():assert family.count(gchild_common) == 1-def test_shift_family():+def test_shift_family() -> None:"""Check that each member of the family is shifted along with the parent.Importantly, here we add a common grandchild to each of the children. So@@ -90,3 +90,38 @@ def test_shift_family():for m in family:np.testing.assert_allclose(positions_before[m] + RIGHT, positions_after[m])+++def test_family_cycle() -> None:+ """Check whether cycles are handled correctly."""+ # Mobject+ # Circle+ # Triangle+ # Square - Non-duplicate+ # Mobject - Duplicate/Loop+ # Circle - Duplicate on same level+ # Triangle - Parent is contained twice+ mobject, circle, square, triangle = Mobject(), Mobject(), Mobject(), Mobject()+ mobject.add(circle, square, circle)+ circle.add(triangle)+ square.add(mobject) # adds loop++ expected = [mobject, square, circle, triangle]+ assert mobject.get_family() == expected+++def test_family_order() -> None:+ """Check that the order of the family is correct."""+ # Mobject+ # Circle+ # Triangle+ # Square+ # Triangle++ mobject, circle, square, triangle = Mobject(), Mobject(), Mobject(), Mobject()+ mobject.add(circle, square)+ circle.add(triangle)+ square.add(triangle)++ expected = [mobject, circle, square, triangle]+ assert mobject.get_family() == expected