Skip to content
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

Type annotations for manim/scene/*.py #3981

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
ba5c73a
Removed typing errors in mobject/geometry/shape_matchers.py
henrikmidtiby Oct 18, 2024
c6e071f
Added type annotations fo mobject/types/image_mobject.py and a few ot…
henrikmidtiby Oct 18, 2024
758e199
Adding type annotations to types/point_cloud_mobject.py
henrikmidtiby Oct 18, 2024
b54a624
Fixing issues revealed by type annotations in types/point_cloud_mobje…
henrikmidtiby Oct 18, 2024
bdf0049
Fix issue in the CI step related to the Camera class.
henrikmidtiby Oct 18, 2024
1962c73
Adding type annotations to geometry/line.py
henrikmidtiby Oct 18, 2024
92d9908
Ignore type errors related to super and sub class in line.py and shap…
henrikmidtiby Oct 19, 2024
bd4f02a
Dealing with some issues and ignoring others identified by type checking
henrikmidtiby Oct 19, 2024
a4bda5d
Ignored a number of type issues related to float and floating[Any].
henrikmidtiby Oct 20, 2024
45e18e8
Help mypy to determine types.
henrikmidtiby Oct 20, 2024
c29a38e
More work on addressing typing issues.
henrikmidtiby Oct 21, 2024
11e6ffa
Reverting two changes that triggers an error in the automatic testing.
henrikmidtiby Oct 21, 2024
935fd0c
Further work on type hinting.
henrikmidtiby Oct 21, 2024
00f546d
Avoid forwarding positional arguments from Arrow to Line in the const…
henrikmidtiby Oct 22, 2024
020a777
Revert "Avoid forwarding positional arguments from Arrow to Line in t…
henrikmidtiby Oct 22, 2024
ef6158a
Removed several type ignore statements and addressed comments from Ja…
henrikmidtiby Oct 22, 2024
e79fd75
Revert "Activate mypy check of mobject.geometry.*"
henrikmidtiby Oct 22, 2024
c85a19d
Revert "Removed several type ignore statements and addressed comments…
henrikmidtiby Oct 22, 2024
6243990
Added type annotations to zoomed_scene.py
henrikmidtiby Oct 22, 2024
ae06d89
Adding type annotations to all methods in vector_space_scene.py
henrikmidtiby Oct 22, 2024
8d6d9c4
Get rid of no-untyped-call errors from my in the vector_space_scene.p…
henrikmidtiby Oct 23, 2024
e2284b1
Handle type issues related to ManimColor in vector_space_scene.py
henrikmidtiby Oct 23, 2024
bfe61b0
Handle var-annotated issues in vector_space_scene.py
henrikmidtiby Oct 23, 2024
eae2296
Handling has-type type errors in vector_space_scene.py
henrikmidtiby Oct 23, 2024
9dd785a
Handled name-defined type issues in vector_space_scene.py
henrikmidtiby Oct 23, 2024
822dba2
Address type issue with calling an untyped method.
henrikmidtiby Oct 24, 2024
501d774
Fix some typing issues in transform_mathcing_parts.py
henrikmidtiby Oct 24, 2024
fa9cad9
Change stroke_width to float in vector_space_scene.py
henrikmidtiby Oct 24, 2024
99c12c0
Handled a few type errors.
henrikmidtiby Oct 24, 2024
dee6ef9
Handled several typing issues in three_d_scene.py
henrikmidtiby Oct 24, 2024
5bf2d0e
Dealing with type errors in scene_file_writer.py
henrikmidtiby Oct 24, 2024
da35261
Ensured that all methods in scene.py have type declarations.
henrikmidtiby Oct 25, 2024
79790db
Handle type issues related to interactivity by asserting that the cam…
henrikmidtiby Oct 25, 2024
080113f
Handle type issues in scene.py
henrikmidtiby Oct 25, 2024
abd7022
Asserting that the renderer or camera is of the proper type to use ce…
henrikmidtiby Oct 25, 2024
3fc6197
Avoid cyclic import of dependencies
henrikmidtiby Oct 26, 2024
c5d5743
Handling no-untyped-call type errors in manim/scene/scene.py
henrikmidtiby Oct 26, 2024
90de428
Handling assignment type errors in manim/scene/*.py
henrikmidtiby Oct 26, 2024
0a8c41e
Handling arg-type type errors in manim/scene/*.py
henrikmidtiby Oct 26, 2024
e8d9741
Handling arg-type type errors in manim/scene/*.py
henrikmidtiby Oct 26, 2024
059566e
Fixing various type errors
henrikmidtiby Oct 26, 2024
5bba091
Fixing various type errors
henrikmidtiby Oct 26, 2024
1165469
Fixing various type errors
henrikmidtiby Oct 26, 2024
4c14674
Some aggressive changes to silence a significant number of type errors.
henrikmidtiby Oct 28, 2024
bf7a82f
Commented out an import (IPython) that makes the CI tests fail.
henrikmidtiby Oct 28, 2024
a172b37
Fix various type errors.
henrikmidtiby Oct 29, 2024
21965d1
More type annotations.
henrikmidtiby Oct 29, 2024
4a31821
Code cleanup.
henrikmidtiby Oct 29, 2024
ab59045
Remove the property mobject_updater_lists of the Scene class as it is…
henrikmidtiby Oct 29, 2024
54cbe6c
Handle import-untyped typing issues.
henrikmidtiby Nov 3, 2024
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
24 changes: 12 additions & 12 deletions manim/mobject/geometry/arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,22 +273,22 @@ def get_first_handle(self) -> InternalPoint3D:
# Type inference of extracting an element from a list, is not
# supported by numpy, see this numpy issue
# https://github.com/numpy/numpy/issues/16544
return self.points[1]
return self.points[1] # type: ignore[no-any-return]

def get_last_handle(self) -> InternalPoint3D:
return self.points[-2]
return self.points[-2] # type: ignore[no-any-return]

def get_end(self) -> InternalPoint3D:
if self.has_tip():
return self.tip.get_start()
return self.tip.get_start() # type: ignore[return-value]
else:
return super().get_end()
return super().get_end() # type: ignore[return-value]

def get_start(self) -> InternalPoint3D:
if self.has_start_tip():
return self.start_tip.get_start()
return self.start_tip.get_start() # type: ignore[return-value]
else:
return super().get_start()
return super().get_start() # type: ignore[return-value]

def get_length(self) -> float:
start, end = self.get_start_and_end()
Expand Down Expand Up @@ -322,10 +322,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 = arc_center
self.start_angle = start_angle
self.angle = angle
self.num_components: int = num_components
self.arc_center: InternalPoint3D = arc_center
self.start_angle: float = start_angle
self.angle: float = angle
self._failed_to_get_center: bool = False
super().__init__(**kwargs)

Expand Down Expand Up @@ -484,7 +484,7 @@ def __init__(
if radius is None:
center = self.get_arc_center(warning=False)
if not self._failed_to_get_center:
temp_radius: float = np.linalg.norm(np.array(start) - np.array(center))
temp_radius: float = np.linalg.norm(np.array(start) - np.array(center)) # type: ignore[assignment]
self.radius = temp_radius
else:
self.radius = np.inf
Expand Down Expand Up @@ -661,7 +661,7 @@ def construct(self):
perpendicular_bisector([np.asarray(p1), np.asarray(p2)]),
perpendicular_bisector([np.asarray(p2), np.asarray(p3)]),
)
radius: float = np.linalg.norm(p1 - center)
radius: float = np.linalg.norm(p1 - center) # type: ignore[assignment]
return Circle(radius=radius, **kwargs).shift(center)


Expand Down
4 changes: 2 additions & 2 deletions manim/mobject/geometry/boolean_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING

import numpy as np
from pathops import Path as SkiaPath
from pathops import Path as SkiaPath # type: ignore[import-untyped]
from pathops import PathVerb, difference, intersection, union, xor

from manim import config
Expand Down Expand Up @@ -106,7 +106,7 @@ def _convert_vmobject_to_skia_path(self, vmobject: VMobject) -> SkiaPath:
for _p0, p1, p2, p3 in quads:
path.cubicTo(*p1[:2], *p2[:2], *p3[:2])

if vmobject.consider_points_equals_2d(subpath[0], subpath[-1]):
if vmobject.consider_points_equals_2d(subpath[0], subpath[-1]): # type: ignore[arg-type]
path.close()

return path
Expand Down
12 changes: 6 additions & 6 deletions manim/mobject/geometry/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ def _pointify(
if isinstance(mob_or_point, (Mobject, OpenGLMobject)):
mob = mob_or_point
if direction is None:
return mob.get_center()
return mob.get_center() # type: ignore[return-value]
else:
return mob.get_boundary_point(direction)
return mob.get_boundary_point(direction) # type: ignore[return-value]
return np.array(mob_or_point)

def set_path_arc(self, new_value: float) -> None:
Expand All @@ -156,8 +156,8 @@ def set_path_arc(self, new_value: float) -> None:

def put_start_and_end_on(
self,
start: InternalPoint3D,
end: InternalPoint3D,
start: InternalPoint3D, # type: ignore[override]
end: InternalPoint3D, # type: ignore[override]
) -> Self:
"""Sets starts and end coordinates of a line.

Expand Down Expand Up @@ -309,7 +309,7 @@ def get_start(self) -> InternalPoint3D:
array([-1., 0., 0.])
"""
if len(self.submobjects) > 0:
return self.submobjects[0].get_start()
return self.submobjects[0].get_start() # type: ignore[return-value]
else:
return super().get_start()

Expand All @@ -324,7 +324,7 @@ def get_end(self) -> InternalPoint3D:
array([1., 0., 0.])
"""
if len(self.submobjects) > 0:
return self.submobjects[-1].get_end()
return self.submobjects[-1].get_end() # type: ignore[return-value]
else:
return super().get_end()

Expand Down
6 changes: 3 additions & 3 deletions manim/mobject/geometry/polygram.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(
for vertices in vertex_groups:
# The inferred type for *vertices is Any, but it should be
# InternalPoint3D_Array
first_vertex, *vertices = vertices
first_vertex, *vertices = vertices # type: ignore[assignment]
first_vertex = np.array(first_vertex)

self.start_new_path(first_vertex)
Expand Down Expand Up @@ -322,7 +322,7 @@ def construct(self):
"""

def __init__(self, *vertices: InternalPoint3D, **kwargs: Any) -> None:
super().__init__(vertices, **kwargs)
super().__init__(vertices, **kwargs) # type: ignore[arg-type]


class RegularPolygram(Polygram):
Expand Down Expand Up @@ -413,7 +413,7 @@ def gen_polygon_vertices(start_angle: float | None) -> tuple[list[Any], float]:

vertex_groups.append(group)

super().__init__(*vertex_groups, **kwargs)
super().__init__(*vertex_groups, **kwargs) # type: ignore[arg-type]


class RegularPolygon(RegularPolygram):
Expand Down
2 changes: 1 addition & 1 deletion manim/mobject/geometry/tips.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def tip_point(self) -> InternalPoint3D:
# Type inference of extracting an element from a list, is not
# supported by numpy, see this numpy issue
# https://github.com/numpy/numpy/issues/16544
return self.points[0]
return self.points[0] # type: ignore[no-any-return]

@property
def vector(self) -> Vector3D:
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
Loading
Loading