A community-maintained Python framework for creating mathematical animations.
Fixing files with few typing (mypy) errors (#4263)
Henrik Skov Midtiby325fa08a728d
diff --git a/manim/_config/cli_colors.py b/manim/_config/cli_colors.pyindex 5b1d151bd..1eca1ac84 100644--- a/manim/_config/cli_colors.py+++ b/manim/_config/cli_colors.py@@ -9,7 +9,7 @@ __all__ = ["parse_cli_ctx"]def parse_cli_ctx(parser: configparser.SectionProxy) -> dict[str, Any]:- formatter_settings: dict[str, str | int] = {+ formatter_settings: dict[str, str | int | None] = {"indent_increment": int(parser["indent_increment"]),"width": int(parser["width"]),"col1_max_width": int(parser["col1_max_width"]),@@ -37,22 +37,24 @@ def parse_cli_ctx(parser: configparser.SectionProxy) -> dict[str, Any]:if theme is None:formatter = HelpFormatter.settings(theme=HelpTheme(**theme_settings),- **formatter_settings, # type: ignore[arg-type]+ **formatter_settings,)elif theme.lower() == "dark":formatter = HelpFormatter.settings(theme=HelpTheme.dark().with_(**theme_settings),- **formatter_settings, # type: ignore[arg-type]+ **formatter_settings,)elif theme.lower() == "light":formatter = HelpFormatter.settings(theme=HelpTheme.light().with_(**theme_settings),- **formatter_settings, # type: ignore[arg-type]+ **formatter_settings,)- return Context.settings(+ return_val: dict[str, Any] = Context.settings(align_option_groups=parser["align_option_groups"].lower() == "true",align_sections=parser["align_sections"].lower() == "true",show_constraints=True,formatter_settings=formatter,)++ return return_valdiff --git a/manim/animation/changing.py b/manim/animation/changing.pyindex bb11cfc0a..caa4dc194 100644--- a/manim/animation/changing.py+++ b/manim/animation/changing.py@@ -4,8 +4,12 @@ from __future__ import annotations__all__ = ["AnimatedBoundary", "TracedPath"]+from collections.abc import Sequencefrom typing import Callable+from typing_extensions import Any, Self++from manim.mobject.mobject import Mobjectfrom manim.mobject.opengl.opengl_compatibility import ConvertToOpenGLfrom manim.mobject.types.vectorized_mobject import VGroup, VMobjectfrom manim.utils.color import (@@ -16,7 +20,7 @@ from manim.utils.color import (WHITE,ParsableManimColor,)-from manim.utils.rate_functions import smooth+from manim.utils.rate_functions import RateFunction, smoothclass AnimatedBoundary(VGroup):@@ -38,14 +42,14 @@ class AnimatedBoundary(VGroup):def __init__(self,- vmobject,- colors=[BLUE_D, BLUE_B, BLUE_E, GREY_BROWN],- max_stroke_width=3,- cycle_rate=0.5,- back_and_forth=True,- draw_rate_func=smooth,- fade_rate_func=smooth,- **kwargs,+ vmobject: VMobject,+ colors: Sequence[ParsableManimColor] = [BLUE_D, BLUE_B, BLUE_E, GREY_BROWN],+ max_stroke_width: float = 3,+ cycle_rate: float = 0.5,+ back_and_forth: bool = True,+ draw_rate_func: RateFunction = smooth,+ fade_rate_func: RateFunction = smooth,+ **kwargs: Any,):super().__init__(**kwargs)self.colors = colors@@ -59,10 +63,10 @@ class AnimatedBoundary(VGroup):vmobject.copy().set_style(stroke_width=0, fill_opacity=0) for x in range(2)]self.add(*self.boundary_copies)- self.total_time = 0+ self.total_time = 0.0self.add_updater(lambda m, dt: self.update_boundary_copies(dt))- def update_boundary_copies(self, dt):+ def update_boundary_copies(self, dt: float) -> None:# Not actual time, but something which passes at# an altered rate to make the implementation below# cleaner@@ -78,9 +82,9 @@ class AnimatedBoundary(VGroup):fade_alpha = self.fade_rate_func(alpha)if self.back_and_forth and int(time) % 2 == 1:- bounds = (1 - draw_alpha, 1)+ bounds = (1.0 - draw_alpha, 1.0)else:- bounds = (0, draw_alpha)+ bounds = (0.0, draw_alpha)self.full_family_become_partial(growing, vmobject, *bounds)growing.set_stroke(colors[index], width=msw)@@ -90,7 +94,9 @@ class AnimatedBoundary(VGroup):self.total_time += dt- def full_family_become_partial(self, mob1, mob2, a, b):+ def full_family_become_partial(+ self, mob1: VMobject, mob2: VMobject, a: float, b: float+ ) -> Self:family1 = mob1.family_members_with_points()family2 = mob2.family_members_with_points()for sm1, sm2 in zip(family1, family2):@@ -146,20 +152,21 @@ class TracedPath(VMobject, metaclass=ConvertToOpenGL):stroke_width: float = 2,stroke_color: ParsableManimColor | None = WHITE,dissipating_time: float | None = None,- **kwargs,- ):+ **kwargs: Any,+ ) -> None:super().__init__(stroke_color=stroke_color, stroke_width=stroke_width, **kwargs)self.traced_point_func = traced_point_funcself.dissipating_time = dissipating_time- self.time = 1 if self.dissipating_time else None+ self.time = 1.0 if self.dissipating_time else Noneself.add_updater(self.update_path)- def update_path(self, mob, dt):+ def update_path(self, mob: Mobject, dt: float) -> None:new_point = self.traced_point_func()if not self.has_points():self.start_new_path(new_point)self.add_line_to(new_point)if self.dissipating_time:+ assert self.time is not Noneself.time += dtif self.time - 1 > self.dissipating_time:nppcc = self.n_points_per_curvediff --git a/manim/animation/fading.py b/manim/animation/fading.pyindex 79cd41a51..99b9bfe19 100644--- a/manim/animation/fading.py+++ b/manim/animation/fading.py@@ -20,6 +20,7 @@ __all__ = []import numpy as np+from typing_extensions import Anyfrom manim.mobject.opengl.opengl_mobject import OpenGLMobject@@ -53,7 +54,7 @@ class _Fade(Transform):shift: np.ndarray | None = None,target_position: np.ndarray | Mobject | None = None,scale: float = 1,- **kwargs,+ **kwargs: Any,) -> None:if not mobjects:raise ValueError("At least one mobject must be passed.")@@ -85,7 +86,7 @@ class _Fade(Transform):MobjectThe faded, shifted and scaled copy of the mobject."""- faded_mobject = self.mobject.copy()+ faded_mobject: Mobject = self.mobject.copy() # type: ignore[assignment]faded_mobject.fade(1)direction_modifier = -1 if fadeIn and not self.point_target else 1faded_mobject.shift(self.shift_vector * direction_modifier)@@ -131,13 +132,13 @@ class FadeIn(_Fade):"""- def __init__(self, *mobjects: Mobject, **kwargs) -> None:+ def __init__(self, *mobjects: Mobject, **kwargs: Any) -> None:super().__init__(*mobjects, introducer=True, **kwargs)- def create_target(self):- return self.mobject+ def create_target(self) -> Mobject:+ return self.mobject # type: ignore[return-value]- def create_starting_mobject(self):+ def create_starting_mobject(self) -> Mobject:return self._create_faded_mobject(fadeIn=True)@@ -179,12 +180,12 @@ class FadeOut(_Fade):"""- def __init__(self, *mobjects: Mobject, **kwargs) -> None:+ def __init__(self, *mobjects: Mobject, **kwargs: Any) -> None:super().__init__(*mobjects, remover=True, **kwargs)- def create_target(self):+ def create_target(self) -> Mobject:return self._create_faded_mobject(fadeIn=False)- def clean_up_from_scene(self, scene: Scene = None) -> None:+ def clean_up_from_scene(self, scene: Scene) -> None:super().clean_up_from_scene(scene)self.interpolate(0)diff --git a/manim/animation/movement.py b/manim/animation/movement.pyindex b9b185db0..6393d8bee 100644--- a/manim/animation/movement.py+++ b/manim/animation/movement.py@@ -171,7 +171,7 @@ class MoveAlongPath(Animation):self,mobject: Mobject,path: VMobject,- suspend_mobject_updating: bool | None = False,+ suspend_mobject_updating: bool = False,**kwargs,) -> None:self.path = pathdiff --git a/manim/animation/numbers.py b/manim/animation/numbers.pyindex 86bfe7154..05126fd91 100644--- a/manim/animation/numbers.py+++ b/manim/animation/numbers.py@@ -7,6 +7,8 @@ __all__ = ["ChangingDecimal", "ChangeDecimalToValue"]import typing+from typing_extensions import Any+from manim.mobject.text.numbers import DecimalNumberfrom ..animation.animation import Animation@@ -18,8 +20,8 @@ class ChangingDecimal(Animation):self,decimal_mob: DecimalNumber,number_update_func: typing.Callable[[float], float],- suspend_mobject_updating: bool | None = False,- **kwargs,+ suspend_mobject_updating: bool = False,+ **kwargs: Any,) -> None:self.check_validity_of_input(decimal_mob)self.number_update_func = number_update_func@@ -32,12 +34,12 @@ class ChangingDecimal(Animation):raise TypeError("ChangingDecimal can only take in a DecimalNumber")def interpolate_mobject(self, alpha: float) -> None:- self.mobject.set_value(self.number_update_func(self.rate_func(alpha)))+ self.mobject.set_value(self.number_update_func(self.rate_func(alpha))) # type: ignore[attr-defined]class ChangeDecimalToValue(ChangingDecimal):def __init__(- self, decimal_mob: DecimalNumber, target_number: int, **kwargs+ self, decimal_mob: DecimalNumber, target_number: int, **kwargs: Any) -> None:start_number = decimal_mob.numbersuper().__init__(diff --git a/manim/animation/rotation.py b/manim/animation/rotation.pyindex 6292b663f..c90715b09 100644--- a/manim/animation/rotation.py+++ b/manim/animation/rotation.py@@ -8,6 +8,7 @@ from collections.abc import Sequencefrom typing import TYPE_CHECKING, Callableimport numpy as np+from typing_extensions import Anyfrom ..animation.animation import Animationfrom ..animation.transform import Transform@@ -94,7 +95,7 @@ class Rotating(Animation):about_edge: np.ndarray | None = None,run_time: float = 5,rate_func: Callable[[float], float] = linear,- **kwargs,+ **kwargs: Any,) -> None:self.angle = angleself.axis = axis@@ -159,7 +160,7 @@ class Rotate(Transform):axis: np.ndarray = OUT,about_point: Sequence[float] | None = None,about_edge: Sequence[float] | None = None,- **kwargs,+ **kwargs: Any,) -> None:if "path_arc" not in kwargs:kwargs["path_arc"] = anglediff --git a/manim/animation/specialized.py b/manim/animation/specialized.pyindex e5f9e96d9..f39c29fbb 100644--- a/manim/animation/specialized.py+++ b/manim/animation/specialized.py@@ -6,6 +6,7 @@ from collections.abc import Sequencefrom typing import Anyfrom manim.animation.transform import Restore+from manim.mobject.mobject import Mobjectfrom ..constants import *from .composition import LaggedStart@@ -50,7 +51,7 @@ class Broadcast(LaggedStart):def __init__(self,- mobject,+ mobject: Mobject,focal_point: Sequence[float] = ORIGIN,n_mobs: int = 5,initial_opacity: float = 1,diff --git a/manim/animation/updaters/update.py b/manim/animation/updaters/update.pyindex ded160cff..893690af1 100644--- a/manim/animation/updaters/update.py+++ b/manim/animation/updaters/update.py@@ -7,6 +7,9 @@ __all__ = ["UpdateFromFunc", "UpdateFromAlphaFunc", "MaintainPositionRelativeTo"import operator as opimport typing+from typing import Callable++from typing_extensions import Anyfrom manim.animation.animation import Animation@@ -24,9 +27,9 @@ class UpdateFromFunc(Animation):def __init__(self,mobject: Mobject,- update_function: typing.Callable[[Mobject], typing.Any],+ update_function: Callable[[Mobject], Any],suspend_mobject_updating: bool = False,- **kwargs,+ **kwargs: Any,) -> None:self.update_function = update_functionsuper().__init__(@@ -34,16 +37,18 @@ class UpdateFromFunc(Animation):)def interpolate_mobject(self, alpha: float) -> None:- self.update_function(self.mobject)+ self.update_function(self.mobject) # type: ignore[arg-type]class UpdateFromAlphaFunc(UpdateFromFunc):def interpolate_mobject(self, alpha: float) -> None:- self.update_function(self.mobject, self.rate_func(alpha))+ self.update_function(self.mobject, self.rate_func(alpha)) # type: ignore[call-arg, arg-type]class MaintainPositionRelativeTo(Animation):- def __init__(self, mobject: Mobject, tracked_mobject: Mobject, **kwargs) -> None:+ def __init__(+ self, mobject: Mobject, tracked_mobject: Mobject, **kwargs: Any+ ) -> None:self.tracked_mobject = tracked_mobjectself.diff = op.sub(mobject.get_center(),diff --git a/manim/mobject/frame.py b/manim/mobject/frame.pyindex 639e8c384..c721dc7a5 100644--- a/manim/mobject/frame.py+++ b/manim/mobject/frame.py@@ -8,17 +8,21 @@ __all__ = []+from typing_extensions import Any+from manim.mobject.geometry.polygram import Rectanglefrom .. import configclass ScreenRectangle(Rectangle):- def __init__(self, aspect_ratio=16.0 / 9.0, height=4, **kwargs):+ def __init__(+ self, aspect_ratio: float = 16.0 / 9.0, height: float = 4, **kwargs: Any+ ) -> None:super().__init__(width=aspect_ratio * height, height=height, **kwargs)@property- def aspect_ratio(self):+ def aspect_ratio(self) -> float:"""The aspect ratio.When set, the width is stretched to accommodate@@ -27,11 +31,11 @@ class ScreenRectangle(Rectangle):return self.width / self.height@aspect_ratio.setter- def aspect_ratio(self, value):+ def aspect_ratio(self, value: float) -> None:self.stretch_to_fit_width(value * self.height)class FullScreenRectangle(ScreenRectangle):- def __init__(self, **kwargs):+ def __init__(self, **kwargs: Any) -> None:super().__init__(**kwargs)self.height = config["frame_height"]diff --git a/manim/mobject/graphing/scale.py b/manim/mobject/graphing/scale.pyindex 78ffa2308..ceda56f0a 100644--- a/manim/mobject/graphing/scale.py+++ b/manim/mobject/graphing/scale.py@@ -139,18 +139,20 @@ class LogBase(_ScaleBase):def function(self, value: float) -> float:"""Scales the value to fit it to a logarithmic scale.``self.function(5)==10**5``"""- return self.base**value+ return_value: float = self.base**value+ return return_valuedef inverse_function(self, value: float) -> float:"""Inverse of ``function``. The value must be greater than 0"""if isinstance(value, np.ndarray):condition = value.any() <= 0- def func(value, base):- return np.log(value) / np.log(base)+ def func(value: float, base: float) -> float:+ return_value: float = np.log(value) / np.log(base)+ return return_valueelse:condition = value <= 0- func = math.log+ func = math.log # type: ignore[assignment]if condition:raise ValueError(@@ -163,8 +165,8 @@ class LogBase(_ScaleBase):self,val_range: Iterable[float],unit_decimal_places: int = 0,- **base_config: dict[str, Any],- ) -> list[Mobject]:+ **base_config: Any,+ ) -> list[Integer]:"""Produces custom :class:`~.Integer` labels in the form of ``10^2``.Parametersdiff --git a/manim/mobject/logo.py b/manim/mobject/logo.pyindex 6242a4c64..bbeb1fa66 100644--- a/manim/mobject/logo.py+++ b/manim/mobject/logo.py@@ -5,10 +5,13 @@ from __future__ import annotations__all__ = ["ManimBanner"]import svgelements as se+from typing_extensions import Anyfrom manim.animation.updaters.update import UpdateFromAlphaFuncfrom manim.mobject.geometry.arc import Circlefrom manim.mobject.geometry.polygram import Square, Triangle+from manim.mobject.mobject import Mobject+from manim.typing import Vector3Dfrom .. import constants as cstfrom ..animation.animation import override_animation@@ -146,7 +149,7 @@ class ManimBanner(VGroup):m_height_over_anim_height = 0.75748self.font_color = "#ece6e2" if dark_theme else "#343434"- self.scale_factor = 1+ self.scale_factor = 1.0self.M = VMobjectFromSVGPath(MANIM_SVG_PATHS[0]).flip(cst.RIGHT).center()self.M.set(stroke_width=0).scale(@@ -180,7 +183,7 @@ class ManimBanner(VGroup):# and thus not yet added to the submobjects of self.self.anim = anim- def scale(self, scale_factor: float, **kwargs) -> ManimBanner:+ def scale(self, scale_factor: float, **kwargs: Any) -> ManimBanner:"""Scale the banner by the specified scale factor.Parameters@@ -219,7 +222,7 @@ class ManimBanner(VGroup):lag_ratio=0.1,)- def expand(self, run_time: float = 1.5, direction="center") -> Succession:+ def expand(self, run_time: float = 1.5, direction: str = "center") -> Succession:"""An animation that expands Manim's logo into its banner.The returned animation transforms the banner from its initial@@ -277,7 +280,7 @@ class ManimBanner(VGroup):self.M.save_state()left_group = VGroup(self.M, self.anim, m_clone)- def shift(vector):+ def shift(vector: Vector3D) -> None:self.shapes.restore()left_group.align_to(self.M.saved_state, cst.LEFT)if direction == "right":@@ -288,7 +291,7 @@ class ManimBanner(VGroup):elif direction == "left":left_group.shift(-vector)- def slide_and_uncover(mob, alpha):+ def slide_and_uncover(mob: Mobject, alpha: float) -> None:shift(alpha * (m_shape_offset + shape_sliding_overshoot) * cst.RIGHT)# Add letters when they are covered@@ -305,7 +308,7 @@ class ManimBanner(VGroup):mob.shapes.save_state()mob.M.save_state()- def slide_back(mob, alpha):+ def slide_back(mob: Mobject, alpha: float) -> None:if alpha == 0:m_clone.set_opacity(1)m_clone.move_to(mob.anim[-1])diff --git a/manim/mobject/three_d/three_d_utils.py b/manim/mobject/three_d/three_d_utils.pyindex 0a9ccb8a2..997efea33 100644--- a/manim/mobject/three_d/three_d_utils.py+++ b/manim/mobject/three_d/three_d_utils.py@@ -24,35 +24,39 @@ from manim.utils.space_ops import get_unit_normalif TYPE_CHECKING:from manim.typing import Point3D, Vector3D+ from ..types.vectorized_mobject import VMobject-def get_3d_vmob_gradient_start_and_end_points(vmob) -> tuple[Point3D, Point3D]:++def get_3d_vmob_gradient_start_and_end_points(+ vmob: VMobject,+) -> tuple[Point3D, Point3D]:return (get_3d_vmob_start_corner(vmob),get_3d_vmob_end_corner(vmob),)-def get_3d_vmob_start_corner_index(vmob) -> Literal[0]:+def get_3d_vmob_start_corner_index(vmob: VMobject) -> Literal[0]:return 0-def get_3d_vmob_end_corner_index(vmob) -> int:+def get_3d_vmob_end_corner_index(vmob: VMobject) -> int:return ((len(vmob.points) - 1) // 6) * 3-def get_3d_vmob_start_corner(vmob) -> Point3D:+def get_3d_vmob_start_corner(vmob: VMobject) -> Point3D:if vmob.get_num_points() == 0:return np.array(ORIGIN)return vmob.points[get_3d_vmob_start_corner_index(vmob)]-def get_3d_vmob_end_corner(vmob) -> Point3D:+def get_3d_vmob_end_corner(vmob: VMobject) -> Point3D:if vmob.get_num_points() == 0:return np.array(ORIGIN)return vmob.points[get_3d_vmob_end_corner_index(vmob)]-def get_3d_vmob_unit_normal(vmob, point_index: int) -> Vector3D:+def get_3d_vmob_unit_normal(vmob: VMobject, point_index: int) -> Vector3D:n_points = vmob.get_num_points()if len(vmob.get_anchors()) <= 2:return np.array(UP)@@ -68,9 +72,9 @@ def get_3d_vmob_unit_normal(vmob, point_index: int) -> Vector3D:return unit_normal-def get_3d_vmob_start_corner_unit_normal(vmob) -> Vector3D:+def get_3d_vmob_start_corner_unit_normal(vmob: VMobject) -> Vector3D:return get_3d_vmob_unit_normal(vmob, get_3d_vmob_start_corner_index(vmob))-def get_3d_vmob_end_corner_unit_normal(vmob) -> Vector3D:+def get_3d_vmob_end_corner_unit_normal(vmob: VMobject) -> Vector3D:return get_3d_vmob_unit_normal(vmob, get_3d_vmob_end_corner_index(vmob))diff --git a/manim/scene/moving_camera_scene.py b/manim/scene/moving_camera_scene.pyindex eafc992ef..686686f07 100644--- a/manim/scene/moving_camera_scene.py+++ b/manim/scene/moving_camera_scene.py@@ -89,7 +89,10 @@ from __future__ import annotations__all__ = ["MovingCameraScene"]+from typing_extensions import Any+from manim.animation.animation import Animation+from manim.mobject.mobject import Mobjectfrom ..camera.moving_camera import MovingCamerafrom ..scene.scene import Scene@@ -111,10 +114,12 @@ class MovingCameraScene(Scene)::class:`.MovingCamera`"""- def __init__(self, camera_class=MovingCamera, **kwargs):+ def __init__(+ self, camera_class: type[MovingCamera] = MovingCamera, **kwargs: Any+ ) -> None:super().__init__(camera_class=camera_class, **kwargs)- def get_moving_mobjects(self, *animations: Animation):+ def get_moving_mobjects(self, *animations: Animation) -> list[Mobject]:"""This method returns a list of all of the Mobjects in the Scene thatare moving, that are also in the animations passed.diff --git a/manim/scene/scene.py b/manim/scene/scene.pyindex fc3d3ede5..de4a1d11c 100644--- a/manim/scene/scene.py+++ b/manim/scene/scene.py@@ -829,7 +829,7 @@ class Scene:self.foreground_mobjects = []return self- def get_moving_mobjects(self, *animations: Animation):+ def get_moving_mobjects(self, *animations: Animation) -> list[Mobject]:"""Gets all moving mobjects in the passed animation(s).diff --git a/manim/scene/section.py b/manim/scene/section.pyindex af005b52d..728104f32 100644--- a/manim/scene/section.py+++ b/manim/scene/section.py@@ -100,5 +100,5 @@ class Section:**video_metadata,)- def __repr__(self):+ def __repr__(self) -> str:return f"<Section '{self.name}' stored in '{self.video}'>"diff --git a/mypy.ini b/mypy.iniindex 4b5f71850..1d2340ca3 100644--- a/mypy.ini+++ b/mypy.ini@@ -52,12 +52,27 @@ warn_return_any = Trueignore_errors = Truedisable_error_code = return-value+[mypy-manim._config.cli_colors]+ignore_errors = False+[mypy-manim._config.logger_utils]ignore_errors = False[mypy-manim.animation.*]ignore_errors = True+[mypy-manim.animation.changing]+ignore_errors = False++[mypy-manim.animation.fading]+ignore_errors = False++[mypy-manim.animation.numbers]+ignore_errors = False++[mypy-manim.animation.updaters.update]+ignore_errors = False+[mypy-manim.camera.*]ignore_errors = True@@ -73,18 +88,33 @@ ignore_errors = True[mypy-manim.mobject.*]ignore_errors = True-[mypy-manim.mobject.text.code_mobject]+[mypy-manim.mobject.frame]ignore_errors = False[mypy-manim.mobject.geometry.*]ignore_errors = True+[mypy-manim.mobject.graphing.scale]+ignore_errors = False++[mypy-manim.mobject.text.code_mobject]+ignore_errors = False++[mypy-manim.mobject.three_d.three_d_utils]+ignore_errors = False+[mypy-manim.renderer.*]ignore_errors = True[mypy-manim.scene.*]ignore_errors = True+[mypy-manim.scene.moving_camera_scene]+ignore_errors = False++[mypy-manim.scene.section]+ignore_errors = False+[mypy-manim.utils.hashing.*]ignore_errors = True