From b0f584786c46f96745d781359940ba855e73e03f Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Mon, 1 Jan 2024 17:50:39 -0500 Subject: [PATCH 1/4] Fix typing of FunctionOverride --- manim/animation/animation.py | 6 +++--- manim/typing.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/manim/animation/animation.py b/manim/animation/animation.py index cb8fcaddfe..d9b0bbc57a 100644 --- a/manim/animation/animation.py +++ b/manim/animation/animation.py @@ -16,7 +16,7 @@ from copy import deepcopy -from typing import TYPE_CHECKING, Callable, Iterable, Sequence +from typing import TYPE_CHECKING, Callable, Iterable, Self, Sequence, cast if TYPE_CHECKING: from manim.scene.scene import Scene @@ -112,7 +112,7 @@ def __new__( *args, use_override=True, **kwargs, - ): + ) -> Self: if isinstance(mobject, Mobject) and use_override: func = mobject.animation_override_for(cls) if func is not None: @@ -122,7 +122,7 @@ def __new__( f"{type(mobject).__name__} mobjects. use_override = False can " f" be used as keyword argument to prevent animation overriding.", ) - return anim + return cast(Self, anim) return super().__new__(cls) def __init__( diff --git a/manim/typing.py b/manim/typing.py index a6c3e2e1d5..f2eb3ae16d 100644 --- a/manim/typing.py +++ b/manim/typing.py @@ -561,7 +561,7 @@ # Due to current limitations # (see https://github.com/python/mypy/issues/14656 / 8263), # we don't specify the first argument type (Mobject). -FunctionOverride: TypeAlias = Callable[..., None] +FunctionOverride: TypeAlias = Callable """Function type returning an :class:`~.Animation` for the specified :class:`~.Mobject`. """ From 0aa472dc93ecc08fcf075d8d49532121faad2910 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Mon, 1 Jan 2024 18:02:55 -0500 Subject: [PATCH 2/4] Use cls instead of Self --- manim/animation/animation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/manim/animation/animation.py b/manim/animation/animation.py index d9b0bbc57a..99707dc9c5 100644 --- a/manim/animation/animation.py +++ b/manim/animation/animation.py @@ -16,7 +16,7 @@ from copy import deepcopy -from typing import TYPE_CHECKING, Callable, Iterable, Self, Sequence, cast +from typing import TYPE_CHECKING, Callable, Iterable, Sequence, cast if TYPE_CHECKING: from manim.scene.scene import Scene @@ -112,7 +112,7 @@ def __new__( *args, use_override=True, **kwargs, - ) -> Self: + ): if isinstance(mobject, Mobject) and use_override: func = mobject.animation_override_for(cls) if func is not None: @@ -122,7 +122,7 @@ def __new__( f"{type(mobject).__name__} mobjects. use_override = False can " f" be used as keyword argument to prevent animation overriding.", ) - return cast(Self, anim) + return cast(cls, anim) return super().__new__(cls) def __init__( From 595ceed419377d7ba76d80042bc4e4d4457d3d88 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Mon, 1 Jan 2024 18:17:43 -0500 Subject: [PATCH 3/4] Add comment --- manim/typing.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/manim/typing.py b/manim/typing.py index f2eb3ae16d..6f16bad905 100644 --- a/manim/typing.py +++ b/manim/typing.py @@ -561,6 +561,8 @@ # Due to current limitations # (see https://github.com/python/mypy/issues/14656 / 8263), # we don't specify the first argument type (Mobject). +# Nor are we able to specify the return type (Animation) since we cannot import +# that here. FunctionOverride: TypeAlias = Callable """Function type returning an :class:`~.Animation` for the specified :class:`~.Mobject`. From f0f06f0e0484c2c562789229261fe147008327bc Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 4 Jan 2024 13:06:40 -0500 Subject: [PATCH 4/4] Use Self from typing_extensions --- manim/animation/animation.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/manim/animation/animation.py b/manim/animation/animation.py index 99707dc9c5..90290eee3e 100644 --- a/manim/animation/animation.py +++ b/manim/animation/animation.py @@ -16,7 +16,9 @@ from copy import deepcopy -from typing import TYPE_CHECKING, Callable, Iterable, Sequence, cast +from typing import TYPE_CHECKING, Callable, Iterable, Sequence + +from typing_extensions import Self if TYPE_CHECKING: from manim.scene.scene import Scene @@ -112,7 +114,7 @@ def __new__( *args, use_override=True, **kwargs, - ): + ) -> Self: if isinstance(mobject, Mobject) and use_override: func = mobject.animation_override_for(cls) if func is not None: @@ -122,7 +124,7 @@ def __new__( f"{type(mobject).__name__} mobjects. use_override = False can " f" be used as keyword argument to prevent animation overriding.", ) - return cast(cls, anim) + return anim return super().__new__(cls) def __init__(