Skip to content

Commit 6fb1845

Browse files
authored
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
1 parent 7787730 commit 6fb1845

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

manimlib/mobject/mobject.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def set_uniforms(self, uniforms: dict) -> Self:
160160
return self
161161

162162
@property
163-
def animate(self) -> _AnimationBuilder:
163+
def animate(self) -> _AnimationBuilder | Self:
164164
"""
165165
Methods called with Mobject.animate.method() can be passed
166166
into a Scene.play call, as if you were calling
@@ -2256,6 +2256,18 @@ def update_target(*method_args, **method_kwargs):
22562256
def __call__(self, **kwargs):
22572257
return self.set_anim_args(**kwargs)
22582258

2259+
def __dir__(self) -> list[str]:
2260+
"""
2261+
Extend attribute list of _AnimationBuilder object to include mobject attributes
2262+
for better autocompletion in the IPython terminal when using interactive mode.
2263+
"""
2264+
methods = super().__dir__()
2265+
mobject_methods = [
2266+
attr for attr in dir(self.mobject)
2267+
if not attr.startswith('_')
2268+
]
2269+
return sorted(set(methods+mobject_methods))
2270+
22592271
def set_anim_args(self, **kwargs):
22602272
'''
22612273
You can change the args of :class:`~manimlib.animation.transform.Transform`, such as

manimlib/typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import re
88

99
try:
10-
from typing import Self
11-
except ImportError:
1210
from typing_extensions import Self
11+
except ImportError:
12+
from typing import Self
1313

1414
# Abbreviations for a common types
1515
ManimColor = Union[str, Color, None]

0 commit comments

Comments
 (0)