manim icon indicating copy to clipboard operation
manim copied to clipboard

Add type annotations to `mobject.py`

Open henrikmidtiby opened this issue 5 months ago • 3 comments

Overview: What does this pull request change?

This PR add type annotations to mobject.py.

Issue #3375.

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

henrikmidtiby avatar Aug 10 '25 19:08 henrikmidtiby

One of the main contributions in this PR is that when accessing elements in a VGroup, the type of these elements are now recognized as VMobjects.

This is achieved by adding/modifying these methods including type annotations to the VMobject class.

def __iter__(self) -> Iterator[VMobject]:
    return iter(self.split())
def split(self) -> list[VMobject]:
    result: list[VMobject] = [self] if len(self.points) > 0 else []
    return result + self.submobjects

And these to the VGroup class.

def __getitem__(self, key: int) -> VMobject:
    return self.submobjects[key]

henrikmidtiby avatar Aug 11 '25 12:08 henrikmidtiby

@chopan050 Thanks for all the comments, even the minor nitpicks! I feel they help me improving the PR. I will get back to resolving the remaining suggestions in the coming days.

henrikmidtiby avatar Dec 04 '25 22:12 henrikmidtiby

The raise ValueError is now handled. Then I discovered a new issue, with the following example

from manim import *

# Scene derived from the example given in the documentation
class TableExamples(Scene):
    def construct(self):
        t2 = MathTable(
            [["+", 0, 5, 10],
            [0, 0, 5, 10],
            [2, 2, 7, 12],
            [4, 4, 9, 14]],
            include_outer_lines=True)

        for line in t2.get_horizontal_lines()[:3]:
            line.set_color(BLUE)

        t2.get_vertical_lines().set_color(BLUE)
        t2.get_vertical_lines()[0].set_color(BLUE)

        # AttributeError: 'list' object has no attribute 'set_color'
        t2.get_vertical_lines()[:3].set_color(BLUE)

I have attempted to solve the issue through implementing a __getitem__ method in the VGroup class. But apparently it does not work... Here is the used implementation.

    def __getitem__(self, item: int) -> Self:
        return self.submobjects[item]

henrikmidtiby avatar Dec 05 '25 08:12 henrikmidtiby