Skip to content

Commit 047db45

Browse files
henrikmidtibypre-commit-ci[bot]chopan050
authored
Add type annotations to mobject/svg/brace.py (#4309)
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 2f02eb9 commit 047db45

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

manim/mobject/svg/brace.py

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44

55
__all__ = ["Brace", "BraceLabel", "ArcBrace", "BraceText", "BraceBetweenPoints"]
66

7-
from collections.abc import Sequence
8-
from typing import TYPE_CHECKING
7+
from typing import TYPE_CHECKING, Any
98

109
import numpy as np
1110
import svgelements as se
11+
from typing_extensions import Self
1212

1313
from manim._config import config
1414
from manim.mobject.geometry.arc import Arc
1515
from manim.mobject.geometry.line import Line
1616
from manim.mobject.mobject import Mobject
1717
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
18-
from manim.mobject.text.tex_mobject import MathTex, Tex
18+
from manim.mobject.text.tex_mobject import MathTex, SingleStringMathTex, Tex
19+
from manim.mobject.text.text_mobject import Text
1920

21+
from ...animation.animation import Animation
2022
from ...animation.composition import AnimationGroup
2123
from ...animation.fading import FadeIn
2224
from ...animation.growing import GrowFromCenter
@@ -26,11 +28,9 @@
2628
from ..svg.svg_mobject import VMobjectFromSVGPath
2729

2830
if TYPE_CHECKING:
29-
from manim.typing import Point3DLike, Vector3D
31+
from manim.typing import Point3D, Point3DLike, Vector3D
3032
from manim.utils.color.core import ParsableManimColor
3133

32-
__all__ = ["Brace", "BraceBetweenPoints", "BraceLabel", "ArcBrace"]
33-
3434

3535
class Brace(VMobjectFromSVGPath):
3636
"""Takes a mobject and draws a brace adjacent to it.
@@ -70,14 +70,14 @@ def construct(self):
7070
def __init__(
7171
self,
7272
mobject: Mobject,
73-
direction: Vector3D | None = DOWN,
73+
direction: Point3DLike = DOWN,
7474
buff: float = 0.2,
7575
sharpness: float = 2,
7676
stroke_width: float = 0,
7777
fill_opacity: float = 1.0,
7878
background_stroke_width: float = 0,
7979
background_stroke_color: ParsableManimColor = BLACK,
80-
**kwargs,
80+
**kwargs: Any,
8181
):
8282
path_string_template = (
8383
"m0.01216 0c-0.01152 0-0.01216 6.103e-4 -0.01216 0.01311v0.007762c0.06776 "
@@ -130,7 +130,7 @@ def __init__(
130130
for mob in mobject, self:
131131
mob.rotate(angle, about_point=ORIGIN)
132132

133-
def put_at_tip(self, mob: Mobject, use_next_to: bool = True, **kwargs):
133+
def put_at_tip(self, mob: Mobject, use_next_to: bool = True, **kwargs: Any) -> Self:
134134
"""Puts the given mobject at the brace tip.
135135
136136
Parameters
@@ -153,7 +153,7 @@ def put_at_tip(self, mob: Mobject, use_next_to: bool = True, **kwargs):
153153
mob.shift(self.get_direction() * shift_distance)
154154
return self
155155

156-
def get_text(self, *text, **kwargs):
156+
def get_text(self, *text: str, **kwargs: Any) -> Tex:
157157
"""Places the text at the brace tip.
158158
159159
Parameters
@@ -172,7 +172,7 @@ def get_text(self, *text, **kwargs):
172172
self.put_at_tip(text_mob, **kwargs)
173173
return text_mob
174174

175-
def get_tex(self, *tex, **kwargs):
175+
def get_tex(self, *tex: str, **kwargs: Any) -> MathTex:
176176
"""Places the tex at the brace tip.
177177
178178
Parameters
@@ -191,15 +191,15 @@ def get_tex(self, *tex, **kwargs):
191191
self.put_at_tip(tex_mob, **kwargs)
192192
return tex_mob
193193

194-
def get_tip(self):
194+
def get_tip(self) -> Point3D:
195195
"""Returns the point at the brace tip."""
196196
# Returns the position of the seventh point in the path, which is the tip.
197197
if config["renderer"] == "opengl":
198198
return self.points[34]
199199

200200
return self.points[28] # = 7*4
201201

202-
def get_direction(self):
202+
def get_direction(self) -> Vector3D:
203203
"""Returns the direction from the center to the brace tip."""
204204
vect = self.get_tip() - self.get_center()
205205
return vect / np.linalg.norm(vect)
@@ -233,12 +233,12 @@ def __init__(
233233
self,
234234
obj: Mobject,
235235
text: str,
236-
brace_direction: np.ndarray = DOWN,
237-
label_constructor: type = MathTex,
236+
brace_direction: Point3DLike = DOWN,
237+
label_constructor: type[SingleStringMathTex | Text] = MathTex,
238238
font_size: float = DEFAULT_FONT_SIZE,
239239
buff: float = 0.2,
240-
brace_config: dict | None = None,
241-
**kwargs,
240+
brace_config: dict[str, Any] | None = None,
241+
**kwargs: Any,
242242
):
243243
self.label_constructor = label_constructor
244244
super().__init__(**kwargs)
@@ -249,37 +249,48 @@ def __init__(
249249
self.brace = Brace(obj, brace_direction, buff, **brace_config)
250250

251251
if isinstance(text, (tuple, list)):
252-
self.label = self.label_constructor(*text, font_size=font_size, **kwargs)
252+
self.label: VMobject = self.label_constructor(
253+
*text, font_size=font_size, **kwargs
254+
)
253255
else:
254256
self.label = self.label_constructor(str(text), font_size=font_size)
255257

256258
self.brace.put_at_tip(self.label)
257259
self.add(self.brace, self.label)
258260

259-
def creation_anim(self, label_anim=FadeIn, brace_anim=GrowFromCenter):
261+
def creation_anim(
262+
self,
263+
label_anim: type[Animation] = FadeIn,
264+
brace_anim: type[Animation] = GrowFromCenter,
265+
) -> AnimationGroup:
260266
return AnimationGroup(brace_anim(self.brace), label_anim(self.label))
261267

262-
def shift_brace(self, obj, **kwargs):
268+
def shift_brace(self, obj: Mobject, **kwargs: Any) -> Self:
263269
if isinstance(obj, list):
264270
obj = self.get_group_class()(*obj)
265271
self.brace = Brace(obj, self.brace_direction, **kwargs)
266272
self.brace.put_at_tip(self.label)
267273
return self
268274

269-
def change_label(self, *text, **kwargs):
270-
self.label = self.label_constructor(*text, **kwargs)
271-
275+
def change_label(self, *text: str, **kwargs: Any) -> Self:
276+
self.label = self.label_constructor(*text, **kwargs) # type: ignore[arg-type]
272277
self.brace.put_at_tip(self.label)
273278
return self
274279

275-
def change_brace_label(self, obj, *text, **kwargs):
280+
def change_brace_label(self, obj: Mobject, *text: str, **kwargs: Any) -> Self:
276281
self.shift_brace(obj)
277282
self.change_label(*text, **kwargs)
278283
return self
279284

280285

281286
class BraceText(BraceLabel):
282-
def __init__(self, obj, text, label_constructor=Tex, **kwargs):
287+
def __init__(
288+
self,
289+
obj: Mobject,
290+
text: str,
291+
label_constructor: type[SingleStringMathTex | Text] = Text,
292+
**kwargs: Any,
293+
):
283294
super().__init__(obj, text, label_constructor=label_constructor, **kwargs)
284295

285296

@@ -317,10 +328,10 @@ def construct(self):
317328

318329
def __init__(
319330
self,
320-
point_1: Point3DLike | None,
321-
point_2: Point3DLike | None,
322-
direction: Vector3D | None = ORIGIN,
323-
**kwargs,
331+
point_1: Point3DLike,
332+
point_2: Point3DLike,
333+
direction: Point3DLike = ORIGIN,
334+
**kwargs: Any,
324335
):
325336
if all(direction == ORIGIN):
326337
line_vector = np.array(point_2) - np.array(point_1)
@@ -386,8 +397,8 @@ def construct(self):
386397
def __init__(
387398
self,
388399
arc: Arc | None = None,
389-
direction: Sequence[float] = RIGHT,
390-
**kwargs,
400+
direction: Point3DLike = RIGHT,
401+
**kwargs: Any,
391402
):
392403
if arc is None:
393404
arc = Arc(start_angle=-1, angle=2, radius=1)

mypy.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ ignore_errors = True
147147
[mypy-manim.mobject.opengl.opengl_vectorized_mobject]
148148
ignore_errors = True
149149

150-
[mypy-manim.mobject.svg.brace]
151-
ignore_errors = True
152-
153150
[mypy-manim.mobject.table]
154151
ignore_errors = True
155152

0 commit comments

Comments
 (0)