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 45 commits into
base: main
Choose a base branch
from

Conversation

henrikmidtiby
Copy link
Contributor

Overview: What does this pull request change?

The pull request add type annotations to the files manim/scene/*.py and some of its dependencies.
The request is still a work in progress, as mypy currently lists 90 errors.
With the pull request I hope to get some feedback and advise on how to proceed with the work.

The pull request is a continuation of the work in #3961

Motivation and Explanation: Why and how do your changes improve the library?

Type annotations help identify errors during development.

Further Information and Comments

I would suggest to make some changes to certain default values.
https://github.com/henrikmidtiby/manim/blob/6c137ee0f9f7b414bd5c1e0b6657eeaab005e088/manim/scene/scene.py#L128-L131
https://github.com/henrikmidtiby/manim/blob/6c137ee0f9f7b414bd5c1e0b6657eeaab005e088/manim/scene/scene.py#L119-L122
https://github.com/henrikmidtiby/manim/blob/6c137ee0f9f7b414bd5c1e0b6657eeaab005e088/manim/scene/scene.py#L119-L122

A change related to the background of a new Camera instance.
https://github.com/henrikmidtiby/manim/blob/6c137ee0f9f7b414bd5c1e0b6657eeaab005e088/manim/scene/vector_space_scene.py#L133-L135

It appears as the property target_text is not really used for something.
https://github.com/henrikmidtiby/manim/blob/6c137ee0f9f7b414bd5c1e0b6657eeaab005e088/manim/scene/vector_space_scene.py#L1117-L1122

In some cases I have chosen to insert assertions about the type of certain data.
See e.g. here. Is this a suitable approach?
https://github.com/henrikmidtiby/manim/blob/6c137ee0f9f7b414bd5c1e0b6657eeaab005e088/manim/scene/scene.py#L1333

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

Error count: 267
…era is the OpenGLCamera

Error count: 225 -> 182
Error count: 182 -> 167
…rtain methods.

This is mainly related to interactive elements and the 3D camera used in the ThreeDScene

Error count: 167 -> 143
Error count: 143 -> 143
Error count: 112 -> 102
Error count: 102 -> 97
Error count: 97 -> 90
@@ -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.
@@ -11,6 +11,8 @@


from .. import __version__, config
from ..renderer.cairo_renderer import CairoRenderer
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.
if TYPE_CHECKING:
from typing_extensions import Self

from manim.mobject.mobject import Mobject

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.
from typing_extensions import Self

from manim.mobject.mobject import Mobject
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.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.
from ..constants import *
from ..gui.gui import configure_pygui
from ..renderer.cairo_renderer import CairoRenderer
from ..renderer.opengl_renderer import OpenGLRenderer
from ..renderer.opengl_renderer import OpenGLCamera, OpenGLRenderer

Check failure

Code scanning / CodeQL

Module-level cyclic import Error

'OpenGLCamera' may not be defined if module
manim.renderer.opengl_renderer
is imported before module
manim.scene.scene
, as the
definition
of OpenGLCamera occurs after the cyclic
import
of manim.scene.scene.
'OpenGLCamera' may not be defined if module
manim.renderer.opengl_renderer
is imported before module
manim.scene.scene
, as the
definition
of OpenGLCamera occurs after the cyclic
import
of manim.scene.scene.
'OpenGLCamera' may not be defined if module
manim.renderer.opengl_renderer
is imported before module
manim.scene.scene
, as the
definition
of OpenGLCamera occurs after the cyclic
import
of manim.scene.scene.
'OpenGLCamera' may not be defined if module
manim.renderer.opengl_renderer
is imported before module
manim.scene.scene
, as the
definition
of OpenGLCamera occurs after the cyclic
import
of manim.scene.scene.
num_families = sum((mobject in family) for family in families)
return num_families == 1

return list(filter(is_top_level, self.mobjects))

def get_mobject_family_members(self):
def get_mobject_family_members(self) -> list[Mobject]:

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
@@ -493,7 +514,7 @@
self.add(mob)
curr_mobjects += mob.get_family()

def remove(self, *mobjects: Mobject):
def remove(self, *mobjects: Mobject) -> Self:

Check notice

Code scanning / CodeQL

Explicit returns mixed with implicit (fall through) returns Note

Mixing implicit and explicit returns may indicate an error as implicit returns always return None.
if not self.check_interactive_embed_is_valid():
return
self.interactive_mode = True

def ipython(shell, namespace):
def ipython(shell: InteractiveShellEmbed, namespace: dict[str, Any]) -> None:

Check failure

Code scanning / CodeQL

Potentially uninitialized local variable Error

Local variable 'InteractiveShellEmbed' may be used before it is initialized.
from .section import DefaultSectionType, Section

if TYPE_CHECKING:
from manim.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.scene.scene_file_writer
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.scene.scene_file_writer.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.scene.scene_file_writer
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.scene.scene_file_writer.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.scene.scene_file_writer
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.scene.scene_file_writer.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.scene.scene_file_writer
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.scene.scene_file_writer.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.scene.scene_file_writer
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.scene.scene_file_writer.
'CairoRenderer' may not be defined if module
manim.renderer.cairo_renderer
is imported before module
manim.scene.scene_file_writer
, as the
definition
of CairoRenderer occurs after the cyclic
import
of manim.scene.scene_file_writer.
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from manim.mobject.mobject import Mobject

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.utils.family_ops
, as the
definition
of Mobject occurs after the cyclic
import
of manim.utils.family_ops.
'Mobject' may not be defined if module
manim.mobject.mobject
is imported before module
manim.utils.family_ops
, as the
definition
of Mobject occurs after the cyclic
import
of manim.utils.family_ops.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

Successfully merging this pull request may close these issues.

1 participant