Skip to content

Commit fdb5cb9

Browse files
Add typing annotations to svg_mobject.py (#4318)
* Add typing annotations to svg_mobject.py * Added ignore statements in four locations. * cleanly resolve non-None stroke_width in svg_mobject * revert clean solution (???) --------- Co-authored-by: Benjamin Hackl <[email protected]>
1 parent ec501b9 commit fdb5cb9

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

manim/mobject/svg/svg_mobject.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66
from pathlib import Path
7+
from typing import Any
78
from xml.etree import ElementTree as ET
89

910
import numpy as np
@@ -108,7 +109,7 @@ def __init__(
108109
svg_default: dict | None = None,
109110
path_string_config: dict | None = None,
110111
use_svg_cache: bool = True,
111-
**kwargs,
112+
**kwargs: Any,
112113
):
113114
super().__init__(color=None, stroke_color=None, fill_color=None, **kwargs)
114115

@@ -121,10 +122,10 @@ def __init__(
121122
self.color = color
122123
self.opacity = opacity
123124
self.fill_color = fill_color
124-
self.fill_opacity = fill_opacity
125+
self.fill_opacity = fill_opacity # type: ignore[assignment]
125126
self.stroke_color = stroke_color
126-
self.stroke_opacity = stroke_opacity
127-
self.stroke_width = stroke_width
127+
self.stroke_opacity = stroke_opacity # type: ignore[assignment]
128+
self.stroke_width = stroke_width # type: ignore[assignment]
128129
if self.stroke_width is None:
129130
self.stroke_width = 0
130131

@@ -264,13 +265,13 @@ def get_mobjects_from(self, svg: se.SVG) -> list[VMobject]:
264265
svg
265266
The parsed SVG file.
266267
"""
267-
result = []
268+
result: list[VMobject] = []
268269
for shape in svg.elements():
269270
# can we combine the two continue cases into one?
270271
if isinstance(shape, se.Group): # noqa: SIM114
271272
continue
272273
elif isinstance(shape, se.Path):
273-
mob = self.path_to_mobject(shape)
274+
mob: VMobject = self.path_to_mobject(shape)
274275
elif isinstance(shape, se.SimpleLine):
275276
mob = self.line_to_mobject(shape)
276277
elif isinstance(shape, se.Rect):
@@ -424,7 +425,7 @@ def polyline_to_mobject(self, polyline: se.Polyline) -> VMobject:
424425
return vmobject_class().set_points_as_corners(points)
425426

426427
@staticmethod
427-
def text_to_mobject(text: se.Text):
428+
def text_to_mobject(text: se.Text) -> VMobject:
428429
"""Convert a text element to a vectorized mobject.
429430
430431
.. warning::
@@ -437,7 +438,7 @@ def text_to_mobject(text: se.Text):
437438
The parsed SVG text.
438439
"""
439440
logger.warning(f"Unsupported element type: {type(text)}")
440-
return
441+
return # type: ignore[return-value]
441442

442443
def move_into_position(self) -> None:
443444
"""Scale and move the generated mobject into position."""
@@ -482,7 +483,7 @@ def __init__(
482483
long_lines: bool = False,
483484
should_subdivide_sharp_curves: bool = False,
484485
should_remove_null_curves: bool = False,
485-
**kwargs,
486+
**kwargs: Any,
486487
):
487488
# Get rid of arcs
488489
path_obj.approximate_arcs_with_quads()
@@ -511,11 +512,11 @@ def init_points(self) -> None:
511512

512513
def handle_commands(self) -> None:
513514
all_points: list[np.ndarray] = []
514-
last_move = None
515+
last_move: np.ndarray = None
515516
curve_start = None
516517
last_true_move = None
517518

518-
def move_pen(pt, *, true_move: bool = False):
519+
def move_pen(pt: np.ndarray, *, true_move: bool = False) -> None:
519520
nonlocal last_move, curve_start, last_true_move
520521
last_move = pt
521522
if curve_start is None:
@@ -525,25 +526,29 @@ def move_pen(pt, *, true_move: bool = False):
525526

526527
if self.n_points_per_curve == 4:
527528

528-
def add_cubic(start, cp1, cp2, end):
529+
def add_cubic(
530+
start: np.ndarray, cp1: np.ndarray, cp2: np.ndarray, end: np.ndarray
531+
) -> None:
529532
nonlocal all_points
530533
assert len(all_points) % 4 == 0, len(all_points)
531534
all_points += [start, cp1, cp2, end]
532535
move_pen(end)
533536

534-
def add_quad(start, cp, end):
537+
def add_quad(start: np.ndarray, cp: np.ndarray, end: np.ndarray) -> None:
535538
add_cubic(start, (start + cp + cp) / 3, (cp + cp + end) / 3, end)
536539
move_pen(end)
537540

538-
def add_line(start, end):
541+
def add_line(start: np.ndarray, end: np.ndarray) -> None:
539542
add_cubic(
540543
start, (start + start + end) / 3, (start + end + end) / 3, end
541544
)
542545
move_pen(end)
543546

544547
else:
545548

546-
def add_cubic(start, cp1, cp2, end):
549+
def add_cubic(
550+
start: np.ndarray, cp1: np.ndarray, cp2: np.ndarray, end: np.ndarray
551+
) -> None:
547552
nonlocal all_points
548553
assert len(all_points) % 3 == 0, len(all_points)
549554
two_quads = get_quadratic_approximation_of_cubic(
@@ -556,13 +561,13 @@ def add_cubic(start, cp1, cp2, end):
556561
all_points += two_quads[3:].tolist()
557562
move_pen(end)
558563

559-
def add_quad(start, cp, end):
564+
def add_quad(start: np.ndarray, cp: np.ndarray, end: np.ndarray) -> None:
560565
nonlocal all_points
561566
assert len(all_points) % 3 == 0, len(all_points)
562567
all_points += [start, cp, end]
563568
move_pen(end)
564569

565-
def add_line(start, end):
570+
def add_line(start: np.ndarray, end: np.ndarray) -> None:
566571
add_quad(start, (start + end) / 2, end)
567572
move_pen(end)
568573

mypy.ini

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

153-
[mypy-manim.mobject.svg.svg_mobject]
154-
ignore_errors = True
155-
156153
[mypy-manim.mobject.table]
157154
ignore_errors = True
158155

0 commit comments

Comments
 (0)