Skip to content

Add type annotations to text_mobject.py #4381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
eabb0ee
Activated typechecking in mobject.text.*
henrikmidtiby Jan 5, 2025
866ea02
Fixed typing errors in mobject/text/code_mobject.py
henrikmidtiby Jan 5, 2025
952b302
Add type annotations to mobject/text/numbers.py
henrikmidtiby Jan 6, 2025
c6f7059
Import ManimColor on every run
henrikmidtiby Jan 6, 2025
648277a
Add type annotations to mobject/text/tex_mobject.py
henrikmidtiby Jan 6, 2025
f56effd
Return the object itself from init_colors
henrikmidtiby Jan 6, 2025
e76e53b
Fix issue.
henrikmidtiby Jan 6, 2025
94382c5
Add type annotations to mobject/text/text_mobject.py
henrikmidtiby Jan 7, 2025
19157a0
Add type annotations to camera/camera.py
henrikmidtiby Jan 10, 2025
94215e0
Remove deprecated code
henrikmidtiby Jul 29, 2025
7f24484
First pass through text_mobject.py for adding type annotations.
henrikmidtiby Jul 29, 2025
1ae6ac5
More work on typing text_mobject.py
henrikmidtiby Jul 31, 2025
46fc244
Merge remote-tracking branch 'upstream/main' into Typing_camera
henrikmidtiby Aug 1, 2025
001e0c8
Hunting down the last typing issues.
henrikmidtiby Aug 8, 2025
2649c5f
Ensure that color_gradient always returns a list
henrikmidtiby Aug 8, 2025
589abb1
Merge remote-tracking branch 'upstream/main' into TypingTextMobject
henrikmidtiby Aug 8, 2025
61a1359
Merge remote-tracking branch 'upstream/main' into Typing_camera
henrikmidtiby Aug 8, 2025
2842c09
Code cleanup
henrikmidtiby Aug 8, 2025
f5529ef
Merge branch 'TypingTextMobject' into Typing_camera
henrikmidtiby Aug 8, 2025
eeacbd7
Code cleanup
henrikmidtiby Aug 8, 2025
de1753f
Code cleanup
henrikmidtiby Aug 8, 2025
69486d8
Use the right version from the merge
henrikmidtiby Aug 8, 2025
9864cd3
Merge branch 'main' into TypingTextMobject
behackl Aug 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion manim/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ def __getitem__(self, value):
def __iter__(self):
return iter(self.split())

def __len__(self):
def __len__(self) -> int:
return len(self.split())

def get_group_class(self) -> type[Group]:
Expand Down
4 changes: 2 additions & 2 deletions manim/mobject/svg/svg_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import svgelements as se

from manim import config, logger
from manim.utils.color import ParsableManimColor
from manim.utils.color import ManimColor, ParsableManimColor

from ...constants import RIGHT
from ...utils.bezier import get_quadratic_approximation_of_cubic
Expand Down Expand Up @@ -120,7 +120,7 @@
self.should_center = should_center
self.svg_height = height
self.svg_width = width
self.color = color
self.color = ManimColor(color)

Check warning

Code scanning / CodeQL

Overwriting attribute in super-class or sub-class Warning

Assignment overwrites attribute color, which was previously defined in superclass
Mobject
.
self.opacity = opacity
self.fill_color = fill_color
self.fill_opacity = fill_opacity # type: ignore[assignment]
Expand Down
4 changes: 3 additions & 1 deletion manim/mobject/text/code_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from manim.mobject.geometry.arc import Dot
from manim.mobject.geometry.shape_matchers import SurroundingRectangle
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
from manim.mobject.text.text_mobject import Paragraph
from manim.mobject.types.vectorized_mobject import VGroup, VMobject
from manim.typing import StrPath
from manim.utils.color import WHITE, ManimColor
Expand Down Expand Up @@ -119,6 +118,7 @@ def construct(self):
"line_spacing": 0.5,
"disable_ligatures": True,
}
code: VMobject

def __init__(
self,
Expand Down Expand Up @@ -200,6 +200,8 @@ def __init__(
base_paragraph_config = self.default_paragraph_config.copy()
base_paragraph_config.update(paragraph_config)

from manim.mobject.text.text_mobject import Paragraph

self.code_lines = Paragraph(
*code_lines,
**base_paragraph_config,
Expand Down
4 changes: 2 additions & 2 deletions manim/mobject/text/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from manim.mobject.value_tracker import ValueTracker
from manim.typing import Vector3DLike

string_to_mob_map: dict[str, VMobject] = {}
string_to_mob_map: dict[str, SingleStringMathTex] = {}


class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
Expand Down Expand Up @@ -227,7 +227,7 @@ def _string_to_mob(
if string not in string_to_mob_map:
string_to_mob_map[string] = mob_class(string, **kwargs)
mob = string_to_mob_map[string].copy()
mob.font_size = self._font_size # type: ignore[attr-defined]
mob.font_size = self._font_size
return mob

def _get_formatter(self, **kwargs: Any) -> str:
Expand Down
20 changes: 12 additions & 8 deletions manim/mobject/text/tex_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def __init__(
*tex_strings: str,
arg_separator: str = " ",
substrings_to_isolate: Iterable[str] | None = None,
tex_to_color_map: dict[str, ManimColor] | None = None,
tex_to_color_map: dict[str, ParsableManimColor] | None = None,
tex_environment: str | None = "align*",
**kwargs: Any,
):
Expand All @@ -269,7 +269,7 @@ def __init__(
[] if substrings_to_isolate is None else substrings_to_isolate
)
if tex_to_color_map is None:
self.tex_to_color_map: dict[str, ManimColor] = {}
self.tex_to_color_map: dict[str, ParsableManimColor] = {}
else:
self.tex_to_color_map = tex_to_color_map
self.tex_environment = tex_environment
Expand Down Expand Up @@ -416,17 +416,17 @@ def set_opacity_by_tex(
return self

def set_color_by_tex_to_color_map(
self, texs_to_color_map: dict[str, ManimColor], **kwargs: Any
self, texs_to_color_map: dict[str, ParsableManimColor], **kwargs: Any
) -> Self:
for texs, color in list(texs_to_color_map.items()):
try:
# If the given key behaves like tex_strings
texs + ""
self.set_color_by_tex(texs, color, **kwargs)
self.set_color_by_tex(texs, ManimColor(color), **kwargs)
except TypeError:
# If the given key is a tuple
for tex in texs:
self.set_color_by_tex(tex, color, **kwargs)
self.set_color_by_tex(tex, ManimColor(color), **kwargs)
return self

def index_of_part(self, part: MathTex) -> int:
Expand Down Expand Up @@ -508,7 +508,9 @@ def __init__(
self.tex_environment = tex_environment
line_separated_items = [s + "\\\\" for s in items]
super().__init__(
*line_separated_items, tex_environment=tex_environment, **kwargs
*line_separated_items,
tex_environment=tex_environment,
**kwargs,
)
for part in self:
dot = MathTex("\\cdot").scale(self.dot_scale_factor)
Expand All @@ -519,9 +521,11 @@ def __init__(
def fade_all_but(self, index_or_string: int | str, opacity: float = 0.5) -> None:
arg = index_or_string
if isinstance(arg, str):
part = self.get_part_by_tex(arg)
part: VGroup | VMobject | None = self.get_part_by_tex(arg)
if part is None:
raise Exception("Could not locate part by provided tex string")
elif isinstance(arg, int):
part = self.submobjects[arg] # type: ignore[assignment]
part = self.submobjects[arg]
else:
raise TypeError(f"Expected int or string, got {arg}")
for other_part in self.submobjects:
Expand Down
Loading
Loading