-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Draft: Add type annotations to mobject.py #2129
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
This adds quite a few type annotations to mobject.py (and a few to other files to help). Type checked with Pyright - MyPy basically can't handle this at all. There are quite a few type errors left but way fewer than when I started (Pyright gave up about half way through the file!) Some misc notes: * The code is not clear where `Sequence[float]` is allowed and where `np.ndarray` is (or either). I've mostly just put `np.ndarray`. * Similarly for `Color` vs `str`. Not clear where either is allowed and some of the existing type annotations seem to be wrong. * Changed `np.float64` to `float` for simplicity. They're officialy not compatible but they actually are the same. * Removed unused `axes` parameter from `rotate_about_origin`.
Just a comment about writing unions as |
This is similar to a pull I already made (but you did better), where I made a dedicated file for this. I think the changes should be combined: #2052 |
It seems to really not like your _typeshed |
|
||
import numpy as np | ||
|
||
# TODO(types): colour has no type annotations. |
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.
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 don't think so. This is about this colour not the Manim Color
type.
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.
Though you have just made me realise that I put Color
in loads of places where I think it should have been Colors
. I'll fix it.
color: Color | ColorStr | None | ||
name: str | ||
dim: int | ||
target: Mobject | None | ||
z_index: float | ||
point_hash: int | 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.
Like already mentioned, those are not supported yet. Use Union.
@@ -499,13 +541,13 @@ def remove(self, *mobjects: "Mobject") -> "Mobject": | |||
self.submobjects.remove(mobject) | |||
return self | |||
|
|||
def __sub__(self, other): | |||
def __sub__(self, other: Mobject) -> 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.
Lying about the return value
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.
Do you mean the return type is wrong? If so what should it be? It's pretty hard to tell in some cases!
I don't think you mean lying - "lying" means being deliberately deceptive and I hope you aren't accusing me of that!
raise NotImplementedError | ||
|
||
def __isub__(self, other): | ||
def __isub__(self, other: Mobject) -> 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.
Lying about the return value
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 similar to a pull I already made (but you did better), where I made a dedicated file for this. I think the changes should be combined: #2052
Nice, I had a look and I think our changes don't conflict much (if at all) so hopefully we can land them separately.
Updater = Union[Callable[["Mobject"], None], Callable[["Mobject", float], None]] | ||
# Any subclass of Mobject. Most Mobject methods `return self` so this is used | ||
# so that e.g. Circle.scale(2) returns a Circle, not an Mobject. | ||
MOS = TypeVar("MOS", bound="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.
Maybe a better name for this? I mean it's not needed. Maybe MOB instead of MOS
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.
"MOS" was meant to be "MObject Subclass"... Maybe that was not obvious - happy to change it! Why MOB? Just MOBject?
Actually I'm pretty sure
I think it doesn't work in some contexts, but Pyright seems to know the rules and give an error when you get it wrong. Do you have tests that run it with Python 3.7 just to be sure? |
Yeah so the thing you can't do until Python 3.10 (even with
But this isn't
Pyright warns you an error for the second case. |
…typeshed, remove accidental `Color`s
I mean look at our CI, we do have tests |
Alright, I was just asking. No need to get defensive! |
Hi! Are you still interested in working on this? |
I'll close this for now, please feel free to reopen if you intend to continue working on this! |
Hey, yeah sorry for not replying. I took this about as far as I could but to be honest didn't see a whole lot of interest and it really needs someone familiar with the code and with commit access to push it through. (It's often hard to divine types without knowing the intent of the code.) I'm going to try https://github.com/imaphatduc/cubecubed for my next project. Terrible name! But it's written in Typescript so hopefully it's easier to follow, and it outputs to the web which is cool. |
This is a draft - it's not quite finished yet; I just put it up for discussion (and in case I never finish it and somebody wants to take over). Or in case you just want to land it unfinished.
This adds quite a few type annotations to mobject.py (and a few to other files to help). Type checked with Pyright - MyPy basically can't handle this at all. There are quite a few type errors left but way fewer than when I started (Pyright gave up about half way through the file!)
Some misc notes:
Sequence[float]
is allowed and wherenp.ndarray
is (or either). I've mostly just putnp.ndarray
.Color
vsstr
. Not clear where either is allowed and some of the existing type annotations seem to be wrong.np.float64
tofloat
for simplicity. They're officialy not compatible but they actually are the same.axes
parameter fromrotate_about_origin
.Reviewer Checklist