Skip to content

Added type annotations to mobject/svg/brace.py #4309

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

henrikmidtiby
Copy link
Contributor

Overview: What does this pull request change?

Type annotations were added to the brace.py file.

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

Copy link
Contributor

@chopan050 chopan050 left a comment

Choose a reason for hiding this comment

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

Thanks! I just reviewed and left some suggestions:

Comment on lines 280 to 281
# TODO: This method is only called from the method change_brace_label, which is
# not called from any location in the current codebase.
Copy link
Contributor

Choose a reason for hiding this comment

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

Although it's not called anywhere, it seems like an useful method to be exposed to the end user. In that case, the real problem would be that it's not documented at all. This could be a good follow-up PR.

Suggested change
# TODO: This method is only called from the method change_brace_label, which is
# not called from any location in the current codebase.

@@ -238,7 +240,7 @@ def __init__(
font_size: float = DEFAULT_FONT_SIZE,
Copy link
Contributor

Choose a reason for hiding this comment

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

The label_constructor above could be typed as type[SingleStringMathTex | Text] to allow passing both TeX and Cairo-rendered texts. SingleStringMathTex is the parent of MathTex, which is the parent of Tex, which is the parent of BulletedList and Title.

Plus, the brace_direction could be typed as Point3DLike (in the future, Vector3DLike).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing the type of label_constructor raises this error

manim/mobject/svg/brace.py:279: error: Argument 1 to "SingleStringMathTex" has incompatible type "*tuple[str, ...]"; expected "float"  [arg-type]
manim/mobject/svg/brace.py:279: error: Argument 1 to "SingleStringMathTex" has incompatible type "*tuple[str, ...]"; expected "bool"  [arg-type]
manim/mobject/svg/brace.py:279: error: Argument 1 to "SingleStringMathTex" has incompatible type "*tuple[str, ...]"; expected "float | None"  [arg-type]
manim/mobject/svg/brace.py:279: error: Argument 1 to "SingleStringMathTex" has incompatible type "*tuple[str, ...]"; expected "TexTemplate | None"  [arg-type]
manim/mobject/svg/brace.py:279: error: Argument 1 to "Text" has incompatible type "*tuple[str, ...]"; expected "float"  [arg-type]
manim/mobject/svg/brace.py:279: error: Argument 1 to "Text" has incompatible type "*tuple[str, ...]"; expected "dict[str, str]"  [arg-type]
manim/mobject/svg/brace.py:279: error: Argument 1 to "Text" has incompatible type "*tuple[str, ...]"; expected "dict[str, tuple[Any, ...]]"  [arg-type]
manim/mobject/svg/brace.py:279: error: Argument 1 to "Text" has incompatible type "*tuple[str, ...]"; expected "tuple[Any, ...]"  [arg-type]
manim/mobject/svg/brace.py:279: error: Argument 1 to "Text" has incompatible type "*tuple[str, ...]"; expected "int"  [arg-type]
manim/mobject/svg/brace.py:279: error: Argument 1 to "Text" has incompatible type "*tuple[str, ...]"; expected "bool"  [arg-type]

@github-project-automation github-project-automation bot moved this from 🆕 New to 👀 In review in Dev Board Jun 25, 2025
from manim.utils.color.core import ParsableManimColor

__all__ = ["Brace", "BraceBetweenPoints", "BraceLabel", "ArcBrace"]

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 not change the value of the __all__ variable. This line made it impossible to use the BraceText object.

def __init__(self, obj, text, label_constructor=Tex, **kwargs):
def __init__(
self, obj: Mobject, text: str, label_constructor: type[SingleStringMathTex | Text] = Text, **kwargs: Any
):
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 would argue that the default label_constructor should be Text.

def change_label(self, *text, **kwargs):
self.label = self.label_constructor(*text, **kwargs)

def change_label(self, *text: str, **kwargs: Any) -> Self:
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 have created this small example to demonstrate the use of the change_label method, but apparently calling the method has no effect on the generated output.

from manim import *

class BraceExample(Scene):
    def construct(self):
        s1 = Square().move_to(2*LEFT)
        self.add(s1)
        br1 = BraceText(s1, "Label")
        self.add(br1)

        s2 = Square().move_to(2*RIGHT)
        self.add(s2)
        br2 = BraceText(s2, "Label")

        # I would expect this line to change the label
        # of the br2 brace to "new". But this is not seen 
        # in the output.
        br2.change_label("new")
        self.add(br2)

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

Successfully merging this pull request may close these issues.

2 participants