Skip to content

Commit f8e3408

Browse files
Fix some typehints in mobject.py (#3668)
* refactor(mobject): fix some typehints * Move typing_extensions import under `if TYPE_CHECKING` * Change from using `def animate(self: T ,...) -> T` to `def animate(self, ...) -> Self` as stated in PEP 673 * Fix incorrect usage of `T` in a method * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * move updaters type alias into TYPE_CHECKING * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 679b89a commit f8e3408

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

manim/animation/speedmodifier.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
import inspect
66
import types
7-
from typing import Callable
7+
from typing import TYPE_CHECKING, Callable
88

99
from numpy import piecewise
1010

1111
from ..animation.animation import Animation, Wait, prepare_animation
1212
from ..animation.composition import AnimationGroup
13-
from ..mobject.mobject import Mobject, Updater, _AnimationBuilder
13+
from ..mobject.mobject import Mobject, _AnimationBuilder
1414
from ..scene.scene import Scene
1515

16+
if TYPE_CHECKING:
17+
from ..mobject.mobject import Updater
18+
1619
__all__ = ["ChangeSpeed"]
1720

1821

manim/mobject/mobject.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
import warnings
1717
from functools import partialmethod, reduce
1818
from pathlib import Path
19-
from typing import TYPE_CHECKING, Callable, Iterable, Literal, TypeVar, Union
19+
from typing import TYPE_CHECKING, Callable, Iterable, Literal
2020

2121
import numpy as np
22-
from typing_extensions import Self, TypeAlias
2322

2423
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
2524

@@ -39,14 +38,9 @@
3938
from ..utils.paths import straight_path
4039
from ..utils.space_ops import angle_between_vectors, normalize, rotation_matrix
4140

42-
# TODO: Explain array_attrs
43-
44-
TimeBasedUpdater: TypeAlias = Callable[["Mobject", float], None]
45-
NonTimeBasedUpdater: TypeAlias = Callable[["Mobject"], None]
46-
Updater: TypeAlias = Union[NonTimeBasedUpdater, TimeBasedUpdater]
47-
T = TypeVar("T", bound="Mobject")
48-
4941
if TYPE_CHECKING:
42+
from typing_extensions import Self, TypeAlias
43+
5044
from manim.typing import (
5145
FunctionOverride,
5246
Image,
@@ -61,6 +55,10 @@
6155

6256
from ..animation.animation import Animation
6357

58+
TimeBasedUpdater: TypeAlias = Callable[["Mobject", float], object]
59+
NonTimeBasedUpdater: TypeAlias = Callable[["Mobject"], object]
60+
Updater: TypeAlias = NonTimeBasedUpdater | TimeBasedUpdater
61+
6462

6563
class Mobject:
6664
"""Mathematical Object: base class for objects that can be displayed on screen.
@@ -237,7 +235,7 @@ def construct(self):
237235
cls.__init__ = cls._original__init__
238236

239237
@property
240-
def animate(self: T) -> _AnimationBuilder | T:
238+
def animate(self) -> _AnimationBuilder | Self:
241239
"""Used to animate the application of any method of :code:`self`.
242240
243241
Any method called on :code:`animate` is converted to an animation of applying
@@ -2926,7 +2924,7 @@ def set_z_index(
29262924
self,
29272925
z_index_value: float,
29282926
family: bool = True,
2929-
) -> T:
2927+
) -> Self:
29302928
"""Sets the :class:`~.Mobject`'s :attr:`z_index` to the value specified in `z_index_value`.
29312929
29322930
Parameters

0 commit comments

Comments
 (0)