Skip to content

Adding type annotations to scene.py and vector_space_scene.py #4260

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 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b7dbc94
Stop ignoring errors from manim/scene.py
henrikmidtiby May 26, 2025
329e98a
Stop ignoring errors from manim/scene/scene.py
henrikmidtiby May 26, 2025
294ea2a
Adding type annotations to scene.py (C1)
henrikmidtiby May 26, 2025
b8fa2c0
Adding type annotations to scene.py (C2.1)
henrikmidtiby May 26, 2025
68d5939
Adding type annotations to scene.py (C2.2)
henrikmidtiby May 26, 2025
94a56a0
Adding type annotations to scene.py (C2.3)
henrikmidtiby May 26, 2025
ca87c16
Adding type annotations to scene.py (C3)
henrikmidtiby May 26, 2025
f920d03
Adding type annotations to scene.py (C4)
henrikmidtiby May 26, 2025
86af814
Adding type annotations to scene.py (C5)
henrikmidtiby May 26, 2025
9bfc983
Adding type annotations to scene.py (C6)
henrikmidtiby May 26, 2025
591bc77
Adding type annotations to scene.py (C7)
henrikmidtiby May 26, 2025
9a8f9bc
...
henrikmidtiby May 26, 2025
241ebb0
Focus on scene.py
henrikmidtiby May 26, 2025
e93dd55
Adding type annotations to vector_space_scene.py
henrikmidtiby May 26, 2025
203124f
Added types to opengl_renderer.py
henrikmidtiby May 26, 2025
2760cb2
Added types to cairo_renderer.py
henrikmidtiby May 26, 2025
5215c9b
Fixed the last mypy errors in scene.py - many was ignored
henrikmidtiby May 26, 2025
6a53cc1
Fixed the last mypy errors in vector_space_scene.py - many was ignored
henrikmidtiby May 26, 2025
d0bb1c0
Got rid of the last mypy errors.
henrikmidtiby May 26, 2025
04a437d
Activate mypy check of vector_space_scene.py
henrikmidtiby May 26, 2025
ce046c4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 26, 2025
2e45b3b
Code cleanup.
henrikmidtiby May 27, 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
10 changes: 9 additions & 1 deletion manim/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@
dearpygui_imported = False


from typing import TYPE_CHECKING, Any

from .. import __version__, config
from ..utils.module_ops import scene_classes_from_file

if TYPE_CHECKING:
from ..renderer.opengl_renderer import OpenGLRenderer

Check failure

Code scanning / CodeQL

Module-level cyclic import Error

'OpenGLRenderer' may not be defined if module
manim.renderer.opengl_renderer
is imported before module
manim.gui.gui
, as the
definition
of OpenGLRenderer occurs after the cyclic
import
of manim.gui.gui.
'OpenGLRenderer' may not be defined if module
manim.renderer.opengl_renderer
is imported before module
manim.gui.gui
, as the
definition
of OpenGLRenderer occurs after the cyclic
import
of manim.gui.gui.
'OpenGLRenderer' may not be defined if module
manim.renderer.opengl_renderer
is imported before module
manim.gui.gui
, as the
definition
of OpenGLRenderer occurs after the cyclic
import
of manim.gui.gui.
'OpenGLRenderer' may not be defined if module
manim.renderer.opengl_renderer
is imported before module
manim.gui.gui
, as the
definition
of OpenGLRenderer occurs after the cyclic
import
of manim.gui.gui.
'OpenGLRenderer' may not be defined if module
manim.renderer.opengl_renderer
is imported before module
manim.gui.gui
, as the
definition
of OpenGLRenderer occurs after the cyclic
import
of manim.gui.gui.


__all__ = ["configure_pygui"]

if dearpygui_imported:
dpg.create_context()
window = dpg.generate_uuid()


def configure_pygui(renderer, widgets, update=True):
def configure_pygui(
renderer: OpenGLRenderer, widgets: list[Any], update: bool = True
) -> None:
if not dearpygui_imported:
raise RuntimeError("Attempted to use DearPyGUI when it isn't imported.")
if update:
Expand Down
4 changes: 2 additions & 2 deletions manim/mobject/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def add_background_to_entries(self):
mob.add_background_rectangle()
return self

def get_mob_matrix(self):
def get_mob_matrix(self) -> list[list[MathTex]]:
"""Return the underlying mob matrix mobjects.

Returns
Expand Down Expand Up @@ -435,7 +435,7 @@ def construct(self):
"""
return self.elements

def get_brackets(self):
def get_brackets(self) -> VGroup:
r"""Return the bracket mobjects.

Returns
Expand Down
4 changes: 3 additions & 1 deletion manim/mobject/text/tex_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
from functools import reduce
from textwrap import dedent

from typing_extensions import Self

from manim import config, logger
from manim.constants import *
from manim.mobject.geometry.line import Line
Expand Down Expand Up @@ -448,7 +450,7 @@ class Tex(MathTex):

def __init__(
self, *tex_strings, arg_separator="", tex_environment="center", **kwargs
):
) -> Self:
super().__init__(
*tex_strings,
arg_separator=arg_separator,
Expand Down
20 changes: 10 additions & 10 deletions manim/renderer/cairo_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Any

from manim.animation.animation import Animation
from manim.scene.scene import Scene

Check failure

Code scanning / CodeQL

Module-level cyclic import Error

'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.cairo_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.cairo_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.cairo_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.cairo_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.cairo_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.cairo_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.cairo_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.cairo_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.cairo_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.cairo_renderer
, as the
definition
of Scene occurs after the cyclic import of manim.renderer.cairo_renderer.

from ..typing import PixelArray

Expand All @@ -33,11 +33,11 @@

def __init__(
self,
file_writer_class=SceneFileWriter,
camera_class=None,
skip_animations=False,
**kwargs,
):
file_writer_class: type[SceneFileWriter] = SceneFileWriter,
camera_class: type[Camera] | None = None,
skip_animations: bool = False,
**kwargs: Any,
) -> None:
# All of the following are set to EITHER the value passed via kwargs,
# OR the value stored in the global config dict at the time of
# _instance construction_.
Expand All @@ -51,7 +51,7 @@
self.time = 0
self.static_image = None

def init_scene(self, scene):
def init_scene(self, scene: Scene) -> None:
self.file_writer: Any = self._file_writer_class(
self,
scene.__class__.__name__,
Expand Down Expand Up @@ -119,12 +119,12 @@

def update_frame( # TODO Description in Docstring
self,
scene,
scene: Scene,
mobjects: typing.Iterable[Mobject] | None = None,
include_submobjects: bool = True,
ignore_skipping: bool = True,
**kwargs,
):
**kwargs: Any,
) -> None:
"""Update the frame.

Parameters
Expand Down Expand Up @@ -263,7 +263,7 @@
self.skip_animations = True
raise EndSceneEarlyException()

def scene_finished(self, scene):
def scene_finished(self, scene: Scene) -> None:
# If no animations in scene, render an image instead
if self.num_plays:
self.file_writer.finish()
Expand Down
49 changes: 33 additions & 16 deletions manim/renderer/opengl_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import itertools as it
import time
from functools import cached_property
from typing import Any
from typing import TYPE_CHECKING, Any

import moderngl
import numpy as np
from PIL import Image

from manim import config, logger
from manim.mobject.opengl.opengl_mobject import OpenGLMobject, OpenGLPoint
from manim.mobject.opengl.opengl_mobject import (
OpenGLMobject,
OpenGLPoint,
_AnimationBuilder,
)
Comment on lines +14 to +18

Check notice

Code scanning / CodeQL

Cyclic import Note

Import of module
manim.mobject.opengl.opengl_mobject
begins an import cycle.
from manim.mobject.opengl.opengl_vectorized_mobject import OpenGLVMobject
from manim.utils.caching import handle_caching_play
from manim.utils.color import color_to_rgba
Expand All @@ -34,7 +38,16 @@
render_opengl_vectorized_mobject_fill,
render_opengl_vectorized_mobject_stroke,
)

if TYPE_CHECKING:

Check failure

Code scanning / CodeQL

Module-level cyclic import Error

'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Scene' may not be defined if module
manim.scene.scene
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Scene occurs after the cyclic
import
of manim.renderer.opengl_renderer.

Check failure

Code scanning / CodeQL

Module-level cyclic import Error

'Mobject' may not be defined if module
manim.mobject.mobject
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Mobject occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Mobject' may not be defined if module
manim.mobject.mobject
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Mobject occurs after the cyclic
import
of manim.renderer.opengl_renderer.
import numpy.typing as npt
from typing_extensions import Self

from manim.animation.animation import Animation

Check failure

Code scanning / CodeQL

Module-level cyclic import Error

'Animation' may not be defined if module
manim.animation.animation
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Animation occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Animation' may not be defined if module
manim.animation.animation
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Animation occurs after the cyclic
import
of manim.renderer.opengl_renderer.
'Animation' may not be defined if module
manim.animation.animation
is imported before module
manim.renderer.opengl_renderer
, as the
definition
of Animation occurs after the cyclic
import
of manim.renderer.opengl_renderer.
from manim.mobject.mobject import Mobject
from manim.scene.scene import Scene


__all__ = ["OpenGLCamera", "OpenGLRenderer"]


Expand Down Expand Up @@ -102,7 +115,7 @@
self.euler_angles = euler_angles
self.refresh_rotation_matrix()

def get_position(self):
def get_position(self) -> npt.NDArray:
return self.model_matrix[:, 3][:3]

def set_position(self, position):
Expand All @@ -123,7 +136,7 @@
self.set_height(self.frame_shape[1], stretch=True)
self.move_to(self.center_point)

def to_default_state(self):
def to_default_state(self) -> Self:
self.center()
self.set_height(config["frame_height"])
self.set_width(config["frame_width"])
Expand Down Expand Up @@ -166,28 +179,28 @@
self.refresh_rotation_matrix()
return self

def set_theta(self, theta):
def set_theta(self, theta: float) -> Self:
return self.set_euler_angles(theta=theta)

def set_phi(self, phi):
def set_phi(self, phi: float) -> Self:
return self.set_euler_angles(phi=phi)

def set_gamma(self, gamma):
def set_gamma(self, gamma: float) -> Self:
return self.set_euler_angles(gamma=gamma)

def increment_theta(self, dtheta):
def increment_theta(self, dtheta: float) -> Self:
self.euler_angles[0] += dtheta
self.refresh_rotation_matrix()
return self

def increment_phi(self, dphi):
def increment_phi(self, dphi: float) -> Self:
phi = self.euler_angles[1]
new_phi = clip(phi + dphi, -PI / 2, PI / 2)
self.euler_angles[1] = new_phi
self.refresh_rotation_matrix()
return self

def increment_gamma(self, dgamma):
def increment_gamma(self, dgamma: float) -> Self:
self.euler_angles[2] += dgamma
self.refresh_rotation_matrix()
return self
Expand All @@ -199,15 +212,15 @@
# Assumes first point is at the center
return self.points[0]

def get_width(self):
def get_width(self) -> float:
points = self.points
return points[2, 0] - points[1, 0]

def get_height(self):
def get_height(self) -> float:
points = self.points
return points[4, 1] - points[3, 1]

def get_focal_distance(self):
def get_focal_distance(self) -> float:
return self.focal_distance * self.get_height()

def interpolate(self, *args, **kwargs):
Expand Down Expand Up @@ -412,7 +425,9 @@
raise EndSceneEarlyException()

@handle_caching_play
def play(self, scene, *args, **kwargs):
def play(
self, scene: Scene, *args: Animation | Mobject | _AnimationBuilder, **kwargs
):
# TODO: Handle data locking / unlocking.
self.animation_start_time = time.time()
self.file_writer.begin_animation(not self.skip_animations)
Expand Down Expand Up @@ -440,11 +455,13 @@
self.time += scene.duration
self.num_plays += 1

def clear_screen(self):
def clear_screen(self) -> None:
self.frame_buffer_object.clear(*self.background_color)
self.window.swap_buffers()

def render(self, scene, frame_offset, moving_mobjects):
def render(
self, scene: Scene, frame_offset, moving_mobjects: list[Mobject]
) -> None:
self.update_frame(scene)

if self.skip_animations:
Expand Down
Loading
Loading