Skip to content

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

Closed
wants to merge 2 commits into from

Conversation

Timmmm
Copy link
Contributor

@Timmmm Timmmm commented Oct 1, 2021

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:

  • 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.

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

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`.
@Timmmm Timmmm mentioned this pull request Oct 1, 2021
@behackl behackl marked this pull request as draft October 1, 2021 23:59
@Darylgolden
Copy link
Member

Just a comment about writing unions as X | Y - since this is only going to be introduced with Python 3.10, and we're officially supporting Python 3.7+, you should use the old syntax.

@GameDungeon
Copy link
Contributor

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

@GameDungeon
Copy link
Contributor

It seems to really not like your _typeshed


import numpy as np

# TODO(types): colour has no type annotations.
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be fixed by either #1972 or #2050

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Comment on lines +96 to +101
color: Color | ColorStr | None
name: str
dim: int
target: Mobject | None
z_index: float
point_hash: int | None
Copy link
Contributor

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:
Copy link
Contributor

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

Copy link
Contributor Author

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:
Copy link
Contributor

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

Copy link
Contributor Author

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")
Copy link
Contributor

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

Copy link
Contributor Author

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?

@Timmmm
Copy link
Contributor Author

Timmmm commented Oct 2, 2021

Just a comment about writing unions as X | Y - since this is only going to be introduced with Python 3.10, and we're officially supporting Python 3.7+, you should use the old syntax.

Actually I'm pretty sure from __future__ import annotations takes care of that. I don't have Python 3.7 to hand but it works fine in Python 3.8:

~ python3
Python 3.8.7 (default, Jan  9 2021, 01:55:12) 
[Clang 9.0.0 (tags/RELEASE_900/final)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x: int | None = 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
>>> from __future__ import annotations
>>> x: int | None = 5
>>> x
5

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?

@Timmmm
Copy link
Contributor Author

Timmmm commented Oct 2, 2021

I think it doesn't work in some contexts

Yeah so the thing you can't do until Python 3.10 (even with from __future__ import annotations) is use | in a non type-hint context. In other words this is fine:

x: int | str = 5

But this isn't

IntOrStr = int | str

Pyright warns you an error for the second case.

@GameDungeon
Copy link
Contributor

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?

I mean look at our CI, we do have tests

@Timmmm
Copy link
Contributor Author

Timmmm commented Oct 2, 2021

I mean look at our CI, we do have tests

Alright, I was just asking. No need to get defensive!

@Darylgolden
Copy link
Member

Hi! Are you still interested in working on this?

@Darylgolden
Copy link
Member

I'll close this for now, please feel free to reopen if you intend to continue working on this!

@Timmmm
Copy link
Contributor Author

Timmmm commented Jan 22, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants