Skip to content

Typing scene #4191

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 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions manim/animation/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
from ..utils.rate_functions import smooth, squish_rate_func

if TYPE_CHECKING:
from typing import Any

from ..scene.scene import Scene


Expand Down Expand Up @@ -516,7 +518,7 @@ def construct(self):

def __init__(
self,
function: types.MethodType,
function: Callable,
mobject: Mobject,
run_time: float = DEFAULT_POINTWISE_FUNCTION_RUN_TIME,
**kwargs,
Expand Down Expand Up @@ -615,7 +617,9 @@ def __init__(self, mobject: Mobject, **kwargs) -> None:


class ApplyFunction(Transform):
def __init__(self, function: types.MethodType, mobject: Mobject, **kwargs) -> None:
def __init__(
self, function: Callable[[Any], Any], mobject: Mobject, **kwargs: Any
) -> None:
self.function = function
super().__init__(mobject, **kwargs)

Expand Down
12 changes: 7 additions & 5 deletions manim/animation/transform_matching_parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from .transform import FadeTransformPieces, Transform

if TYPE_CHECKING:
from typing import Any

from ..scene.scene import Scene


Expand Down Expand Up @@ -74,7 +76,7 @@ def __init__(
transform_mismatches: bool = False,
fade_transform_mismatches: bool = False,
key_map: dict | None = None,
**kwargs,
**kwargs: Any,
):
if isinstance(mobject, OpenGLVMobject):
group_type = OpenGLVGroup
Expand Down Expand Up @@ -162,11 +164,11 @@ def clean_up_from_scene(self, scene: Scene) -> None:
scene.add(self.to_add)

@staticmethod
def get_mobject_parts(mobject: Mobject):
def get_mobject_parts(mobject: Mobject) -> None:
raise NotImplementedError("To be implemented in subclass.")

@staticmethod
def get_mobject_key(mobject: Mobject):
def get_mobject_key(mobject: Mobject) -> None:
raise NotImplementedError("To be implemented in subclass.")


Expand Down Expand Up @@ -206,7 +208,7 @@ def __init__(
transform_mismatches: bool = False,
fade_transform_mismatches: bool = False,
key_map: dict | None = None,
**kwargs,
**kwargs: Any,
):
super().__init__(
mobject,
Expand Down Expand Up @@ -269,7 +271,7 @@ def __init__(
transform_mismatches: bool = False,
fade_transform_mismatches: bool = False,
key_map: dict | None = None,
**kwargs,
**kwargs: Any,
):
super().__init__(
mobject,
Expand Down
4 changes: 3 additions & 1 deletion manim/camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ def init_background(self):
)
self.background[:, :] = background_rgba

def get_image(self, pixel_array: np.ndarray | list | tuple | None = None):
def get_image(
self, pixel_array: np.ndarray | list | tuple | None = None
) -> Image.Image:
"""Returns an image from the passed
pixel array, or from the current frame
if the passed pixel array is none.
Expand Down
21 changes: 14 additions & 7 deletions manim/camera/moving_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

__all__ = ["MovingCamera"]

from typing import TYPE_CHECKING

import numpy as np

from .. import config
Expand All @@ -19,6 +21,11 @@
from ..mobject.mobject import Mobject
from ..utils.color import WHITE

if TYPE_CHECKING:
from typing import Any

from manim.utils.color import ParsableManimColor


class MovingCamera(Camera):
"""
Expand All @@ -32,12 +39,12 @@ class MovingCamera(Camera):

def __init__(
self,
frame=None,
fixed_dimension=0, # width
default_frame_stroke_color=WHITE,
default_frame_stroke_width=0,
**kwargs,
):
frame: ScreenRectangle | None = None,
fixed_dimension: Any = 0, # width
default_frame_stroke_color: ParsableManimColor = WHITE,
default_frame_stroke_width: float = 0,
**kwargs: Any,
) -> None:
"""
Frame is a Mobject, (should almost certainly be a rectangle)
determining which region of space the camera displays
Expand Down Expand Up @@ -158,7 +165,7 @@ def cache_cairo_context(self, pixel_array, ctx):
# self.frame_shape = (self.frame.height, width)
# self.resize_frame_shape(fixed_dimension=self.fixed_dimension)

def get_mobjects_indicating_movement(self):
def get_mobjects_indicating_movement(self) -> list[ScreenRectangle]:
"""
Returns all mobjects whose movement implies that the camera
should think of all other mobjects on the screen as moving
Expand Down
6 changes: 4 additions & 2 deletions manim/camera/multi_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__all__ = ["MultiCamera"]


from manim.mobject.types.image_mobject import ImageMobject
from manim.mobject.types.image_mobject import ImageMobject, ImageMobjectFromCamera

from ..camera.moving_camera import MovingCamera
from ..utils.iterables import list_difference_update
Expand Down Expand Up @@ -38,7 +38,9 @@ def __init__(
)
super().__init__(**kwargs)

def add_image_mobject_from_camera(self, image_mobject_from_camera: ImageMobject):
def add_image_mobject_from_camera(
self, image_mobject_from_camera: ImageMobjectFromCamera
):
"""Adds an ImageMobject that's been obtained from the camera
into the list ``self.image_mobject_from_cameras``

Expand Down
2 changes: 1 addition & 1 deletion manim/camera/three_d_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def capture_mobjects(self, mobjects, **kwargs):
self.reset_rotation_matrix()
super().capture_mobjects(mobjects, **kwargs)

def get_value_trackers(self):
def get_value_trackers(self) -> list[ValueTracker]:
"""A list of :class:`ValueTrackers <.ValueTracker>` of phi, theta, focal_distance,
gamma and zoom.

Expand Down
6 changes: 5 additions & 1 deletion manim/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@


from .. import __version__, config
from ..renderer.cairo_renderer import CairoRenderer

Check failure

Code scanning / CodeQL

Module-level cyclic import Error

'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.gui.gui
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.gui.gui.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.gui.gui
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.gui.gui.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.gui.gui
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.gui.gui.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.gui.gui
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.gui.gui.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.gui.gui
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.gui.gui.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.gui.gui
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.gui.gui.
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.
from ..utils.module_ops import scene_classes_from_file

__all__ = ["configure_pygui"]
Expand All @@ -20,7 +22,9 @@
window = dpg.generate_uuid()


def configure_pygui(renderer, widgets, update=True):
def configure_pygui(
renderer: CairoRenderer | OpenGLRenderer, widgets, update: bool = True
) -> None:
if not dearpygui_imported:
raise RuntimeError("Attempted to use DearPyGUI when it isn't imported.")
if update:
Expand Down
8 changes: 4 additions & 4 deletions manim/mobject/geometry/arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,10 @@ def __init__(
if radius is None: # apparently None is passed by ArcBetweenPoints
radius = 1.0
self.radius = radius
self.num_components = num_components
self.arc_center: Point3D = np.asarray(arc_center)
self.start_angle = start_angle
self.angle = angle
self.num_components: int = num_components
self.arc_center: Point3D = arc_center
self.start_angle: float = start_angle
self.angle: float = angle
self._failed_to_get_center: bool = False
super().__init__(**kwargs)

Expand Down
2 changes: 1 addition & 1 deletion manim/mobject/geometry/polygram.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def construct(self):
self.add(isosceles, square_and_triangles)
"""

def __init__(self, *vertices: Point3DLike, **kwargs: Any) -> None:
def __init__(self, *vertices: Point3D, **kwargs: Any) -> None:
super().__init__(vertices, **kwargs)


Expand Down
6 changes: 3 additions & 3 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) -> VGroup:
"""Return the underlying mob matrix mobjects.

Returns
Expand All @@ -410,7 +410,7 @@ def get_mob_matrix(self):
"""
return self.mob_matrix

def get_entries(self):
def get_entries(self) -> VGroup:
"""Return the individual entries of the matrix.

Returns
Expand All @@ -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
20 changes: 14 additions & 6 deletions manim/mobject/text/tex_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from collections.abc import Iterable
from functools import reduce
from textwrap import dedent
from typing import TYPE_CHECKING

from manim import config, logger
from manim.constants import *
Expand All @@ -38,6 +39,9 @@
from manim.utils.tex import TexTemplate
from manim.utils.tex_file_writing import tex_to_svg_file

if TYPE_CHECKING:
from typing_extensions import Any

tex_string_to_mob_map = {}


Expand Down Expand Up @@ -206,7 +210,7 @@ def _organize_submobjects_left_to_right(self):
self.sort(lambda p: p[0])
return self

def get_tex_string(self):
def get_tex_string(self) -> str:
return self.tex_string

def init_colors(self, propagate_colors=True):
Expand Down Expand Up @@ -255,13 +259,13 @@ def construct(self):

def __init__(
self,
*tex_strings,
*tex_strings: str,
arg_separator: str = " ",
substrings_to_isolate: Iterable[str] | None = None,
tex_to_color_map: dict[str, ManimColor] = None,
tex_environment: str = "align*",
**kwargs,
):
**kwargs: Any,
) -> None:
self.tex_template = kwargs.pop("tex_template", config["tex_template"])
self.arg_separator = arg_separator
self.substrings_to_isolate = (
Expand Down Expand Up @@ -447,8 +451,12 @@ class Tex(MathTex):
"""

def __init__(
self, *tex_strings, arg_separator="", tex_environment="center", **kwargs
):
self,
*tex_strings: str,
arg_separator: str = "",
tex_environment: str = "center",
**kwargs: Any,
) -> None:
super().__init__(
*tex_strings,
arg_separator=arg_separator,
Expand Down
9 changes: 7 additions & 2 deletions manim/mobject/value_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
__all__ = ["ValueTracker", "ComplexValueTracker"]


from typing import TYPE_CHECKING

import numpy as np

from manim.mobject.mobject import Mobject
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
from manim.utils.paths import straight_path

if TYPE_CHECKING:
from typing_extensions import Any, Self


class ValueTracker(Mobject, metaclass=ConvertToOpenGL):
"""A mobject that can be used for tracking (real-valued) parameters.
Expand Down Expand Up @@ -69,7 +74,7 @@ def construct(self):

"""

def __init__(self, value=0, **kwargs):
def __init__(self, value: float = 0, **kwargs: Any) -> None:
super().__init__(**kwargs)
self.set(points=np.zeros((1, 3)))
self.set_value(value)
Expand All @@ -78,7 +83,7 @@ def get_value(self) -> float:
"""Get the current value of this ValueTracker."""
return self.points[0, 0]

def set_value(self, value: float):
def set_value(self, value: float) -> Self:
"""Sets a new scalar value to the ValueTracker"""
self.points[0, 0] = value
return self
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: 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
Loading
Loading