-
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
Conversation
manim/animation/animation.py
Outdated
| 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: typing.Callable[[float], float] = smooth, |
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.
is typing.Callable Deprecated? https://docs.python.org/3/library/typing.html#typing.Callable
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.
ah I see. but only since 3.9.
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.
and it wasn't I who put it in there...
I suppose we could remove the import typing line, change this line to just Callable[[float].... and add from collections.abc import Callable.
Would that be the thing to do?
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.
I tried adding it from collections.abc but of course our CI and our readthedocs failed because they try using python3.7 and 3.8. Let's keep importing from typing for now.
manim/animation/animation.py
Outdated
| self, | ||
| submobject: Mobject, | ||
| starting_submobject: Mobject, | ||
| # target_copy: Mobject, |
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.
Here is an issue that the signature of the interpolate_submobject method is different in Animation than in Transform. It needs cleaning up.. but if I add this line here uncommented, then a bunch of test fail.
| return self.remover | ||
|
|
||
| def is_dummy(self) -> bool: | ||
| return self.mobject is None |
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.
This is really not needed imo.
| def __init__( | ||
| self, method: types.MethodType, *args, **kwargs | ||
| ) -> None: # method typing? for args? | ||
| ) -> None: # method typing (we want to specify Mobject method)? for args? |
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.
I have more information about this. types.methodType is an abstract method, and not a method bound to an object. We need the type for a bound method which is a Callable[...] and has a __self__ of type mobject.
However it looks like it might not be possible to do this correctly
https://stackoverflow.com/questions/64225522/python-typing-how-to-type-hint-a-variable-as-a-bound-method
https://stackoverflow.com/questions/58085648/mypy-type-annotation-for-a-bound-method
|
I just realised that this PR was from my dev branch and I've been pushing too it constantly. The theme of this PR is - fix many - but not all - errors coming from mypy. |
for more information, see https://pre-commit.ci
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.
Looks mostly good although I have a few concerns:
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.
LGTM
|
I managed merge conflict in web editor. I hope it's OK. It's always hard to satisfy black and flake while working in web edit mode... Anyway, since there is clearly so much more to do on type hints, I hope we can just merge this one now and then continue working on the issue... |
|
@cobordism : can you wait with merging until #1338 is a bit more formulated? |
Oh, you dont' have to satisfy black anymore, that does pre-commit.ci for you ;) |
| def __init__( | ||
| self, | ||
| mobject: Mobject, | ||
| mobject: Union[Mobject, None], |
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:
You can use Optional[X] as a shorthand for Union[X, None].
| return vmobject.get_color() | ||
|
|
||
| def get_all_mobjects(self) -> typing.List[typing.Union[Mobject, None]]: | ||
| def get_all_mobjects(self) -> List[Union[Mobject, None]]: |
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.
Also here : Optional[Mobject]
do you think that helps? Rather than merging and then continuing the work? After all, the type hints still require a lot of work overall... and at least this PR makes a start at reducing mypy errors. On the whole, I'm somewhat ambivalent. It's up to you. |
Let's merge it then. |
In light of some of the issues I raise in #1212, I have started a process of going through all mypy errors and trying to fix them.
This PR started by adding some Type Annotations to
animations.pyand a few torate_functionsand a few more places... I was following the mypy output on error at a time, skipping over harder ones and solving the simple ones.Changelog / Overview
I have added and corrected typing hints in
animations.pyandrate_functions.pyI have removed the use ofnp.clipin favour or min and max, so that a rate function is justfloat -> floatas expected (and notUnion[float, numpy.ndarray])I have added a
TypeVariabletoMobject.copyso that derived objects (likeGroup) are preserved by copying.I continued into
composition.py,transform.py,creation.pypicking off the low hanging fruit. Mostly a change in type hints, but also a few minor refactors. Prime example: in theWriteanimation,self.run_timeandself.lag_ratiowere being set in the__init__function and then set again in the parent__init__function. I cleaned that up. Each variable should have only one level in the hierarchy where it is set/written during__init__.I have changed the
Wait()animation so that it has aMobjectattached to it - albeit an emptyMobject(). This allows us to tag the first argument toAnimation()asMobjectinstead ofOptional[Mobject]. Keeping it as the latter would have required a myriad of checks of the formif animation.mobject is not None: animation.mobject.method(...)and that's not too nice.There is a slight hickup in that a
GLMobjectis not (yet) aMobjectand using theWaitAnimation in the GL context causes an error. I have found an ugly quick fix for this that I hope will become unnecessary soon asGLMobjects mature.Motivation
This fixes a bunch of errors found by running the
mypytype checker. See Issue #1212. I think we should have everything correctly typed, and no typing errors shown bymypyExplanation for Changes
Animations will not crash when given a None type object.
AnimationGroups can contain aWait()animation.Testing Status
All tests pass locally. No new tests added.
Further Comments
Try running
mypy ./before and after this PR to see the difference.Before:
mypy
Found 538 errors in 90 filesAfter:
mypy
Found 491 errors in 90 files... there is plenty more to clean up.
Checklist
Reviewer Checklist