From 653a01e4f0ad26e208ed35bd623c7ee05ef3c45c Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Mon, 1 Apr 2024 10:42:08 -0400 Subject: [PATCH 1/4] 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 --- manim/mobject/mobject.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index ce5c971192..fb8e1d9065 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -16,10 +16,9 @@ import warnings from functools import partialmethod, reduce from pathlib import Path -from typing import TYPE_CHECKING, Callable, Iterable, Literal, TypeVar, Union +from typing import TYPE_CHECKING, Callable, Iterable, Literal import numpy as np -from typing_extensions import Self, TypeAlias from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL @@ -39,13 +38,6 @@ from ..utils.paths import straight_path from ..utils.space_ops import angle_between_vectors, normalize, rotation_matrix -# TODO: Explain array_attrs - -TimeBasedUpdater: TypeAlias = Callable[["Mobject", float], None] -NonTimeBasedUpdater: TypeAlias = Callable[["Mobject"], None] -Updater: TypeAlias = Union[NonTimeBasedUpdater, TimeBasedUpdater] -T = TypeVar("T", bound="Mobject") - if TYPE_CHECKING: from manim.typing import ( FunctionOverride, @@ -60,6 +52,11 @@ ) from ..animation.animation import Animation + from typing_extensions import Self, TypeAlias + +TimeBasedUpdater: TypeAlias = Callable[["Mobject", float], object] +NonTimeBasedUpdater: TypeAlias = Callable[["Mobject"], object] +Updater: TypeAlias = NonTimeBasedUpdater | TimeBasedUpdater class Mobject: @@ -237,7 +234,7 @@ def construct(self): cls.__init__ = cls._original__init__ @property - def animate(self: T) -> _AnimationBuilder | T: + def animate(self) -> _AnimationBuilder | Self: """Used to animate the application of any method of :code:`self`. Any method called on :code:`animate` is converted to an animation of applying @@ -2926,7 +2923,7 @@ def set_z_index( self, z_index_value: float, family: bool = True, - ) -> T: + ) -> Self: """Sets the :class:`~.Mobject`'s :attr:`z_index` to the value specified in `z_index_value`. Parameters From eeab7a950f03b0a1ad3cf2c3c5172d05806eafd3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Apr 2024 14:48:31 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/mobject/mobject.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index fb8e1d9065..1011e42e80 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -39,6 +39,8 @@ from ..utils.space_ops import angle_between_vectors, normalize, rotation_matrix if TYPE_CHECKING: + from typing_extensions import Self, TypeAlias + from manim.typing import ( FunctionOverride, Image, @@ -52,7 +54,6 @@ ) from ..animation.animation import Animation - from typing_extensions import Self, TypeAlias TimeBasedUpdater: TypeAlias = Callable[["Mobject", float], object] NonTimeBasedUpdater: TypeAlias = Callable[["Mobject"], object] From 1c4b35913fb0530ddc7a68248feb903c6da4f2d1 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Mon, 1 Apr 2024 13:33:38 -0400 Subject: [PATCH 3/4] move updaters type alias into TYPE_CHECKING --- manim/animation/speedmodifier.py | 7 +++++-- manim/mobject/mobject.py | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/manim/animation/speedmodifier.py b/manim/animation/speedmodifier.py index 9df1c9f018..30aab85adb 100644 --- a/manim/animation/speedmodifier.py +++ b/manim/animation/speedmodifier.py @@ -4,15 +4,18 @@ import inspect import types -from typing import Callable +from typing import Callable, TYPE_CHECKING from numpy import piecewise from ..animation.animation import Animation, Wait, prepare_animation from ..animation.composition import AnimationGroup -from ..mobject.mobject import Mobject, Updater, _AnimationBuilder +from ..mobject.mobject import Mobject, _AnimationBuilder from ..scene.scene import Scene +if TYPE_CHECKING: + from ..mobject.mobject import Updater + __all__ = ["ChangeSpeed"] diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py index 1011e42e80..73b1920ff6 100644 --- a/manim/mobject/mobject.py +++ b/manim/mobject/mobject.py @@ -55,9 +55,9 @@ from ..animation.animation import Animation -TimeBasedUpdater: TypeAlias = Callable[["Mobject", float], object] -NonTimeBasedUpdater: TypeAlias = Callable[["Mobject"], object] -Updater: TypeAlias = NonTimeBasedUpdater | TimeBasedUpdater + TimeBasedUpdater: TypeAlias = Callable[["Mobject", float], object] + NonTimeBasedUpdater: TypeAlias = Callable[["Mobject"], object] + Updater: TypeAlias = NonTimeBasedUpdater | TimeBasedUpdater class Mobject: From e47e617f70e24a11e547f60192a05cfc8d4451c2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Apr 2024 17:34:21 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- manim/animation/speedmodifier.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/animation/speedmodifier.py b/manim/animation/speedmodifier.py index 30aab85adb..63b9b2e5b3 100644 --- a/manim/animation/speedmodifier.py +++ b/manim/animation/speedmodifier.py @@ -4,7 +4,7 @@ import inspect import types -from typing import Callable, TYPE_CHECKING +from typing import TYPE_CHECKING, Callable from numpy import piecewise