A community-maintained Python framework for creating mathematical animations.
Add typing annotations to svg_mobject.py (#4318)
Henrik Skov Midtibyfdb5cb9f0406
diff --git a/manim/mobject/svg/svg_mobject.py b/manim/mobject/svg/svg_mobject.pyindex 9a8666c2f..8891f25e0 100644--- a/manim/mobject/svg/svg_mobject.py+++ b/manim/mobject/svg/svg_mobject.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport osfrom pathlib import Path+from typing import Anyfrom xml.etree import ElementTree as ETimport numpy as np@@ -108,7 +109,7 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):svg_default: dict | None = None,path_string_config: dict | None = None,use_svg_cache: bool = True,- **kwargs,+ **kwargs: Any,):super().__init__(color=None, stroke_color=None, fill_color=None, **kwargs)@@ -121,10 +122,10 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):self.color = colorself.opacity = opacityself.fill_color = fill_color- self.fill_opacity = fill_opacity+ self.fill_opacity = fill_opacity # type: ignore[assignment]self.stroke_color = stroke_color- self.stroke_opacity = stroke_opacity- self.stroke_width = stroke_width+ self.stroke_opacity = stroke_opacity # type: ignore[assignment]+ self.stroke_width = stroke_width # type: ignore[assignment]if self.stroke_width is None:self.stroke_width = 0@@ -264,13 +265,13 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):svgThe parsed SVG file."""- result = []+ result: list[VMobject] = []for shape in svg.elements():# can we combine the two continue cases into one?if isinstance(shape, se.Group): # noqa: SIM114continueelif isinstance(shape, se.Path):- mob = self.path_to_mobject(shape)+ mob: VMobject = self.path_to_mobject(shape)elif isinstance(shape, se.SimpleLine):mob = self.line_to_mobject(shape)elif isinstance(shape, se.Rect):@@ -424,7 +425,7 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):return vmobject_class().set_points_as_corners(points)@staticmethod- def text_to_mobject(text: se.Text):+ def text_to_mobject(text: se.Text) -> VMobject:"""Convert a text element to a vectorized mobject... warning::@@ -437,7 +438,7 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):The parsed SVG text."""logger.warning(f"Unsupported element type: {type(text)}")- return+ return # type: ignore[return-value]def move_into_position(self) -> None:"""Scale and move the generated mobject into position."""@@ -482,7 +483,7 @@ class VMobjectFromSVGPath(VMobject, metaclass=ConvertToOpenGL):long_lines: bool = False,should_subdivide_sharp_curves: bool = False,should_remove_null_curves: bool = False,- **kwargs,+ **kwargs: Any,):# Get rid of arcspath_obj.approximate_arcs_with_quads()@@ -511,11 +512,11 @@ class VMobjectFromSVGPath(VMobject, metaclass=ConvertToOpenGL):def handle_commands(self) -> None:all_points: list[np.ndarray] = []- last_move = None+ last_move: np.ndarray = Nonecurve_start = Nonelast_true_move = None- def move_pen(pt, *, true_move: bool = False):+ def move_pen(pt: np.ndarray, *, true_move: bool = False) -> None:nonlocal last_move, curve_start, last_true_movelast_move = ptif curve_start is None:@@ -525,17 +526,19 @@ class VMobjectFromSVGPath(VMobject, metaclass=ConvertToOpenGL):if self.n_points_per_curve == 4:- def add_cubic(start, cp1, cp2, end):+ def add_cubic(+ start: np.ndarray, cp1: np.ndarray, cp2: np.ndarray, end: np.ndarray+ ) -> None:nonlocal all_pointsassert len(all_points) % 4 == 0, len(all_points)all_points += [start, cp1, cp2, end]move_pen(end)- def add_quad(start, cp, end):+ def add_quad(start: np.ndarray, cp: np.ndarray, end: np.ndarray) -> None:add_cubic(start, (start + cp + cp) / 3, (cp + cp + end) / 3, end)move_pen(end)- def add_line(start, end):+ def add_line(start: np.ndarray, end: np.ndarray) -> None:add_cubic(start, (start + start + end) / 3, (start + end + end) / 3, end)@@ -543,7 +546,9 @@ class VMobjectFromSVGPath(VMobject, metaclass=ConvertToOpenGL):else:- def add_cubic(start, cp1, cp2, end):+ def add_cubic(+ start: np.ndarray, cp1: np.ndarray, cp2: np.ndarray, end: np.ndarray+ ) -> None:nonlocal all_pointsassert len(all_points) % 3 == 0, len(all_points)two_quads = get_quadratic_approximation_of_cubic(@@ -556,13 +561,13 @@ class VMobjectFromSVGPath(VMobject, metaclass=ConvertToOpenGL):all_points += two_quads[3:].tolist()move_pen(end)- def add_quad(start, cp, end):+ def add_quad(start: np.ndarray, cp: np.ndarray, end: np.ndarray) -> None:nonlocal all_pointsassert len(all_points) % 3 == 0, len(all_points)all_points += [start, cp, end]move_pen(end)- def add_line(start, end):+ def add_line(start: np.ndarray, end: np.ndarray) -> None:add_quad(start, (start + end) / 2, end)move_pen(end)diff --git a/mypy.ini b/mypy.iniindex d1602ffe2..68b01b00b 100644--- a/mypy.ini+++ b/mypy.ini@@ -150,9 +150,6 @@ ignore_errors = True[mypy-manim.mobject.svg.brace]ignore_errors = True-[mypy-manim.mobject.svg.svg_mobject]-ignore_errors = True-[mypy-manim.mobject.table]ignore_errors = True