Skip to content

Commit 75ba8c0

Browse files
authored
Revert "Add type annotations to tex_mobject.py (#4355)"
This reverts commit df36f4f.
1 parent df36f4f commit 75ba8c0

File tree

4 files changed

+56
-69
lines changed

4 files changed

+56
-69
lines changed

manim/mobject/svg/svg_mobject.py

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

1313
from manim import config, logger
14-
from manim.utils.color import ParsableManimColor
1514

1615
from ...constants import RIGHT
1716
from ...utils.bezier import get_quadratic_approximation_of_cubic
@@ -100,11 +99,11 @@ def __init__(
10099
should_center: bool = True,
101100
height: float | None = 2,
102101
width: float | None = None,
103-
color: ParsableManimColor | None = None,
102+
color: str | None = None,
104103
opacity: float | None = None,
105-
fill_color: ParsableManimColor | None = None,
104+
fill_color: str | None = None,
106105
fill_opacity: float | None = None,
107-
stroke_color: ParsableManimColor | None = None,
106+
stroke_color: str | None = None,
108107
stroke_opacity: float | None = None,
109108
stroke_width: float | None = None,
110109
svg_default: dict | None = None,

manim/mobject/text/tex_mobject.py

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@
2626
import itertools as it
2727
import operator as op
2828
import re
29-
from collections.abc import Iterable, Sequence
29+
from collections.abc import Iterable
3030
from functools import reduce
3131
from textwrap import dedent
3232
from typing import Any
3333

34-
from typing_extensions import Self
35-
3634
from manim import config, logger
3735
from manim.constants import *
3836
from manim.mobject.geometry.line import Line
@@ -41,6 +39,8 @@
4139
from manim.utils.tex import TexTemplate
4240
from manim.utils.tex_file_writing import tex_to_svg_file
4341

42+
tex_string_to_mob_map = {}
43+
4444

4545
class SingleStringMathTex(SVGMobject):
4646
"""Elementary building block for rendering text with LaTeX.
@@ -74,8 +74,9 @@ def __init__(
7474
self.tex_environment = tex_environment
7575
if tex_template is None:
7676
tex_template = config["tex_template"]
77-
self.tex_template: TexTemplate = tex_template
77+
self.tex_template = tex_template
7878

79+
assert isinstance(tex_string, str)
7980
self.tex_string = tex_string
8081
file_name = tex_to_svg_file(
8182
self._get_modified_expression(tex_string),
@@ -105,7 +106,7 @@ def __init__(
105106
if self.organize_left_to_right:
106107
self._organize_submobjects_left_to_right()
107108

108-
def __repr__(self) -> str:
109+
def __repr__(self):
109110
return f"{type(self).__name__}({repr(self.tex_string)})"
110111

111112
@property
@@ -114,7 +115,7 @@ def font_size(self) -> float:
114115
return self.height / self.initial_height / SCALE_FACTOR_PER_FONT_POINT
115116

116117
@font_size.setter
117-
def font_size(self, font_val: float) -> None:
118+
def font_size(self, font_val: float):
118119
if font_val <= 0:
119120
raise ValueError("font_size must be greater than 0.")
120121
elif self.height > 0:
@@ -125,13 +126,13 @@ def font_size(self, font_val: float) -> None:
125126
# font_size does not depend on current size.
126127
self.scale(font_val / self.font_size)
127128

128-
def _get_modified_expression(self, tex_string: str) -> str:
129+
def _get_modified_expression(self, tex_string):
129130
result = tex_string
130131
result = result.strip()
131132
result = self._modify_special_strings(result)
132133
return result
133134

134-
def _modify_special_strings(self, tex: str) -> str:
135+
def _modify_special_strings(self, tex):
135136
tex = tex.strip()
136137
should_add_filler = reduce(
137138
op.or_,
@@ -184,7 +185,7 @@ def _modify_special_strings(self, tex: str) -> str:
184185
tex = ""
185186
return tex
186187

187-
def _remove_stray_braces(self, tex: str) -> str:
188+
def _remove_stray_braces(self, tex):
188189
r"""
189190
Makes :class:`~.MathTex` resilient to unmatched braces.
190191
@@ -202,14 +203,14 @@ def _remove_stray_braces(self, tex: str) -> str:
202203
num_rights += 1
203204
return tex
204205

205-
def _organize_submobjects_left_to_right(self) -> Self:
206+
def _organize_submobjects_left_to_right(self):
206207
self.sort(lambda p: p[0])
207208
return self
208209

209-
def get_tex_string(self) -> str:
210+
def get_tex_string(self):
210211
return self.tex_string
211212

212-
def init_colors(self, propagate_colors: bool = True) -> Self:
213+
def init_colors(self, propagate_colors=True):
213214
for submobject in self.submobjects:
214215
# needed to preserve original (non-black)
215216
# TeX colors of individual submobjects
@@ -220,7 +221,6 @@ def init_colors(self, propagate_colors: bool = True) -> Self:
220221
submobject.init_colors()
221222
elif config.renderer == RendererType.CAIRO:
222223
submobject.init_colors(propagate_colors=propagate_colors)
223-
return self
224224

225225

226226
class MathTex(SingleStringMathTex):
@@ -256,22 +256,21 @@ def construct(self):
256256

257257
def __init__(
258258
self,
259-
*tex_strings: str,
259+
*tex_strings,
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, ManimColor] = None,
263263
tex_environment: str = "align*",
264-
**kwargs: Any,
264+
**kwargs,
265265
):
266266
self.tex_template = kwargs.pop("tex_template", config["tex_template"])
267267
self.arg_separator = arg_separator
268268
self.substrings_to_isolate = (
269269
[] if substrings_to_isolate is None else substrings_to_isolate
270270
)
271-
if tex_to_color_map is None:
272-
self.tex_to_color_map: dict[str, ManimColor] = {}
273-
else:
274-
self.tex_to_color_map = tex_to_color_map
271+
self.tex_to_color_map = tex_to_color_map
272+
if self.tex_to_color_map is None:
273+
self.tex_to_color_map = {}
275274
self.tex_environment = tex_environment
276275
self.brace_notation_split_occurred = False
277276
self.tex_strings = self._break_up_tex_strings(tex_strings)
@@ -303,14 +302,12 @@ def __init__(
303302
if self.organize_left_to_right:
304303
self._organize_submobjects_left_to_right()
305304

306-
def _break_up_tex_strings(self, tex_strings: Sequence[str]) -> list[str]:
305+
def _break_up_tex_strings(self, tex_strings):
307306
# Separate out anything surrounded in double braces
308307
pre_split_length = len(tex_strings)
309-
tex_strings_brace_splitted = [
310-
re.split("{{(.*?)}}", str(t)) for t in tex_strings
311-
]
312-
tex_strings_combined = sum(tex_strings_brace_splitted, [])
313-
if len(tex_strings_combined) > pre_split_length:
308+
tex_strings = [re.split("{{(.*?)}}", str(t)) for t in tex_strings]
309+
tex_strings = sum(tex_strings, [])
310+
if len(tex_strings) > pre_split_length:
314311
self.brace_notation_split_occurred = True
315312

316313
# Separate out any strings specified in the isolate
@@ -328,19 +325,19 @@ def _break_up_tex_strings(self, tex_strings: Sequence[str]) -> list[str]:
328325
pattern = "|".join(patterns)
329326
if pattern:
330327
pieces = []
331-
for s in tex_strings_combined:
328+
for s in tex_strings:
332329
pieces.extend(re.split(pattern, s))
333330
else:
334-
pieces = tex_strings_combined
331+
pieces = tex_strings
335332
return [p for p in pieces if p]
336333

337-
def _break_up_by_substrings(self) -> Self:
334+
def _break_up_by_substrings(self):
338335
"""
339336
Reorganize existing submobjects one layer
340337
deeper based on the structure of tex_strings (as a list
341338
of tex_strings)
342339
"""
343-
new_submobjects: list[VMobject] = []
340+
new_submobjects = []
344341
curr_index = 0
345342
for tex_string in self.tex_strings:
346343
sub_tex_mob = SingleStringMathTex(
@@ -362,10 +359,8 @@ def _break_up_by_substrings(self) -> Self:
362359
self.submobjects = new_submobjects
363360
return self
364361

365-
def get_parts_by_tex(
366-
self, tex: str, substring: bool = True, case_sensitive: bool = True
367-
) -> VGroup:
368-
def test(tex1: str, tex2: str) -> bool:
362+
def get_parts_by_tex(self, tex, substring=True, case_sensitive=True):
363+
def test(tex1, tex2):
369364
if not case_sensitive:
370365
tex1 = tex1.lower()
371366
tex2 = tex2.lower()
@@ -376,25 +371,19 @@ def test(tex1: str, tex2: str) -> bool:
376371

377372
return VGroup(*(m for m in self.submobjects if test(tex, m.get_tex_string())))
378373

379-
def get_part_by_tex(self, tex: str, **kwargs: Any) -> MathTex | None:
374+
def get_part_by_tex(self, tex, **kwargs):
380375
all_parts = self.get_parts_by_tex(tex, **kwargs)
381376
return all_parts[0] if all_parts else None
382377

383-
def set_color_by_tex(
384-
self, tex: str, color: ParsableManimColor, **kwargs: Any
385-
) -> Self:
378+
def set_color_by_tex(self, tex, color, **kwargs):
386379
parts_to_color = self.get_parts_by_tex(tex, **kwargs)
387380
for part in parts_to_color:
388381
part.set_color(color)
389382
return self
390383

391384
def set_opacity_by_tex(
392-
self,
393-
tex: str,
394-
opacity: float = 0.5,
395-
remaining_opacity: float | None = None,
396-
**kwargs: Any,
397-
) -> Self:
385+
self, tex: str, opacity: float = 0.5, remaining_opacity: float = None, **kwargs
386+
):
398387
"""
399388
Sets the opacity of the tex specified. If 'remaining_opacity' is specified,
400389
then the remaining tex will be set to that opacity.
@@ -415,9 +404,7 @@ def set_opacity_by_tex(
415404
part.set_opacity(opacity)
416405
return self
417406

418-
def set_color_by_tex_to_color_map(
419-
self, texs_to_color_map: dict[str, ManimColor], **kwargs: Any
420-
) -> Self:
407+
def set_color_by_tex_to_color_map(self, texs_to_color_map, **kwargs):
421408
for texs, color in list(texs_to_color_map.items()):
422409
try:
423410
# If the given key behaves like tex_strings
@@ -429,19 +416,17 @@ def set_color_by_tex_to_color_map(
429416
self.set_color_by_tex(tex, color, **kwargs)
430417
return self
431418

432-
def index_of_part(self, part: MathTex) -> int:
419+
def index_of_part(self, part):
433420
split_self = self.split()
434421
if part not in split_self:
435422
raise ValueError("Trying to get index of part not in MathTex")
436423
return split_self.index(part)
437424

438-
def index_of_part_by_tex(self, tex: str, **kwargs: Any) -> int:
425+
def index_of_part_by_tex(self, tex, **kwargs):
439426
part = self.get_part_by_tex(tex, **kwargs)
440-
if part is None:
441-
return -1
442427
return self.index_of_part(part)
443428

444-
def sort_alphabetically(self) -> None:
429+
def sort_alphabetically(self):
445430
self.submobjects.sort(key=lambda m: m.get_tex_string())
446431

447432

@@ -497,11 +482,11 @@ def construct(self):
497482

498483
def __init__(
499484
self,
500-
*items: str,
501-
buff: float = MED_LARGE_BUFF,
502-
dot_scale_factor: float = 2,
503-
tex_environment: str = "",
504-
**kwargs: Any,
485+
*items,
486+
buff=MED_LARGE_BUFF,
487+
dot_scale_factor=2,
488+
tex_environment=None,
489+
**kwargs,
505490
):
506491
self.buff = buff
507492
self.dot_scale_factor = dot_scale_factor
@@ -516,12 +501,12 @@ def __init__(
516501
part.add_to_back(dot)
517502
self.arrange(DOWN, aligned_edge=LEFT, buff=self.buff)
518503

519-
def fade_all_but(self, index_or_string: int | str, opacity: float = 0.5) -> None:
504+
def fade_all_but(self, index_or_string, opacity=0.5):
520505
arg = index_or_string
521506
if isinstance(arg, str):
522507
part = self.get_part_by_tex(arg)
523508
elif isinstance(arg, int):
524-
part = self.submobjects[arg] # type: ignore[assignment]
509+
part = self.submobjects[arg]
525510
else:
526511
raise TypeError(f"Expected int or string, got {arg}")
527512
for other_part in self.submobjects:
@@ -551,11 +536,11 @@ def construct(self):
551536

552537
def __init__(
553538
self,
554-
*text_parts: str,
555-
include_underline: bool = True,
556-
match_underline_width_to_text: bool = False,
557-
underline_buff: float = MED_SMALL_BUFF,
558-
**kwargs: Any,
539+
*text_parts,
540+
include_underline=True,
541+
match_underline_width_to_text=False,
542+
underline_buff=MED_SMALL_BUFF,
543+
**kwargs,
559544
):
560545
self.include_underline = include_underline
561546
self.match_underline_width_to_text = match_underline_width_to_text

manim/scene/vector_space_scene.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ def add_transformable_label(
962962
label_mob.target_text = new_label # type: ignore[attr-defined]
963963
else:
964964
label_mob.target_text = ( # type: ignore[attr-defined]
965-
f"{transformation_name}({label_mob.get_tex_string()})"
965+
f"{transformation_name}({label_mob.get_tex_string()})" # type: ignore[no-untyped-call]
966966
)
967967
label_mob.vector = vector # type: ignore[attr-defined]
968968
label_mob.kwargs = kwargs # type: ignore[attr-defined]

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ ignore_errors = True
150150
[mypy-manim.mobject.table]
151151
ignore_errors = True
152152

153+
[mypy-manim.mobject.text.tex_mobject]
154+
ignore_errors = True
155+
153156
[mypy-manim.mobject.text.text_mobject]
154157
ignore_errors = True
155158

0 commit comments

Comments
 (0)