Skip to content

Commit d0521fa

Browse files
henrikmidtibypre-commit-ci[bot]chopan050
authored
Add type annotations to movement.py (#4371)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Francisco Manríquez Novoa <[email protected]>
1 parent e339a68 commit d0521fa

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

manim/animation/movement.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
from ..utils.rate_functions import linear
1919

2020
if TYPE_CHECKING:
21-
from ..mobject.mobject import Mobject, VMobject
21+
from typing_extensions import Self
22+
23+
from manim.mobject.types.vectorized_mobject import VMobject
24+
from manim.typing import MappingFunction, Point3D
25+
from manim.utils.rate_functions import RateFunction
26+
27+
from ..mobject.mobject import Mobject
2228

2329

2430
class Homotopy(Animation):
@@ -72,27 +78,33 @@ def __init__(
7278
mobject: Mobject,
7379
run_time: float = 3,
7480
apply_function_kwargs: dict[str, Any] | None = None,
75-
**kwargs,
76-
) -> None:
81+
**kwargs: Any,
82+
):
7783
self.homotopy = homotopy
7884
self.apply_function_kwargs = (
7985
apply_function_kwargs if apply_function_kwargs is not None else {}
8086
)
8187
super().__init__(mobject, run_time=run_time, **kwargs)
8288

83-
def function_at_time_t(self, t: float) -> tuple[float, float, float]:
84-
return lambda p: self.homotopy(*p, t)
89+
def function_at_time_t(self, t: float) -> MappingFunction:
90+
def mapping_function(p: Point3D) -> Point3D:
91+
x, y, z = p
92+
return np.array(self.homotopy(x, y, z, t))
93+
94+
return mapping_function
8595

8696
def interpolate_submobject(
8797
self,
8898
submobject: Mobject,
8999
starting_submobject: Mobject,
90100
alpha: float,
91-
) -> None:
101+
) -> Self:
92102
submobject.points = starting_submobject.points
93103
submobject.apply_function(
94-
self.function_at_time_t(alpha), **self.apply_function_kwargs
104+
self.function_at_time_t(alpha),
105+
**self.apply_function_kwargs,
95106
)
107+
return self
96108

97109

98110
class SmoothedVectorizedHomotopy(Homotopy):
@@ -101,15 +113,20 @@ def interpolate_submobject(
101113
submobject: Mobject,
102114
starting_submobject: Mobject,
103115
alpha: float,
104-
) -> None:
116+
) -> Self:
117+
assert isinstance(submobject, VMobject)
105118
super().interpolate_submobject(submobject, starting_submobject, alpha)
106119
submobject.make_smooth()
120+
return self
107121

108122

109123
class ComplexHomotopy(Homotopy):
110124
def __init__(
111-
self, complex_homotopy: Callable[[complex], float], mobject: Mobject, **kwargs
112-
) -> None:
125+
self,
126+
complex_homotopy: Callable[[complex, float], float],
127+
mobject: Mobject,
128+
**kwargs: Any,
129+
):
113130
"""Complex Homotopy a function Cx[0, 1] to C"""
114131

115132
def homotopy(
@@ -131,9 +148,9 @@ def __init__(
131148
mobject: Mobject,
132149
virtual_time: float = 1,
133150
suspend_mobject_updating: bool = False,
134-
rate_func: Callable[[float], float] = linear,
135-
**kwargs,
136-
) -> None:
151+
rate_func: RateFunction = linear,
152+
**kwargs: Any,
153+
):
137154
self.virtual_time = virtual_time
138155
self.function = function
139156
super().__init__(
@@ -149,7 +166,7 @@ def interpolate_mobject(self, alpha: float) -> None:
149166
self.rate_func(alpha) - self.rate_func(self.last_alpha)
150167
)
151168
self.mobject.apply_function(lambda p: p + dt * self.function(p))
152-
self.last_alpha = alpha
169+
self.last_alpha: float = alpha
153170

154171

155172
class MoveAlongPath(Animation):
@@ -172,8 +189,8 @@ def __init__(
172189
mobject: Mobject,
173190
path: VMobject,
174191
suspend_mobject_updating: bool = False,
175-
**kwargs,
176-
) -> None:
192+
**kwargs: Any,
193+
):
177194
self.path = path
178195
super().__init__(
179196
mobject, suspend_mobject_updating=suspend_mobject_updating, **kwargs

mypy.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ ignore_errors = True
5757
[mypy-manim.animation.creation]
5858
ignore_errors = True
5959

60-
[mypy-manim.animation.movement]
61-
ignore_errors = True
62-
6360
[mypy-manim.animation.rotation]
6461
ignore_errors = True
6562

0 commit comments

Comments
 (0)