-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Type Annotations: Fixing errors showing up in static type checking tool mypy #1241
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
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
802d36c
Type hints in animations and rate_functions
689eb8d
Type Signatures on most rate functions
b9bf896
A rate_function should just be float -> float
e2226d9
Merge branch 'master' into dev
cobordism e65444a
Merge branch 'master' of github.com:ManimCommunity/manim into dev
68a32f5
black?
2bedb01
`self.mobject` is of type `Union[mobject, None]`
b6f1a1b
Typing Corrections in composition.py
9e8f2e3
chasing down more type errors for transformations
2ada98e
mobject.copy needs better typing
a18f695
mobject.copy preserves derived type
9699d8b
Every Animation has a mobject, even Wait
451d603
Typing errors in creation.py
eddf942
Typing errors in creation.py
7a16d44
quick fix Wait to work with GL test
98e183e
isort
bc54b95
Merge branch 'master' into dev
45155f8
Merge branch 'master' into dev
cobordism 72c3033
import Callable from Typying to make test for python <3.9 work again
838dbb2
Merge branch 'dev' of github.com:cobordism/manim into dev
a20c81d
import Callable from Typying to make test for python <3.9 work again
6f8ac0d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7f6ce48
to make flake8 happy
c494180
Merge branch 'dev' of github.com:cobordism/manim into dev
ac52947
rate functions take float to float
7cdfee6
re-added begin to DrawBorderTheenFill
7c37ea4
Merge branch 'master' into dev
d3640d2
Merge branch 'master' into dev
cobordism 27b78b6
Merge branch 'master' into dev
cobordism 2700ea5
Merge branch 'master' into dev
cobordism c02f25a
Merge branch 'master' into dev
cobordism 4515e41
Merge branch 'master' into dev
cobordism be6832e
Merge branch 'master' into dev
jsonvillanueva 90eef30
Merge branch 'master' into dev
cobordism f1d28ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5e48caf
Merge branch 'master' into dev
kolibril13 98579c9
Merge branch 'master' into dev
kolibril13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,10 @@ | |
| __all__ = ["Animation", "Wait"] | ||
|
|
||
|
|
||
| import typing | ||
| from copy import deepcopy | ||
| from typing import Union | ||
| from typing import TYPE_CHECKING, Callable, Iterable, Optional, Tuple, Union | ||
|
|
||
| import numpy as np | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| if TYPE_CHECKING: | ||
| from manim.scene.scene import Scene | ||
|
|
||
| from .. import logger | ||
|
|
@@ -26,29 +23,29 @@ | |
| class Animation: | ||
| def __init__( | ||
| self, | ||
| mobject: Mobject, | ||
| mobject: Union[Mobject, None], | ||
| # If lag_ratio is 0, the animation is applied to all submobjects | ||
| # at the same time | ||
| # If 1, it is applied to each successively. | ||
| # If 0 < lag_ratio < 1, its applied to each | ||
| # with lagged start times | ||
| lag_ratio: float = DEFAULT_ANIMATION_LAG_RATIO, | ||
| run_time: float = DEFAULT_ANIMATION_RUN_TIME, | ||
| rate_func: typing.Callable[[float, float], np.ndarray] = smooth, | ||
| rate_func: Callable[[float], float] = 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 = run_time | ||
| self.rate_func = rate_func | ||
| self.name = name | ||
| self.remover = remover | ||
| self.suspend_mobject_updating = suspend_mobject_updating | ||
| self.lag_ratio = lag_ratio | ||
| self.starting_mobject = None | ||
| self.mobject = mobject | ||
| self.run_time: float = run_time | ||
naveen521kk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.rate_func: Callable[[float], float] = rate_func | ||
| self.name: Optional[str] = name | ||
| self.remover: bool = remover | ||
| self.suspend_mobject_updating: bool = suspend_mobject_updating | ||
| self.lag_ratio: float = lag_ratio | ||
| self.starting_mobject: Mobject = Mobject() | ||
| self.mobject: Mobject = mobject if mobject is not None else Mobject() | ||
| if kwargs: | ||
| logger.debug("Animation received extra kwargs: %s", kwargs) | ||
|
|
||
|
|
@@ -60,9 +57,9 @@ def __init__( | |
| ) | ||
| ) | ||
|
|
||
| def _typecheck_input(self, mobject: Mobject) -> None: | ||
| def _typecheck_input(self, mobject: Union[Mobject, None]) -> None: | ||
kolibril13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if mobject is None: | ||
| logger.debug("creating dummy animation") | ||
| logger.debug("Animation with empty mobject") | ||
| elif not isinstance(mobject, Mobject) and not isinstance( | ||
| mobject, OpenGLMobject | ||
| ): | ||
|
|
@@ -94,7 +91,7 @@ def begin(self) -> None: | |
|
|
||
| def finish(self) -> None: | ||
| self.interpolate(1) | ||
| if self.suspend_mobject_updating: | ||
| if self.suspend_mobject_updating and self.mobject is not None: | ||
| self.mobject.resume_updating() | ||
|
|
||
| def clean_up_from_scene(self, scene: "Scene") -> None: | ||
|
|
@@ -105,13 +102,13 @@ def create_starting_mobject(self) -> Mobject: | |
| # Keep track of where the mobject starts | ||
| return self.mobject.copy() | ||
|
|
||
| def get_all_mobjects(self) -> typing.Tuple[Mobject, typing.Union[Mobject, None]]: | ||
| def get_all_mobjects(self) -> Tuple[Mobject, Mobject]: | ||
| """ | ||
| Ordering must match the ordering of arguments to interpolate_submobject | ||
| """ | ||
| return self.mobject, self.starting_mobject | ||
|
|
||
| def get_all_families_zipped(self) -> typing.Iterator[typing.Tuple]: | ||
| def get_all_families_zipped(self) -> Iterable[Tuple]: | ||
| return zip( | ||
| *[mob.family_members_with_points() for mob in self.get_all_mobjects()] | ||
| ) | ||
|
|
@@ -138,7 +135,7 @@ def copy(self) -> "Animation": | |
|
|
||
| # Methods for interpolation, the mean of an Animation | ||
| def interpolate(self, alpha: float) -> None: | ||
| alpha = np.clip(alpha, 0, 1) | ||
| alpha = min(max(alpha, 0), 1) | ||
jsonvillanueva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.interpolate_mobject(self.rate_func(alpha)) | ||
|
|
||
| def update(self, alpha: float) -> None: | ||
|
|
@@ -159,20 +156,24 @@ def interpolate_mobject(self, alpha: float) -> None: | |
| self.interpolate_submobject(*mobs, sub_alpha) | ||
|
|
||
| def interpolate_submobject( | ||
| self, submobject: Mobject, starting_submobject: Mobject, alpha: float | ||
| ) -> None: | ||
| self, | ||
| submobject: Mobject, | ||
| starting_submobject: Mobject, | ||
| # target_copy: Mobject, #Todo: fix - signature of interpolate_submobject differes in Transform(). | ||
| alpha: float, | ||
| ) -> "Animation": | ||
| # Typically implemented by subclass | ||
| pass | ||
|
|
||
| def get_sub_alpha(self, alpha: float, index: int, num_submobjects: int): | ||
| def get_sub_alpha(self, alpha: float, index: int, num_submobjects: int) -> float: | ||
| # TODO, make this more understandable, and/or combine | ||
| # its functionality with AnimationGroup's method | ||
| # build_animations_with_timings | ||
| lag_ratio = self.lag_ratio | ||
| full_length = (num_submobjects - 1) * lag_ratio + 1 | ||
| value = alpha * full_length | ||
| lower = index * lag_ratio | ||
| return np.clip((value - lower), 0, 1) | ||
| return min(max((value - lower), 0), 1) | ||
|
|
||
| # Getters and setters | ||
| def set_run_time(self, run_time: float) -> "Animation": | ||
|
|
@@ -183,12 +184,15 @@ def get_run_time(self) -> float: | |
| return self.run_time | ||
|
|
||
| def set_rate_func( | ||
| self, rate_func: typing.Callable[[float, float], np.ndarray] | ||
| self, | ||
| rate_func: Callable[[float], float], | ||
| ) -> "Animation": | ||
| self.rate_func = rate_func | ||
| return self | ||
|
|
||
| def get_rate_func(self) -> typing.Callable[[float, float], np.ndarray]: | ||
| def get_rate_func( | ||
| self, | ||
| ) -> Callable[[float], float]: | ||
| return self.rate_func | ||
|
|
||
| def set_name(self, name: str) -> "Animation": | ||
|
|
@@ -198,9 +202,6 @@ def set_name(self, name: str) -> "Animation": | |
| def is_remover(self) -> bool: | ||
| return self.remover | ||
|
|
||
| def is_dummy(self) -> bool: | ||
| return self.mobject is None | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really not needed imo. |
||
|
|
||
|
|
||
| def prepare_animation( | ||
| anim: Union["Animation", "mobject._AnimationBuilder"] | ||
|
|
@@ -245,13 +246,14 @@ def prepare_animation( | |
|
|
||
| class Wait(Animation): | ||
| def __init__( | ||
| self, duration: float = 1, stop_condition=None, **kwargs | ||
| self, run_time: float = 1, stop_condition=None, **kwargs | ||
| ): # what is stop_condition? | ||
| self.duration = duration | ||
| self.mobject = None | ||
| self.duration: float = run_time | ||
| self.stop_condition = stop_condition | ||
| self.is_static_wait = False | ||
| super().__init__(None, **kwargs) | ||
| self.is_static_wait: bool = False | ||
| super().__init__(None, run_time=run_time, **kwargs) | ||
| # quick fix to work in opengl setting: | ||
| self.mobject.shader_wrapper_list = [] | ||
|
|
||
| def begin(self) -> None: | ||
| pass | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From https://docs.python.org/3/library/typing.html#typing.Union: