Skip to content

Internal Typing File for Type Aliases #2052

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion docs/source/contributing/typings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ When adding type hints to manim, there are some guidelines that should be follow

.. code:: py

rate_func: Callable[[float], float] = lambda t: smooth(1 - t)
int_func: Callable[[np.ndarray], np.ndarray] = np.floor


* Assuming that typical path objects are either Paths or strs, one can use the typehint ``typing.Union[str, pathlib.Path]``
Expand Down
11 changes: 6 additions & 5 deletions manim/animation/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

if TYPE_CHECKING:
from manim.scene.scene import Scene
from manim.utils.internal_types import rate_function


DEFAULT_ANIMATION_RUN_TIME: float = 1.0
Expand Down Expand Up @@ -118,15 +119,15 @@ def __init__(
mobject: Optional[Mobject],
lag_ratio: float = DEFAULT_ANIMATION_LAG_RATIO,
run_time: float = DEFAULT_ANIMATION_RUN_TIME,
rate_func: Callable[[float], float] = smooth,
rate_func: "rate_function" = smooth,
name: str = None,
remover: bool = False, # remove a mobject from the screen?
suspend_mobject_updating: bool = True,
**kwargs,
) -> None:
self._typecheck_input(mobject)
self.run_time: float = run_time
self.rate_func: Callable[[float], float] = rate_func
self.rate_func: "rate_function" = rate_func
self.name: Optional[str] = name
self.remover: bool = remover
self.suspend_mobject_updating: bool = suspend_mobject_updating
Expand Down Expand Up @@ -368,7 +369,7 @@ def get_run_time(self) -> float:

def set_rate_func(
self,
rate_func: Callable[[float], float],
rate_func: "rate_function",
Copy link
Member

@Darylgolden Darylgolden Nov 28, 2021

Choose a reason for hiding this comment

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

Should this be in quotes?

) -> "Animation":
"""Set the rate function of the animation.

Expand All @@ -388,12 +389,12 @@ def set_rate_func(

def get_rate_func(
self,
) -> Callable[[float], float]:
) -> "rate_function":
"""Get the rate function of the animation.

Returns
-------
Callable[[float], float]
rate_function
The rate function of the animation.
"""
return self.rate_func
Expand Down
7 changes: 3 additions & 4 deletions manim/animation/composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
from ..utils.rate_functions import linear

if TYPE_CHECKING:
from ..mobject.types.opengl_vectorized_mobject import OpenGLVGroup
from ..mobject.types.vectorized_mobject import VGroup
from ..utils.internal_types import manim_group, rate_function

__all__ = ["AnimationGroup", "Succession", "LaggedStart", "LaggedStartMap"]

Expand All @@ -27,9 +26,9 @@ class AnimationGroup(Animation):
def __init__(
self,
*animations: Animation,
group: Union[Group, "VGroup", OpenGLGroup, "OpenGLVGroup"] = None,
group: "manim_group" = None,
run_time: Optional[float] = None,
rate_func: Callable[[float], float] = linear,
rate_func: "rate_function" = linear,
lag_ratio: float = 0,
**kwargs
) -> None:
Expand Down
11 changes: 6 additions & 5 deletions manim/animation/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def construct(self):

if TYPE_CHECKING:
from manim.mobject.svg.text_mobject import Text
from manim.utils.internal_types import rate_function

from ..animation.animation import Animation
from ..animation.composition import Succession
Expand Down Expand Up @@ -193,7 +194,7 @@ def construct(self):
def __init__(
self,
mobject: Union[VMobject, OpenGLVMobject],
rate_func: Callable[[float], float] = lambda t: smooth(1 - t),
rate_func: "rate_function" = lambda t: smooth(1 - t),
remover: bool = True,
**kwargs,
) -> None:
Expand All @@ -216,7 +217,7 @@ def __init__(
self,
vmobject: Union[VMobject, OpenGLVMobject],
run_time: float = 2,
rate_func: Callable[[float], float] = double_smooth,
rate_func: "rate_function" = double_smooth,
stroke_width: float = 2,
stroke_color: str = None,
draw_border_animation_config: Dict = {}, # what does this dict accept?
Expand Down Expand Up @@ -294,7 +295,7 @@ def construct(self):
def __init__(
self,
vmobject: Union[VMobject, OpenGLVMobject],
rate_func: Callable[[float], float] = linear,
rate_func: "rate_function" = linear,
reverse: bool = False,
**kwargs,
) -> None:
Expand Down Expand Up @@ -375,7 +376,7 @@ def construct(self):
def __init__(
self,
vmobject: VMobject,
rate_func: Callable[[float], float] = linear,
rate_func: "rate_function" = linear,
reverse: bool = True,
**kwargs,
) -> None:
Expand Down Expand Up @@ -457,7 +458,7 @@ def __init__(
text: "Text",
suspend_mobject_updating: bool = False,
int_func: Callable[[np.ndarray], np.ndarray] = np.ceil,
rate_func: Callable[[float], float] = linear,
rate_func: "rate_function" = linear,
time_per_char: float = 0.1,
run_time: Optional[float] = None,
**kwargs,
Expand Down
2 changes: 1 addition & 1 deletion manim/animation/indication.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def __init__(
mobject: "Mobject",
direction: np.ndarray = UP,
amplitude: float = 0.2,
wave_func: Callable[[float], float] = smooth,
wave_func: "rate_function" = smooth,
time_width: float = 1,
ripples: int = 1,
run_time: float = 2,
Expand Down
3 changes: 2 additions & 1 deletion manim/animation/movement.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

if TYPE_CHECKING:
from ..mobject.mobject import Mobject
from ..utils.internal_types import rate_function


class Homotopy(Animation):
Expand Down Expand Up @@ -91,7 +92,7 @@ def __init__(
mobject: "Mobject",
virtual_time: float = 1,
suspend_mobject_updating: bool = False,
rate_func: Callable[[float], float] = linear,
rate_func: "rate_function" = linear,
**kwargs
) -> None:
self.virtual_time = virtual_time
Expand Down
6 changes: 5 additions & 1 deletion manim/animation/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@


import typing
from typing import TYPE_CHECKING

from ..animation.animation import Animation
from ..mobject.numbers import DecimalNumber
from ..utils.bezier import interpolate

if TYPE_CHECKING:
from ..utils.internal_types import rate_function


class ChangingDecimal(Animation):
def __init__(
self,
decimal_mob: DecimalNumber,
number_update_func: typing.Callable[[float], float],
number_update_func: "rate_function",
suspend_mobject_updating: typing.Optional[bool] = False,
**kwargs,
) -> None:
Expand Down
3 changes: 2 additions & 1 deletion manim/animation/rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

if typing.TYPE_CHECKING:
from ..mobject.mobject import Mobject
from ..utils.internal_types import rate_function


class Rotating(Animation):
Expand All @@ -25,7 +26,7 @@ def __init__(
about_point: Optional[np.ndarray] = None,
about_edge: Optional[np.ndarray] = None,
run_time: float = 5,
rate_func: Callable[[float], float] = linear,
rate_func: "rate_function" = linear,
**kwargs
) -> None:
self.axis = axis
Expand Down
9 changes: 6 additions & 3 deletions manim/camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ..mobject.types.point_cloud_mobject import PMobject
from ..mobject.types.vectorized_mobject import VMobject
from ..utils.color import color_to_int_rgba
from ..utils.deprecation import deprecated
from ..utils.family import extract_mobject_family_members
from ..utils.images import get_full_raster_image_path
from ..utils.iterables import list_difference_update
Expand Down Expand Up @@ -461,9 +462,8 @@ def is_in_frame(self, mobject):
],
)

def capture_mobject(
self, mobject, **kwargs
): # TODO Write better docstrings for this method.
@deprecated(since="v11", until="v13", replacement="capture_mobjects")
def capture_mobject(self, mobject, **kwargs):
return self.capture_mobjects([mobject], **kwargs)

def capture_mobjects(self, mobjects, **kwargs):
Expand Down Expand Up @@ -492,6 +492,9 @@ def capture_mobjects(self, mobjects, **kwargs):
# VMobject], [PMobject, PMobject], and [VMobject]. This must be done
# without altering their order. it.groupby computes exactly this
# partition while at the same time preserving order.
if isinstance(mobjects, Mobject):
mobjects = [mobjects]

mobjects = self.get_mobjects_to_display(mobjects, **kwargs)
for group_type, group in it.groupby(mobjects, self.type_or_raise):
self.display_funcs[group_type](list(group), self.pixel_array)
Expand Down
17 changes: 15 additions & 2 deletions manim/mobject/coordinate_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@

import fractions as fr
import numbers
from typing import Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
from typing import (
TYPE_CHECKING,
Callable,
Dict,
Iterable,
List,
Optional,
Sequence,
Tuple,
Union,
)

import numpy as np
from colour import Color
Expand Down Expand Up @@ -57,6 +67,9 @@
from ..utils.simple_functions import binary_search
from ..utils.space_ops import angle_of_vector

if TYPE_CHECKING:
from ..utils.internal_types import rate_function


class CoordinateSystem:
r"""
Expand Down Expand Up @@ -607,7 +620,7 @@ def construct(self):

def get_graph(
self,
function: Callable[[float], float],
function: "rate_function",
x_range: Optional[Sequence[float]] = None,
**kwargs,
):
Expand Down
2 changes: 1 addition & 1 deletion manim/mobject/mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def get_image(self, camera=None):
from ..camera.camera import Camera

camera = Camera()
camera.capture_mobject(self)
camera.capture_mobjects(self)
return camera.get_image()

def show(self, camera=None):
Expand Down
11 changes: 7 additions & 4 deletions manim/mobject/vector_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import itertools as it
import random
from math import ceil, floor
from typing import Callable, Iterable, Optional, Sequence, Tuple, Type
from typing import TYPE_CHECKING, Callable, Iterable, Optional, Sequence, Tuple, Type

import numpy as np
from colour import Color
Expand All @@ -31,6 +31,9 @@
from ..utils.simple_functions import sigmoid
from .types.opengl_vectorized_mobject import OpenGLVMobject

if TYPE_CHECKING:
from ..utils.internal_types import rate_function

DEFAULT_SCALAR_FIELD_COLORS: list = [BLUE_E, GREEN, YELLOW, RED]


Expand Down Expand Up @@ -531,7 +534,7 @@ def __init__(
z_range: Sequence[float] = None,
three_dimensions: bool = False, # Automatically True if z_range is set
# Takes in actual norm, spits out displayed norm
length_func: Callable[[float], float] = lambda norm: 0.45 * sigmoid(norm),
length_func: "rate_function" = lambda norm: 0.45 * sigmoid(norm),
opacity: float = 1.0,
vector_config: Optional[dict] = None,
**kwargs
Expand Down Expand Up @@ -855,7 +858,7 @@ def outside_box(p):
def create(
self,
lag_ratio: Optional[float] = None,
run_time: Optional[Callable[[float], float]] = None,
run_time: Optional["rate_function"] = None,
**kwargs
) -> AnimationGroup:
"""The creation animation of the stream lines.
Expand Down Expand Up @@ -913,7 +916,7 @@ def start_animation(
warm_up=True,
flow_speed: float = 1,
time_width: float = 0.3,
rate_func: Callable[[float], float] = linear,
rate_func: "rate_function" = linear,
line_animation_class: Type[ShowPassingFlash] = ShowPassingFlash,
**kwargs
) -> None:
Expand Down
12 changes: 12 additions & 0 deletions manim/utils/internal_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from typing import TYPE_CHECKING, Callable, Union

if TYPE_CHECKING:
from ..mobject.mobject import Group, Mobject
from ..mobject.opengl_mobject import OpenGLGroup
from ..mobject.types.opengl_vectorized_mobject import OpenGLVGroup
from ..mobject.types.vectorized_mobject import VGroup

manim_group = Union["Group", "VGroup", "OpenGLGroup", "OpenGLVGroup"]
rate_function = Callable[[float], float]

__all__ = ["manim_group", "rate_function"]
11 changes: 7 additions & 4 deletions manim/utils/rate_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ def construct(self):
from ..utils.bezier import bezier
from ..utils.simple_functions import sigmoid

if typing.TYPE_CHECKING:
from internal_types import rate_function


# This is a decorator that makes sure any function it's used on will
# return 0 if t<0 and 1 if t>1.
Expand Down Expand Up @@ -202,9 +205,9 @@ def running_start(


def not_quite_there(
func: typing.Callable[[float], float] = smooth,
func: "rate_function" = smooth,
proportion: float = 0.7,
) -> typing.Callable[[float], float]:
) -> "rate_function":
def result(t):
return proportion * func(t)

Expand All @@ -217,10 +220,10 @@ def wiggle(t: float, wiggles: float = 2) -> float:


def squish_rate_func(
func: typing.Callable[[float], float],
func: "rate_function",
a: float = 0.4,
b: float = 0.6,
) -> typing.Callable[[float], float]:
) -> "rate_function":
def result(t):
if a == b:
return a
Expand Down
Loading