Skip to content

Commit 03a9414

Browse files
Add type annotations to text_mobject.py (#4381)
Co-authored-by: Benjamin Hackl <[email protected]>
1 parent 5a2b338 commit 03a9414

File tree

8 files changed

+124
-130
lines changed

8 files changed

+124
-130
lines changed

manim/mobject/mobject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2357,7 +2357,7 @@ def __getitem__(self, value):
23572357
def __iter__(self):
23582358
return iter(self.split())
23592359

2360-
def __len__(self):
2360+
def __len__(self) -> int:
23612361
return len(self.split())
23622362

23632363
def get_group_class(self) -> type[Group]:

manim/mobject/svg/svg_mobject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import svgelements as se
1212

1313
from manim import config, logger
14-
from manim.utils.color import ParsableManimColor
14+
from manim.utils.color import ManimColor, ParsableManimColor
1515

1616
from ...constants import RIGHT
1717
from ...utils.bezier import get_quadratic_approximation_of_cubic
@@ -120,7 +120,7 @@ def __init__(
120120
self.should_center = should_center
121121
self.svg_height = height
122122
self.svg_width = width
123-
self.color = color
123+
self.color = ManimColor(color)
124124
self.opacity = opacity
125125
self.fill_color = fill_color
126126
self.fill_opacity = fill_opacity # type: ignore[assignment]

manim/mobject/text/code_mobject.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from manim.mobject.geometry.arc import Dot
2020
from manim.mobject.geometry.shape_matchers import SurroundingRectangle
2121
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
22-
from manim.mobject.text.text_mobject import Paragraph
2322
from manim.mobject.types.vectorized_mobject import VGroup, VMobject
2423
from manim.typing import StrPath
2524
from manim.utils.color import WHITE, ManimColor
@@ -119,6 +118,7 @@ def construct(self):
119118
"line_spacing": 0.5,
120119
"disable_ligatures": True,
121120
}
121+
code: VMobject
122122

123123
def __init__(
124124
self,
@@ -200,6 +200,8 @@ def __init__(
200200
base_paragraph_config = self.default_paragraph_config.copy()
201201
base_paragraph_config.update(paragraph_config)
202202

203+
from manim.mobject.text.text_mobject import Paragraph
204+
203205
self.code_lines = Paragraph(
204206
*code_lines,
205207
**base_paragraph_config,

manim/mobject/text/numbers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from manim.mobject.value_tracker import ValueTracker
1919
from manim.typing import Vector3DLike
2020

21-
string_to_mob_map: dict[str, VMobject] = {}
21+
string_to_mob_map: dict[str, SingleStringMathTex] = {}
2222

2323

2424
class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
@@ -227,7 +227,7 @@ def _string_to_mob(
227227
if string not in string_to_mob_map:
228228
string_to_mob_map[string] = mob_class(string, **kwargs)
229229
mob = string_to_mob_map[string].copy()
230-
mob.font_size = self._font_size # type: ignore[attr-defined]
230+
mob.font_size = self._font_size
231231
return mob
232232

233233
def _get_formatter(self, **kwargs: Any) -> str:

manim/mobject/text/tex_mobject.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def __init__(
259259
*tex_strings: str,
260260
arg_separator: str = " ",
261261
substrings_to_isolate: Iterable[str] | None = None,
262-
tex_to_color_map: dict[str, ManimColor] | None = None,
262+
tex_to_color_map: dict[str, ParsableManimColor] | None = None,
263263
tex_environment: str | None = "align*",
264264
**kwargs: Any,
265265
):
@@ -269,7 +269,7 @@ def __init__(
269269
[] if substrings_to_isolate is None else substrings_to_isolate
270270
)
271271
if tex_to_color_map is None:
272-
self.tex_to_color_map: dict[str, ManimColor] = {}
272+
self.tex_to_color_map: dict[str, ParsableManimColor] = {}
273273
else:
274274
self.tex_to_color_map = tex_to_color_map
275275
self.tex_environment = tex_environment
@@ -416,17 +416,17 @@ def set_opacity_by_tex(
416416
return self
417417

418418
def set_color_by_tex_to_color_map(
419-
self, texs_to_color_map: dict[str, ManimColor], **kwargs: Any
419+
self, texs_to_color_map: dict[str, ParsableManimColor], **kwargs: Any
420420
) -> Self:
421421
for texs, color in list(texs_to_color_map.items()):
422422
try:
423423
# If the given key behaves like tex_strings
424424
texs + ""
425-
self.set_color_by_tex(texs, color, **kwargs)
425+
self.set_color_by_tex(texs, ManimColor(color), **kwargs)
426426
except TypeError:
427427
# If the given key is a tuple
428428
for tex in texs:
429-
self.set_color_by_tex(tex, color, **kwargs)
429+
self.set_color_by_tex(tex, ManimColor(color), **kwargs)
430430
return self
431431

432432
def index_of_part(self, part: MathTex) -> int:
@@ -508,7 +508,9 @@ def __init__(
508508
self.tex_environment = tex_environment
509509
line_separated_items = [s + "\\\\" for s in items]
510510
super().__init__(
511-
*line_separated_items, tex_environment=tex_environment, **kwargs
511+
*line_separated_items,
512+
tex_environment=tex_environment,
513+
**kwargs,
512514
)
513515
for part in self:
514516
dot = MathTex("\\cdot").scale(self.dot_scale_factor)
@@ -519,9 +521,13 @@ def __init__(
519521
def fade_all_but(self, index_or_string: int | str, opacity: float = 0.5) -> None:
520522
arg = index_or_string
521523
if isinstance(arg, str):
522-
part = self.get_part_by_tex(arg)
524+
part: VGroup | VMobject | None = self.get_part_by_tex(arg)
525+
if part is None:
526+
raise Exception(
527+
f"Could not locate part by provided tex string '{arg}'."
528+
)
523529
elif isinstance(arg, int):
524-
part = self.submobjects[arg] # type: ignore[assignment]
530+
part = self.submobjects[arg]
525531
else:
526532
raise TypeError(f"Expected int or string, got {arg}")
527533
for other_part in self.submobjects:

0 commit comments

Comments
 (0)