From 8e25ad7bb969c019d9bc1891b6d0203ceba1f852 Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Mon, 23 Jun 2025 08:42:28 +0200 Subject: [PATCH 1/8] Added type annotations to mobject/svg/brace.py --- manim/mobject/svg/brace.py | 52 ++++++++++++++++++++----------- manim/mobject/text/tex_mobject.py | 7 ++++- mypy.ini | 3 -- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py index 3d826f4f01..95c45d55ee 100644 --- a/manim/mobject/svg/brace.py +++ b/manim/mobject/svg/brace.py @@ -5,10 +5,11 @@ __all__ = ["Brace", "BraceLabel", "ArcBrace", "BraceText", "BraceBetweenPoints"] from collections.abc import Sequence -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import numpy as np import svgelements as se +from typing_extensions import Self from manim._config import config from manim.mobject.geometry.arc import Arc @@ -17,6 +18,7 @@ from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL from manim.mobject.text.tex_mobject import MathTex, Tex +from ...animation.animation import Animation from ...animation.composition import AnimationGroup from ...animation.fading import FadeIn from ...animation.growing import GrowFromCenter @@ -26,7 +28,7 @@ from ..svg.svg_mobject import VMobjectFromSVGPath if TYPE_CHECKING: - from manim.typing import Point3DLike, Vector3D + from manim.typing import Point3D, Point3DLike, Vector3D from manim.utils.color.core import ParsableManimColor __all__ = ["Brace", "BraceBetweenPoints", "BraceLabel", "ArcBrace"] @@ -70,14 +72,14 @@ def construct(self): def __init__( self, mobject: Mobject, - direction: Vector3D | None = DOWN, + direction: Vector3D = DOWN, buff: float = 0.2, sharpness: float = 2, stroke_width: float = 0, fill_opacity: float = 1.0, background_stroke_width: float = 0, background_stroke_color: ParsableManimColor = BLACK, - **kwargs, + **kwargs: Any, ): path_string_template = ( "m0.01216 0c-0.01152 0-0.01216 6.103e-4 -0.01216 0.01311v0.007762c0.06776 " @@ -130,7 +132,7 @@ def __init__( for mob in mobject, self: mob.rotate(angle, about_point=ORIGIN) - def put_at_tip(self, mob: Mobject, use_next_to: bool = True, **kwargs): + def put_at_tip(self, mob: Mobject, use_next_to: bool = True, **kwargs: Any) -> Self: """Puts the given mobject at the brace tip. Parameters @@ -153,7 +155,7 @@ def put_at_tip(self, mob: Mobject, use_next_to: bool = True, **kwargs): mob.shift(self.get_direction() * shift_distance) return self - def get_text(self, *text, **kwargs): + def get_text(self, *text: str, **kwargs: Any) -> Tex: """Places the text at the brace tip. Parameters @@ -172,7 +174,7 @@ def get_text(self, *text, **kwargs): self.put_at_tip(text_mob, **kwargs) return text_mob - def get_tex(self, *tex, **kwargs): + def get_tex(self, *tex: str, **kwargs: Any) -> MathTex: """Places the tex at the brace tip. Parameters @@ -191,7 +193,7 @@ def get_tex(self, *tex, **kwargs): self.put_at_tip(tex_mob, **kwargs) return tex_mob - def get_tip(self): + def get_tip(self) -> Point3D: """Returns the point at the brace tip.""" # Returns the position of the seventh point in the path, which is the tip. if config["renderer"] == "opengl": @@ -199,7 +201,7 @@ def get_tip(self): return self.points[28] # = 7*4 - def get_direction(self): + def get_direction(self) -> Vector3D: """Returns the direction from the center to the brace tip.""" vect = self.get_tip() - self.get_center() return vect / np.linalg.norm(vect) @@ -238,7 +240,7 @@ def __init__( font_size: float = DEFAULT_FONT_SIZE, buff: float = 0.2, brace_config: dict | None = None, - **kwargs, + **kwargs: Any, ): self.label_constructor = label_constructor super().__init__(**kwargs) @@ -249,37 +251,51 @@ def __init__( self.brace = Brace(obj, brace_direction, buff, **brace_config) if isinstance(text, (tuple, list)): - self.label = self.label_constructor(*text, font_size=font_size, **kwargs) + self.label: VMobject = self.label_constructor( + *text, font_size=font_size, **kwargs + ) else: self.label = self.label_constructor(str(text), font_size=font_size) self.brace.put_at_tip(self.label) self.add(self.brace, self.label) - def creation_anim(self, label_anim=FadeIn, brace_anim=GrowFromCenter): + def creation_anim( + self, + label_anim: type[Animation] = FadeIn, + brace_anim: type[Animation] = GrowFromCenter, + ) -> AnimationGroup: return AnimationGroup(brace_anim(self.brace), label_anim(self.label)) - def shift_brace(self, obj, **kwargs): + # TODO: This method is only called from the method change_brace_label, which is + # not called from any location in the current codebase. + # TODO: The Mobject could be more specific. + def shift_brace(self, obj: Mobject, **kwargs: Any) -> Self: if isinstance(obj, list): obj = self.get_group_class()(*obj) self.brace = Brace(obj, self.brace_direction, **kwargs) self.brace.put_at_tip(self.label) return self - def change_label(self, *text, **kwargs): + # TODO: This method is only called from the method change_brace_label, which is + # not called from any location in the current codebase. + def change_label(self, *text: str, **kwargs: Any) -> Self: self.label = self.label_constructor(*text, **kwargs) self.brace.put_at_tip(self.label) return self - def change_brace_label(self, obj, *text, **kwargs): + # TODO: This method is not called from anywhere in the codebase. + def change_brace_label(self, obj: Mobject, *text: str, **kwargs: Any) -> Self: self.shift_brace(obj) self.change_label(*text, **kwargs) return self class BraceText(BraceLabel): - def __init__(self, obj, text, label_constructor=Tex, **kwargs): + def __init__( + self, obj: Mobject, text: str, label_constructor: type[Tex] = Tex, **kwargs: Any + ): super().__init__(obj, text, label_constructor=label_constructor, **kwargs) @@ -320,7 +336,7 @@ def __init__( point_1: Point3DLike | None, point_2: Point3DLike | None, direction: Vector3D | None = ORIGIN, - **kwargs, + **kwargs: Any, ): if all(direction == ORIGIN): line_vector = np.array(point_2) - np.array(point_1) @@ -387,7 +403,7 @@ def __init__( self, arc: Arc | None = None, direction: Sequence[float] = RIGHT, - **kwargs, + **kwargs: Any, ): if arc is None: arc = Arc(start_angle=-1, angle=2, radius=1) diff --git a/manim/mobject/text/tex_mobject.py b/manim/mobject/text/tex_mobject.py index 26334a60d9..b5a7df4e20 100644 --- a/manim/mobject/text/tex_mobject.py +++ b/manim/mobject/text/tex_mobject.py @@ -29,6 +29,7 @@ from collections.abc import Iterable from functools import reduce from textwrap import dedent +from typing import Any from manim import config, logger from manim.constants import * @@ -447,7 +448,11 @@ class Tex(MathTex): """ def __init__( - self, *tex_strings, arg_separator="", tex_environment="center", **kwargs + self, + *tex_strings: str, + arg_separator: str = "", + tex_environment: str = "center", + **kwargs: Any, ): super().__init__( *tex_strings, diff --git a/mypy.ini b/mypy.ini index 07d93b4e47..88d98bde84 100644 --- a/mypy.ini +++ b/mypy.ini @@ -153,9 +153,6 @@ ignore_errors = True [mypy-manim.mobject.opengl.opengl_vectorized_mobject] ignore_errors = True -[mypy-manim.mobject.svg.brace] -ignore_errors = True - [mypy-manim.mobject.svg.svg_mobject] ignore_errors = True From f2e3d04a180f86645e2f91ca2ccfb320cb70d8ea Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Sat, 28 Jun 2025 22:00:48 +0200 Subject: [PATCH 2/8] Do not overwrite the __all__ variable defined earlier in the file. --- manim/mobject/svg/brace.py | 1 - 1 file changed, 1 deletion(-) diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py index 95c45d55ee..f4931fb68e 100644 --- a/manim/mobject/svg/brace.py +++ b/manim/mobject/svg/brace.py @@ -31,7 +31,6 @@ from manim.typing import Point3D, Point3DLike, Vector3D from manim.utils.color.core import ParsableManimColor -__all__ = ["Brace", "BraceBetweenPoints", "BraceLabel", "ArcBrace"] class Brace(VMobjectFromSVGPath): From d65a1afdc39d33026f0975c314103cf46a77f4fe Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Sat, 28 Jun 2025 22:02:00 +0200 Subject: [PATCH 3/8] Implemented suggestions from chopan50 --- manim/mobject/svg/brace.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py index f4931fb68e..a22145a36c 100644 --- a/manim/mobject/svg/brace.py +++ b/manim/mobject/svg/brace.py @@ -71,7 +71,7 @@ def construct(self): def __init__( self, mobject: Mobject, - direction: Vector3D = DOWN, + direction: Point3DLike = DOWN, buff: float = 0.2, sharpness: float = 2, stroke_width: float = 0, @@ -234,11 +234,11 @@ def __init__( self, obj: Mobject, text: str, - brace_direction: np.ndarray = DOWN, + brace_direction: Point3DLike = DOWN, label_constructor: type = MathTex, font_size: float = DEFAULT_FONT_SIZE, buff: float = 0.2, - brace_config: dict | None = None, + brace_config: dict[str, Any] | None = None, **kwargs: Any, ): self.label_constructor = label_constructor @@ -334,7 +334,7 @@ def __init__( self, point_1: Point3DLike | None, point_2: Point3DLike | None, - direction: Vector3D | None = ORIGIN, + direction: Point3DLike = ORIGIN, **kwargs: Any, ): if all(direction == ORIGIN): @@ -401,7 +401,7 @@ def construct(self): def __init__( self, arc: Arc | None = None, - direction: Sequence[float] = RIGHT, + direction: Point3DLike = RIGHT, **kwargs: Any, ): if arc is None: From 606872880421e8f0763b94a3a06a0c67a656cf4c Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Sat, 28 Jun 2025 22:04:22 +0200 Subject: [PATCH 4/8] Code cleanup. --- manim/mobject/svg/brace.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py index a22145a36c..ba8248a79e 100644 --- a/manim/mobject/svg/brace.py +++ b/manim/mobject/svg/brace.py @@ -266,9 +266,6 @@ def creation_anim( ) -> AnimationGroup: return AnimationGroup(brace_anim(self.brace), label_anim(self.label)) - # TODO: This method is only called from the method change_brace_label, which is - # not called from any location in the current codebase. - # TODO: The Mobject could be more specific. def shift_brace(self, obj: Mobject, **kwargs: Any) -> Self: if isinstance(obj, list): obj = self.get_group_class()(*obj) @@ -276,15 +273,11 @@ def shift_brace(self, obj: Mobject, **kwargs: Any) -> Self: self.brace.put_at_tip(self.label) return self - # TODO: This method is only called from the method change_brace_label, which is - # not called from any location in the current codebase. def change_label(self, *text: str, **kwargs: Any) -> Self: self.label = self.label_constructor(*text, **kwargs) - self.brace.put_at_tip(self.label) return self - # TODO: This method is not called from anywhere in the codebase. def change_brace_label(self, obj: Mobject, *text: str, **kwargs: Any) -> Self: self.shift_brace(obj) self.change_label(*text, **kwargs) From 73f2de28ecb01dbe09a9d9c3d5927e454ece235e Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Sat, 28 Jun 2025 22:04:47 +0200 Subject: [PATCH 5/8] Implemented suggestion from chopan50 --- manim/mobject/svg/brace.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py index ba8248a79e..b31c9c6576 100644 --- a/manim/mobject/svg/brace.py +++ b/manim/mobject/svg/brace.py @@ -16,7 +16,8 @@ from manim.mobject.geometry.line import Line from manim.mobject.mobject import Mobject from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL -from manim.mobject.text.tex_mobject import MathTex, Tex +from manim.mobject.text.tex_mobject import MathTex, Tex, SingleStringMathTex +from manim.mobject.text.text_mobject import Text from ...animation.animation import Animation from ...animation.composition import AnimationGroup @@ -235,7 +236,7 @@ def __init__( obj: Mobject, text: str, brace_direction: Point3DLike = DOWN, - label_constructor: type = MathTex, + label_constructor: type[SingleStringMathTex | Text] = MathTex, font_size: float = DEFAULT_FONT_SIZE, buff: float = 0.2, brace_config: dict[str, Any] | None = None, @@ -286,7 +287,7 @@ def change_brace_label(self, obj: Mobject, *text: str, **kwargs: Any) -> Self: class BraceText(BraceLabel): def __init__( - self, obj: Mobject, text: str, label_constructor: type[Tex] = Tex, **kwargs: Any + self, obj: Mobject, text: str, label_constructor: type[SingleStringMathTex | Text] = Text, **kwargs: Any ): super().__init__(obj, text, label_constructor=label_constructor, **kwargs) From f5765220b705b8e791ba16ee0ed1c6ec52b3c40e Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Sat, 28 Jun 2025 22:16:36 +0200 Subject: [PATCH 6/8] Another suggestion from chopan50 --- manim/mobject/svg/brace.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py index b31c9c6576..d0cfadf269 100644 --- a/manim/mobject/svg/brace.py +++ b/manim/mobject/svg/brace.py @@ -326,8 +326,8 @@ def construct(self): def __init__( self, - point_1: Point3DLike | None, - point_2: Point3DLike | None, + point_1: Point3DLike, + point_2: Point3DLike, direction: Point3DLike = ORIGIN, **kwargs: Any, ): From 195aaa073b4eb5b29fc4270cd30e82f4bc03e749 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Jun 2025 20:17:09 +0000 Subject: [PATCH 7/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/mobject/svg/brace.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py index d0cfadf269..8c407ba981 100644 --- a/manim/mobject/svg/brace.py +++ b/manim/mobject/svg/brace.py @@ -4,7 +4,6 @@ __all__ = ["Brace", "BraceLabel", "ArcBrace", "BraceText", "BraceBetweenPoints"] -from collections.abc import Sequence from typing import TYPE_CHECKING, Any import numpy as np @@ -16,7 +15,7 @@ from manim.mobject.geometry.line import Line from manim.mobject.mobject import Mobject from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL -from manim.mobject.text.tex_mobject import MathTex, Tex, SingleStringMathTex +from manim.mobject.text.tex_mobject import MathTex, SingleStringMathTex, Tex from manim.mobject.text.text_mobject import Text from ...animation.animation import Animation @@ -33,7 +32,6 @@ from manim.utils.color.core import ParsableManimColor - class Brace(VMobjectFromSVGPath): """Takes a mobject and draws a brace adjacent to it. @@ -287,7 +285,11 @@ def change_brace_label(self, obj: Mobject, *text: str, **kwargs: Any) -> Self: class BraceText(BraceLabel): def __init__( - self, obj: Mobject, text: str, label_constructor: type[SingleStringMathTex | Text] = Text, **kwargs: Any + self, + obj: Mobject, + text: str, + label_constructor: type[SingleStringMathTex | Text] = Text, + **kwargs: Any, ): super().__init__(obj, text, label_constructor=label_constructor, **kwargs) From 0ad09db1443972b8944d681a1657fd001deccefa Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Sat, 28 Jun 2025 22:22:16 +0200 Subject: [PATCH 8/8] Ignore single type error. --- manim/mobject/svg/brace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py index 8c407ba981..2595005d08 100644 --- a/manim/mobject/svg/brace.py +++ b/manim/mobject/svg/brace.py @@ -273,7 +273,7 @@ def shift_brace(self, obj: Mobject, **kwargs: Any) -> Self: return self def change_label(self, *text: str, **kwargs: Any) -> Self: - self.label = self.label_constructor(*text, **kwargs) + self.label = self.label_constructor(*text, **kwargs) # type: ignore[arg-type] self.brace.put_at_tip(self.label) return self