Skip to content

Commit ea16d22

Browse files
henrikmidtibypre-commit-ci[bot]chopan050
authored
Add type annotations to scene.py and vector_space_scene.py (#4260)
* Stop ignoring errors from manim/scene.py Count: 307 errors Type annotations on scene/vector_space_scene.py Add type annotations to scene/vector_space_scene.py Mypy count: 210 Reverting two changes that triggers an error in the automatic testing. Further work on type hinting. Avoid forwarding positional arguments from Arrow to Line in the constructor. Revert "Avoid forwarding positional arguments from Arrow to Line in the constructor." This reverts commit 80ae857. Removed several type ignore statements and addressed comments from JasonGrace2282 Revert "Activate mypy check of mobject.geometry.*" This reverts commit d477c9a. Revert "Removed several type ignore statements and addressed comments from JasonGrace2282" This reverts commit 07bbe3f. Added type annotations to zoomed_scene.py Error count: 308 -> 303 Adding type annotations to all methods in vector_space_scene.py Error count: 303 -> 272 Get rid of no-untyped-call errors from my in the vector_space_scene.py file Error count: 272 -> 343 Handle type issues related to ManimColor in vector_space_scene.py Handle var-annotated issues in vector_space_scene.py Error count: 332 -> 330 Handling has-type type errors in vector_space_scene.py Error count: 330 -> 285 Handled name-defined type issues in vector_space_scene.py Error count: 285 -> 282 Address type issue with calling an untyped method. Error count: 282 -> 281 Fix some typing issues in transform_mathcing_parts.py Change stroke_width to float in vector_space_scene.py Handled a few type errors. Error count: 267 Handled several typing issues in three_d_scene.py Error count: 267 -> 248 Dealing with type errors in scene_file_writer.py Error count: 248 -> 216 Ensured that all methods in scene.py have type declarations. Error count: 216 -> 225 Handle type issues related to interactivity by asserting that the camera is the OpenGLCamera Error count: 225 -> 182 Handle type issues in scene.py Error count: 182 -> 167 Asserting that the renderer or camera is of the proper type to use certain methods. This is mainly related to interactive elements and the 3D camera used in the ThreeDScene Error count: 167 -> 143 Avoid cyclic import of dependencies Error count: 143 -> 143 Handling no-untyped-call type errors in manim/scene/scene.py Error count: 143 -> 131 Handling assignment type errors in manim/scene/*.py Error count: 131 -> 121 Handling arg-type type errors in manim/scene/*.py Error count: 121 -> 116 Handling arg-type type errors in manim/scene/*.py Error count: 116 -> 112 Fixing various type errors Error count: 112 -> 102 Fixing various type errors Error count: 102 -> 97 Fixing various type errors Error count: 97 -> 90 Some aggressive changes to silence a significant number of type errors. Error count: 90 -> 66 Commented out an import (IPython) that makes the CI tests fail. Fix various type errors. More type annotations. Code cleanup. Remove the property mobject_updater_lists of the Scene class as it is not used anywhere. Handle import-untyped typing issues. More work on type annotations in manin/scene/.* More work on scenes/*.py looking at the dependency opengl_renderer.py More work on types in scenes/*.py Ignored an old bunch of type ignore statements. More work on dependencies for scene.py More work on dependencies for scene.py * Stop ignoring errors from manim/scene/scene.py mypy error count: 307 * Adding type annotations to scene.py (C1) * Adding type annotations to scene.py (C2.1) * Adding type annotations to scene.py (C2.2) * Adding type annotations to scene.py (C2.3) * Adding type annotations to scene.py (C3) * Adding type annotations to scene.py (C4) * Adding type annotations to scene.py (C5) * Adding type annotations to scene.py (C6) * Adding type annotations to scene.py (C7) * ... * Focus on scene.py * Adding type annotations to vector_space_scene.py * Added types to opengl_renderer.py * Added types to cairo_renderer.py * Fixed the last mypy errors in scene.py - many was ignored * Fixed the last mypy errors in vector_space_scene.py - many was ignored * Got rid of the last mypy errors. * Activate mypy check of vector_space_scene.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Code cleanup. * Update manim/gui/gui.py Co-authored-by: Francisco Manríquez Novoa <[email protected]> * Update manim/renderer/opengl_renderer.py Co-authored-by: Francisco Manríquez Novoa <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update manim/scene/scene.py Co-authored-by: Francisco Manríquez Novoa <[email protected]> * Update manim/scene/scene.py Co-authored-by: Francisco Manríquez Novoa <[email protected]> * Update manim/scene/scene.py Co-authored-by: Francisco Manríquez Novoa <[email protected]> * Update manim/scene/scene.py Co-authored-by: Francisco Manríquez Novoa <[email protected]> * Update manim/scene/scene.py Co-authored-by: Francisco Manríquez Novoa <[email protected]> * Implementing suggestions from chopan50 * Explicitly mention all files where type errors are ignored in mypy.ini # Conflicts: # mypy.ini * Fix issue * Updates based on comments from chopan50 * Updates suggested by Chopan50 * Addressed more comments from chopan50 * Addressing more comments from chopan50 * Added docstring to SceneInteractAction * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Differentiate between _AnimationBuilder from an mobject and an opengl_mobject * Avoid a nameclash with the mobject module and variable name * Last touches. * Rolled back some changes related to _AnimationBuilder --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Francisco Manríquez Novoa <[email protected]>
1 parent c4b7a80 commit ea16d22

File tree

9 files changed

+453
-270
lines changed

9 files changed

+453
-270
lines changed

manim/animation/animation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def construct(self):
540540

541541

542542
def prepare_animation(
543-
anim: Animation | mobject._AnimationBuilder,
543+
anim: Animation | mobject._AnimationBuilder | opengl_mobject._AnimationBuilder,
544544
) -> Animation:
545545
r"""Returns either an unchanged animation, or the animation built
546546
from a passed animation factory.

manim/gui/gui.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,26 @@
1010
dearpygui_imported = False
1111

1212

13+
from collections.abc import Sequence
14+
from typing import TYPE_CHECKING, Any
15+
1316
from .. import __version__, config
1417
from ..utils.module_ops import scene_classes_from_file
1518

19+
if TYPE_CHECKING:
20+
from ..renderer.opengl_renderer import OpenGLRenderer
21+
22+
1623
__all__ = ["configure_pygui"]
1724

1825
if dearpygui_imported:
1926
dpg.create_context()
2027
window = dpg.generate_uuid()
2128

2229

23-
def configure_pygui(renderer, widgets, update=True):
30+
def configure_pygui(
31+
renderer: OpenGLRenderer, widgets: Sequence[dict[str, Any]], update: bool = True
32+
) -> None:
2433
if not dearpygui_imported:
2534
raise RuntimeError("Attempted to use DearPyGUI when it isn't imported.")
2635
if update:

manim/mobject/matrix.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def add_background_to_entries(self):
400400
mob.add_background_rectangle()
401401
return self
402402

403-
def get_mob_matrix(self):
403+
def get_mob_matrix(self) -> list[list[MathTex]]:
404404
"""Return the underlying mob matrix mobjects.
405405
406406
Returns
@@ -435,13 +435,13 @@ def construct(self):
435435
"""
436436
return self.elements
437437

438-
def get_brackets(self):
438+
def get_brackets(self) -> VGroup:
439439
r"""Return the bracket mobjects.
440440
441441
Returns
442442
--------
443-
List[:class:`~.VGroup`]
444-
Each VGroup contains a bracket
443+
:class:`~.VGroup`
444+
A VGroup containing the left and right bracket.
445445
446446
Examples
447447
--------

manim/mobject/text/tex_mobject.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from collections.abc import Iterable
3030
from functools import reduce
3131
from textwrap import dedent
32+
from typing import Any
3233

3334
from manim import config, logger
3435
from manim.constants import *
@@ -447,7 +448,11 @@ class Tex(MathTex):
447448
"""
448449

449450
def __init__(
450-
self, *tex_strings, arg_separator="", tex_environment="center", **kwargs
451+
self,
452+
*tex_strings: str,
453+
arg_separator: str = "",
454+
tex_environment: str = "center",
455+
**kwargs: Any,
451456
):
452457
super().__init__(
453458
*tex_strings,

manim/renderer/cairo_renderer.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class CairoRenderer:
3333

3434
def __init__(
3535
self,
36-
file_writer_class=SceneFileWriter,
37-
camera_class=None,
38-
skip_animations=False,
39-
**kwargs,
40-
):
36+
file_writer_class: type[SceneFileWriter] = SceneFileWriter,
37+
camera_class: type[Camera] | None = None,
38+
skip_animations: bool = False,
39+
**kwargs: Any,
40+
) -> None:
4141
# All of the following are set to EITHER the value passed via kwargs,
4242
# OR the value stored in the global config dict at the time of
4343
# _instance construction_.
@@ -51,7 +51,7 @@ def __init__(
5151
self.time = 0
5252
self.static_image = None
5353

54-
def init_scene(self, scene):
54+
def init_scene(self, scene: Scene) -> None:
5555
self.file_writer: Any = self._file_writer_class(
5656
self,
5757
scene.__class__.__name__,
@@ -119,12 +119,12 @@ def play(
119119

120120
def update_frame( # TODO Description in Docstring
121121
self,
122-
scene,
122+
scene: Scene,
123123
mobjects: typing.Iterable[Mobject] | None = None,
124124
include_submobjects: bool = True,
125125
ignore_skipping: bool = True,
126-
**kwargs,
127-
):
126+
**kwargs: Any,
127+
) -> None:
128128
"""Update the frame.
129129
130130
Parameters
@@ -263,7 +263,7 @@ def update_skipping_status(self):
263263
self.skip_animations = True
264264
raise EndSceneEarlyException()
265265

266-
def scene_finished(self, scene):
266+
def scene_finished(self, scene: Scene) -> None:
267267
# If no animations in scene, render an image instead
268268
if self.num_plays:
269269
self.file_writer.finish()

manim/renderer/opengl_renderer.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
import itertools as it
55
import time
66
from functools import cached_property
7-
from typing import Any
7+
from typing import TYPE_CHECKING, Any
88

99
import moderngl
1010
import numpy as np
1111
from PIL import Image
1212

1313
from manim import config, logger
14-
from manim.mobject.opengl.opengl_mobject import OpenGLMobject, OpenGLPoint
14+
from manim.mobject.opengl.opengl_mobject import (
15+
OpenGLMobject,
16+
OpenGLPoint,
17+
_AnimationBuilder,
18+
)
1519
from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject
1620
from manim.utils.caching import handle_caching_play
1721
from manim.utils.color import color_to_rgba
@@ -35,6 +39,14 @@
3539
render_opengl_vectorized_mobject_stroke,
3640
)
3741

42+
if TYPE_CHECKING:
43+
from typing_extensions import Self
44+
45+
from manim.animation.animation import Animation
46+
from manim.mobject.mobject import Mobject
47+
from manim.scene.scene import Scene
48+
49+
3850
__all__ = ["OpenGLCamera", "OpenGLRenderer"]
3951

4052

@@ -102,7 +114,7 @@ def __init__(
102114
self.euler_angles = euler_angles
103115
self.refresh_rotation_matrix()
104116

105-
def get_position(self):
117+
def get_position(self) -> Point3D:
106118
return self.model_matrix[:, 3][:3]
107119

108120
def set_position(self, position):
@@ -123,7 +135,7 @@ def init_points(self):
123135
self.set_height(self.frame_shape[1], stretch=True)
124136
self.move_to(self.center_point)
125137

126-
def to_default_state(self):
138+
def to_default_state(self) -> Self:
127139
self.center()
128140
self.set_height(config["frame_height"])
129141
self.set_width(config["frame_width"])
@@ -166,28 +178,28 @@ def set_euler_angles(self, theta=None, phi=None, gamma=None):
166178
self.refresh_rotation_matrix()
167179
return self
168180

169-
def set_theta(self, theta):
181+
def set_theta(self, theta: float) -> Self:
170182
return self.set_euler_angles(theta=theta)
171183

172-
def set_phi(self, phi):
184+
def set_phi(self, phi: float) -> Self:
173185
return self.set_euler_angles(phi=phi)
174186

175-
def set_gamma(self, gamma):
187+
def set_gamma(self, gamma: float) -> Self:
176188
return self.set_euler_angles(gamma=gamma)
177189

178-
def increment_theta(self, dtheta):
190+
def increment_theta(self, dtheta: float) -> Self:
179191
self.euler_angles[0] += dtheta
180192
self.refresh_rotation_matrix()
181193
return self
182194

183-
def increment_phi(self, dphi):
195+
def increment_phi(self, dphi: float) -> Self:
184196
phi = self.euler_angles[1]
185197
new_phi = clip(phi + dphi, -PI / 2, PI / 2)
186198
self.euler_angles[1] = new_phi
187199
self.refresh_rotation_matrix()
188200
return self
189201

190-
def increment_gamma(self, dgamma):
202+
def increment_gamma(self, dgamma: float) -> Self:
191203
self.euler_angles[2] += dgamma
192204
self.refresh_rotation_matrix()
193205
return self
@@ -199,15 +211,15 @@ def get_center(self):
199211
# Assumes first point is at the center
200212
return self.points[0]
201213

202-
def get_width(self):
214+
def get_width(self) -> float:
203215
points = self.points
204216
return points[2, 0] - points[1, 0]
205217

206-
def get_height(self):
218+
def get_height(self) -> float:
207219
points = self.points
208220
return points[4, 1] - points[3, 1]
209221

210-
def get_focal_distance(self):
222+
def get_focal_distance(self) -> float:
211223
return self.focal_distance * self.get_height()
212224

213225
def interpolate(self, *args, **kwargs):
@@ -412,7 +424,12 @@ def update_skipping_status(self) -> None:
412424
raise EndSceneEarlyException()
413425

414426
@handle_caching_play
415-
def play(self, scene, *args, **kwargs):
427+
def play(
428+
self,
429+
scene: Scene,
430+
*args: Animation | Mobject | _AnimationBuilder,
431+
**kwargs: Any,
432+
) -> None:
416433
# TODO: Handle data locking / unlocking.
417434
self.animation_start_time = time.time()
418435
self.file_writer.begin_animation(not self.skip_animations)
@@ -440,11 +457,13 @@ def play(self, scene, *args, **kwargs):
440457
self.time += scene.duration
441458
self.num_plays += 1
442459

443-
def clear_screen(self):
460+
def clear_screen(self) -> None:
444461
self.frame_buffer_object.clear(*self.background_color)
445462
self.window.swap_buffers()
446463

447-
def render(self, scene, frame_offset, moving_mobjects):
464+
def render(
465+
self, scene: Scene, frame_offset, moving_mobjects: list[Mobject]
466+
) -> None:
448467
self.update_frame(scene)
449468

450469
if self.skip_animations:

0 commit comments

Comments
 (0)