From 71e6797e89de246204c2e7ea722c813329eb4f7e Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Sat, 16 Aug 2025 22:58:27 +0200 Subject: [PATCH 1/3] Add type annotations to `transform_matching_parts.py` --- manim/animation/transform.py | 9 ++++++++- manim/animation/transform_matching_parts.py | 20 ++++++++++++-------- mypy.ini | 3 --- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/manim/animation/transform.py b/manim/animation/transform.py index 9719ddf7c1..1eaa91b0fc 100644 --- a/manim/animation/transform.py +++ b/manim/animation/transform.py @@ -835,7 +835,14 @@ def construct(self): """ - def __init__(self, mobject, target_mobject, stretch=True, dim_to_match=1, **kwargs): + def __init__( + self, + mobject: Mobject, + target_mobject: Mobject, + stretch: bool = True, + dim_to_match: int = 1, + **kwargs: Any, + ): self.to_add_on_completion = target_mobject self.stretch = stretch self.dim_to_match = dim_to_match diff --git a/manim/animation/transform_matching_parts.py b/manim/animation/transform_matching_parts.py index 03305201f1..b8ce3e94cc 100644 --- a/manim/animation/transform_matching_parts.py +++ b/manim/animation/transform_matching_parts.py @@ -4,12 +4,13 @@ __all__ = ["TransformMatchingShapes", "TransformMatchingTex"] -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import numpy as np from manim.mobject.opengl.opengl_mobject import OpenGLGroup, OpenGLMobject from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVGroup, OpenGLVMobject +from manim.mobject.text.tex_mobject import SingleStringMathTex from .._config import config from ..constants import RendererType @@ -74,10 +75,10 @@ def __init__( transform_mismatches: bool = False, fade_transform_mismatches: bool = False, key_map: dict | None = None, - **kwargs, + **kwargs: Any, ): if isinstance(mobject, OpenGLVMobject): - group_type = OpenGLVGroup + group_type: type[OpenGLVGroup | OpenGLGroup | VGroup | Group] = OpenGLVGroup elif isinstance(mobject, OpenGLMobject): group_type = OpenGLGroup elif isinstance(mobject, VMobject): @@ -141,7 +142,7 @@ def __init__( self.to_add = target_mobject def get_shape_map(self, mobject: Mobject) -> dict: - shape_map = {} + shape_map: dict[int | str, VGroup | OpenGLVGroup] = {} for sm in self.get_mobject_parts(mobject): key = self.get_mobject_key(sm) if key not in shape_map: @@ -149,6 +150,7 @@ def get_shape_map(self, mobject: Mobject) -> dict: shape_map[key] = OpenGLVGroup() else: shape_map[key] = VGroup() + # error: Argument 1 to "add" of "OpenGLVGroup" has incompatible type "Mobject"; expected "OpenGLVMobject" [arg-type] shape_map[key].add(sm) return shape_map @@ -156,16 +158,17 @@ def clean_up_from_scene(self, scene: Scene) -> None: # Interpolate all animations back to 0 to ensure source mobjects remain unchanged. for anim in self.animations: anim.interpolate(0) + # error: Argument 1 to "remove" of "Scene" has incompatible type "OpenGLMobject"; expected "Mobject" [arg-type] scene.remove(self.mobject) scene.remove(*self.to_remove) scene.add(self.to_add) @staticmethod - def get_mobject_parts(mobject: Mobject): + def get_mobject_parts(mobject: Mobject) -> list[Mobject]: raise NotImplementedError("To be implemented in subclass.") @staticmethod - def get_mobject_key(mobject: Mobject): + def get_mobject_key(mobject: Mobject) -> int | str: raise NotImplementedError("To be implemented in subclass.") @@ -205,7 +208,7 @@ def __init__( transform_mismatches: bool = False, fade_transform_mismatches: bool = False, key_map: dict | None = None, - **kwargs, + **kwargs: Any, ): super().__init__( mobject, @@ -269,7 +272,7 @@ def __init__( transform_mismatches: bool = False, fade_transform_mismatches: bool = False, key_map: dict | None = None, - **kwargs, + **kwargs: Any, ): super().__init__( mobject, @@ -294,4 +297,5 @@ def get_mobject_parts(mobject: Mobject) -> list[Mobject]: @staticmethod def get_mobject_key(mobject: Mobject) -> str: + assert isinstance(mobject, SingleStringMathTex) return mobject.tex_string diff --git a/mypy.ini b/mypy.ini index 4020e1f2e6..5d5be69916 100644 --- a/mypy.ini +++ b/mypy.ini @@ -64,9 +64,6 @@ ignore_errors = True [mypy-manim.animation.speedmodifier] ignore_errors = True -[mypy-manim.animation.transform_matching_parts] -ignore_errors = True - [mypy-manim.animation.transform] ignore_errors = True From 1bfae6149e2991250dd6e83c497e8a24df9ab841 Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Tue, 10 Mar 2026 22:13:25 +0100 Subject: [PATCH 2/3] Make two type errors quiet --- manim/animation/transform_matching_parts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manim/animation/transform_matching_parts.py b/manim/animation/transform_matching_parts.py index b8ce3e94cc..aa6642f581 100644 --- a/manim/animation/transform_matching_parts.py +++ b/manim/animation/transform_matching_parts.py @@ -151,7 +151,7 @@ def get_shape_map(self, mobject: Mobject) -> dict: else: shape_map[key] = VGroup() # error: Argument 1 to "add" of "OpenGLVGroup" has incompatible type "Mobject"; expected "OpenGLVMobject" [arg-type] - shape_map[key].add(sm) + shape_map[key].add(sm) # type: ignore[arg-type] return shape_map def clean_up_from_scene(self, scene: Scene) -> None: @@ -159,7 +159,7 @@ def clean_up_from_scene(self, scene: Scene) -> None: for anim in self.animations: anim.interpolate(0) # error: Argument 1 to "remove" of "Scene" has incompatible type "OpenGLMobject"; expected "Mobject" [arg-type] - scene.remove(self.mobject) + scene.remove(self.mobject) # type: ignore[arg-type] scene.remove(*self.to_remove) scene.add(self.to_add) From 322576a20cdf6061ebcfef7e95ce9a49f8a3ccf9 Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Tue, 10 Mar 2026 22:13:38 +0100 Subject: [PATCH 3/3] Make the pytests pass --- manim/animation/transform_matching_parts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manim/animation/transform_matching_parts.py b/manim/animation/transform_matching_parts.py index aa6642f581..b52ad22f70 100644 --- a/manim/animation/transform_matching_parts.py +++ b/manim/animation/transform_matching_parts.py @@ -10,7 +10,7 @@ from manim.mobject.opengl.opengl_mobject import OpenGLGroup, OpenGLMobject from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVGroup, OpenGLVMobject -from manim.mobject.text.tex_mobject import SingleStringMathTex +from manim.mobject.text.tex_mobject import MathTexPart from .._config import config from ..constants import RendererType @@ -297,5 +297,5 @@ def get_mobject_parts(mobject: Mobject) -> list[Mobject]: @staticmethod def get_mobject_key(mobject: Mobject) -> str: - assert isinstance(mobject, SingleStringMathTex) + assert isinstance(mobject, MathTexPart) return mobject.tex_string