From 6fb1845f4a4dd1cd22519b0a10a99a56fc171619 Mon Sep 17 00:00:00 2001 From: Irvanal Haq <125118413+irvanalhaq9@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:13:29 +0700 Subject: [PATCH] Enhance Autocompletion for `mobject.animate.` to Display `Mobject` Methods (#2342) * Improve autocompletion for mobject.animate to show Mobject methods - Added type hint `-> _AnimationBuilder | Self` to `Mobject.animate`, enabling autocompletion for `Mobject` methods after `mobject.animate`. - Prioritized `typing_extensions.Self` over `typing.Self` in imports, so autocompletion of `Mobject` methods also works in Python < 3.11. * Support `mobject.animate.` autocompletion in IPython * Add docstring to `__dir__` and add return type hint * improve docsting `__dir__` _AnimationBuilder --- manimlib/mobject/mobject.py | 14 +++++++++++++- manimlib/typing.py | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/manimlib/mobject/mobject.py b/manimlib/mobject/mobject.py index 159c8508..cdc4bfc2 100644 --- a/manimlib/mobject/mobject.py +++ b/manimlib/mobject/mobject.py @@ -160,7 +160,7 @@ class Mobject(object): return self @property - def animate(self) -> _AnimationBuilder: + def animate(self) -> _AnimationBuilder | Self: """ Methods called with Mobject.animate.method() can be passed into a Scene.play call, as if you were calling @@ -2256,6 +2256,18 @@ class _AnimationBuilder: def __call__(self, **kwargs): return self.set_anim_args(**kwargs) + def __dir__(self) -> list[str]: + """ + Extend attribute list of _AnimationBuilder object to include mobject attributes + for better autocompletion in the IPython terminal when using interactive mode. + """ + methods = super().__dir__() + mobject_methods = [ + attr for attr in dir(self.mobject) + if not attr.startswith('_') + ] + return sorted(set(methods+mobject_methods)) + def set_anim_args(self, **kwargs): ''' You can change the args of :class:`~manimlib.animation.transform.Transform`, such as diff --git a/manimlib/typing.py b/manimlib/typing.py index c5c55729..9d51a5b5 100644 --- a/manimlib/typing.py +++ b/manimlib/typing.py @@ -7,9 +7,9 @@ if TYPE_CHECKING: import re try: - from typing import Self - except ImportError: from typing_extensions import Self + except ImportError: + from typing import Self # Abbreviations for a common types ManimColor = Union[str, Color, None]