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

Add type annotations to manim.utils.* #3999

Merged
merged 52 commits into from
Jan 2, 2025
Merged
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
45d84fd
Handled mypy issues in utils/bezier.py
henrikmidtiby Dec 28, 2024
c2a715a
Disable mypy errors in manim.utils.*
henrikmidtiby Nov 4, 2024
40250de
Fix mypy errors in utils/unit.py
henrikmidtiby Nov 4, 2024
910aa3f
Handle mypy errors in utils/debug.py
henrikmidtiby Nov 4, 2024
8bf07ca
Fix mypy issues in utils.color.*
henrikmidtiby Nov 4, 2024
5d2ce00
Avoid circular import.
henrikmidtiby Nov 4, 2024
ec07a4f
Handle mypy errors in utils.simple_functions.*
henrikmidtiby Nov 5, 2024
bb3ec8a
Handle my errors in utils.testing.*
henrikmidtiby Nov 5, 2024
d6210c3
Avoid circular import.
henrikmidtiby Nov 5, 2024
21599b1
Handle mypy errors in utils/family_ops.py
henrikmidtiby Nov 6, 2024
4997a37
Handle mypy errors in utils/parameter_parsing.py
henrikmidtiby Nov 6, 2024
3be6129
Handle some of the mypy errors in utils.docbuild.*
henrikmidtiby Dec 28, 2024
3cabd9e
Handle mypy errors for utils/config_ops.py
henrikmidtiby Nov 6, 2024
29792df
Handle mypy errors from utils/commands.py
henrikmidtiby Nov 6, 2024
9f0be52
Handle mypy errors in utils/tex_templates.py
henrikmidtiby Nov 6, 2024
111775b
Handle mypy errors in utils/space_ops.py
henrikmidtiby Nov 6, 2024
459092e
Fixed most type errors in utils/rate_functions.py
henrikmidtiby Nov 8, 2024
be12a0f
Handle type errors in utils/paths.py
henrikmidtiby Nov 8, 2024
713a03e
Handled type errors in utils/deprecation.py
henrikmidtiby Nov 8, 2024
0970168
Handle type errors in utils/tex_file_writing.py
henrikmidtiby Nov 8, 2024
37d9071
Handle type error in utils/sounds.py
henrikmidtiby Nov 8, 2024
c1a154a
Handle type errors in utils/opengl.py
henrikmidtiby Nov 9, 2024
15e10b2
Handle type errors in utils/module_ops.py
henrikmidtiby Nov 9, 2024
1569831
Handle type errors in utils/caching.py, utils/familily.py and utils/i…
henrikmidtiby Nov 9, 2024
714b49a
Handle type errors in utils/file_ops.py
henrikmidtiby Nov 9, 2024
1b54065
Handle type errors in utils/ipython_magic.py
henrikmidtiby Nov 9, 2024
7bf0312
Update manim/utils/testing/_frames_testers.py
henrikmidtiby Dec 28, 2024
8e15d0c
Addressing comments from chopan50, block 1
henrikmidtiby Dec 28, 2024
36906b8
Addressing comments from chopan50, block 2
henrikmidtiby Dec 28, 2024
0bfe02f
Addressing comments from chopan50, block 3
henrikmidtiby Dec 28, 2024
7b8a529
Addressing comments from chopan50, block 4
henrikmidtiby Dec 28, 2024
67acc91
Addressing comments from chopan50, block 5
henrikmidtiby Dec 28, 2024
2a29354
Addressing comments from chopan50, block 7
henrikmidtiby Dec 28, 2024
a50dd1a
Addressing comments from chopan50, block 8
henrikmidtiby Dec 28, 2024
e055ba1
Addressing comments from chopan50, block 9
henrikmidtiby Dec 28, 2024
d5686d1
Addressing comments from chopan50, block 9
henrikmidtiby Dec 28, 2024
b6b1ff0
Addressing comments from chopan50, block 10
henrikmidtiby Jan 1, 2025
f3ed2b1
Addressing comments from chopan50, block 11
henrikmidtiby Jan 1, 2025
3a4bd5b
Addressing comments from chopan50, block 12
henrikmidtiby Jan 1, 2025
98ff7a8
Addressing comments from chopan50, block 13
henrikmidtiby Jan 1, 2025
44a8645
Addressing comments from chopan50, block 14
henrikmidtiby Jan 1, 2025
e1db9ea
Addressing comments from chopan50, block 15
henrikmidtiby Jan 1, 2025
de80832
Addressing comments from chopan50, block 16
henrikmidtiby Jan 1, 2025
0736393
Addressing comments from chopan50, block 17
henrikmidtiby Jan 1, 2025
711b2d8
Addressing comments from chopan50, block 18
henrikmidtiby Jan 1, 2025
3f0d9ac
Removed some more files from mypy.ini and fix remaining errors
chopan050 Jan 2, 2025
35e6597
Fix errors in rate_functions.py and be compatible with Python 3.9
chopan050 Jan 2, 2025
8cce30f
Remove incorrect use of typing.Concatenate from rate_functions.py
chopan050 Jan 2, 2025
3bb4f6b
ManimConfig dirs have always been strings - Fix manim_directive.py My…
chopan050 Jan 2, 2025
0436034
Fix StrList error in manim_directive.py
chopan050 Jan 2, 2025
3c5183c
Fix or ignore errors in manim.utils.docbuild
chopan050 Jan 2, 2025
6931d36
Reverse points in manim.utils.rate_functions.running_start
chopan050 Jan 2, 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
Prev Previous commit
Next Next commit
Handle type errors in utils/module_ops.py
henrikmidtiby committed Dec 28, 2024
commit 15e10b220f5ba7666b92cb0a8796dbd01bc2d259
31 changes: 19 additions & 12 deletions manim/utils/module_ops.py
Original file line number Diff line number Diff line change
@@ -7,14 +7,18 @@
import types
import warnings
from pathlib import Path
from typing import TYPE_CHECKING

from .. import config, console, constants, logger
from ..scene.scene_file_writer import SceneFileWriter

if TYPE_CHECKING:
from typing import Any

__all__ = ["scene_classes_from_file"]


def get_module(file_name: Path):
def get_module(file_name: Path) -> types.ModuleType:

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 str(file_name) == "-":
module = types.ModuleType("input_scenes")
logger.info(
@@ -47,19 +51,22 @@ def get_module(file_name: Path):
)

spec = importlib.util.spec_from_file_location(module_name, file_name)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
sys.path.insert(0, str(file_name.parent.absolute()))
spec.loader.exec_module(module)
return module
if isinstance(spec, importlib.machinery.ModuleSpec):
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
sys.path.insert(0, str(file_name.parent.absolute()))
assert spec.loader
spec.loader.exec_module(module)
return module
raise FileNotFoundError(f"{file_name} not found")
else:
raise FileNotFoundError(f"{file_name} not found")


def get_scene_classes_from_module(module):
def get_scene_classes_from_module(module: types.ModuleType) -> list[Any]:
from ..scene.scene import Scene

def is_child_scene(obj, module):
def is_child_scene(obj: Any, module: types.ModuleType) -> bool:
return (
inspect.isclass(obj)
and issubclass(obj, Scene)
@@ -73,7 +80,7 @@ def is_child_scene(obj, module):
]


def get_scenes_to_render(scene_classes):
def get_scenes_to_render(scene_classes: list[Any]) -> list[Any]:
henrikmidtiby marked this conversation as resolved.
Show resolved Hide resolved
if not scene_classes:
logger.error(constants.NO_SCENE_MESSAGE)
return []
@@ -97,7 +104,7 @@ def get_scenes_to_render(scene_classes):
return prompt_user_for_choice(scene_classes)


def prompt_user_for_choice(scene_classes):
def prompt_user_for_choice(scene_classes: list[Any]) -> list[Any]:
num_to_class = {}
henrikmidtiby marked this conversation as resolved.
Show resolved Hide resolved
SceneFileWriter.force_output_as_scene_name = True
for count, scene_class in enumerate(scene_classes, 1):
@@ -125,8 +132,8 @@ def prompt_user_for_choice(scene_classes):


def scene_classes_from_file(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one could be overloaded like this:

@overload
def scene_classes_from_file(
    file_path: Path, require_single_scene: bool, full_list: Literal[True]
) -> list[type[Scene]]: ...

@overload
def scene_classes_from_file(
    file_path: Path, require_single_scene: Literal[True], full_list: Literal[False] = False
) -> type[Scene]: ...

@overload
def scene_classes_from_file(
    file_path: Path, require_single_scene: Literal[False] = False, full_list: Literal[False] = False
) -> list[type[Scene]]: ...

def scene_classes_from_file(
    file_path: Path, require_single_scene: bool = False, full_list: bool = False
) -> type[Scene] | list[type[Scene]]:
    # the rest

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try the changes that you suggest in module_ops.py, I get the errors shown below.
I will leave it for now and hope to get some help in resolving it.

manim/utils/module_ops.py:95: error: "Scene" has no attribute "__name__"  [attr-defined]
manim/utils/module_ops.py:104: error: "Scene" has no attribute "__name__"  [attr-defined]
manim/utils/module_ops.py:113: error: "Scene" has no attribute "__name__"  [attr-defined]
manim/utils/module_ops.py:115: error: Incompatible types in assignment (expression has type "Scene", target has type "type[Scene]")  [assignment]
manim/utils/module_ops.py:121: error: List comprehension has incompatible type List[type[Scene]]; expected List[Scene]  [misc]
manim/utils/module_ops.py:124: error: "Scene" has no attribute "__name__"  [attr-defined]
manim/utils/module_ops.py:168: error: Incompatible return value type (got "Scene", expected "type[Scene] | list[type[Scene]]")  [return-value]
manim/utils/module_ops.py:169: error: Incompatible return value type (got "list[Scene]", expected "type[Scene] | list[type[Scene]]")  [return-value]
Found 8 errors in 1 file (checked 1 source file)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried myself, everything was OK.
Your errors suggest that you might have used Scene instead of type[Scene].

file_path: Path, require_single_scene=False, full_list=False
):
file_path: Path, require_single_scene: bool = False, full_list: bool = False
) -> list[Any] | Any:
module = get_module(file_path)
all_scene_classes = get_scene_classes_from_module(module)
if full_list:
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -106,6 +106,9 @@ ignore_errors = True
[mypy-manim.utils.family_ops.*]
ignore_errors = False

[mypy-manim.utils.module_ops.*]
ignore_errors = False

[mypy-manim.utils.opengl.*]
ignore_errors = False