Skip to content

Add type annotations for most of camera and mobject.graphing #4125

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

Merged

Conversation

henrikmidtiby
Copy link
Contributor

Overview: What does this pull request change?

Adding type annotations for files in mobject/graphing/.* and camera/.*

@henrikmidtiby henrikmidtiby force-pushed the Typing_graphing_and_camera branch from 6b8f27d to f767e8a Compare January 21, 2025 07:21
return self.point_to_coords(point)

@staticmethod
def _origin_shift(axis_range: Sequence[float]) -> float: ...

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.
@henrikmidtiby henrikmidtiby marked this pull request as ready for review January 21, 2025 07:27
@JasonGrace2282 JasonGrace2282 added the typehints For adding/discussing typehints label Feb 9, 2025
@henrikmidtiby henrikmidtiby marked this pull request as draft July 25, 2025 09:14
self.display_funcs: dict[
type[Mobject], Callable[[list[Mobject], PixelArray], Any]
] = {
VMobject: self.display_multiple_vectorized_mobjects, # type: ignore[dict-item]
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 ended up using a type ignore statement here.

Comment on lines +165 to +169
assert isinstance(label_mob, VMobject)
label_mobs.add(label_mob)
parts.braces = braces
parts.labels = label_mobs
parts.label_kwargs = {
parts.braces = braces # type: ignore[attr-defined]
parts.labels = label_mobs # type: ignore[attr-defined]
parts.label_kwargs = { # type: ignore[attr-defined]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here I chose to use an assert statement and a few type ignores.

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.

I left some suggestions I missed. Hopefully these will be the last 🙏🏼

def __init__(self, function, x_range=None, color=YELLOW, **kwargs):
def __init__(
self,
function: Callable[[float], Any],
Copy link
Contributor

Choose a reason for hiding this comment

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

It's probably fine for now that function returns Any instead of float for compatibility with the parent class' function attribute. In a subsequent PR, there should be a rewrite such that FunctionGraph.function is different from ParametricFunction.function.

tick_range,
unit_decimal_places=decimal_number_config["num_decimal_places"],
)

self.add_labels(
Copy link
Contributor

Choose a reason for hiding this comment

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

It's strange that this wasn't autocollapsed by pre-commit into a single line like this:

                self.add_labels(dict(zip(tick_range, custom_labels)))

Comment on lines +159 to +160
# TODO: I think the method should be able to return more than just a single point.
# E.g. see the implementation of it on line 2065.
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 think that this function (or rather its descendants) should be able to return more than a single point.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's actually correct. The Axes.coords_to_points() method I optimized in #3286 also uses that behavior. Thanks for reminding me of it.

This behavior is probably too complex to type for now, so let's leave the TODO there.

@henrikmidtiby
Copy link
Contributor Author

I took another look at the code and found a number of things that I wanted to change. See the last four commits.

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.

LGTM! Thanks a lot for these changes! It's a lot of progress for these modules.

@chopan050 chopan050 changed the title Typing graphing and camera Add type annotations for most of camera and mobject.graphing Aug 1, 2025
@github-project-automation github-project-automation bot moved this to 🆕 New in Dev Board Aug 1, 2025
@chopan050 chopan050 enabled auto-merge (squash) August 1, 2025 00:47
@@ -26,6 +28,12 @@
def __init__(self, custom_labels: bool = False):
self.custom_labels = custom_labels

@overload
def function(self, value: float) -> float: ...

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.
def function(self, value: float) -> float: ...

@overload
def function(self, value: np.ndarray) -> np.ndarray: ...

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.
@chopan050 chopan050 merged commit 7eb8856 into ManimCommunity:main Aug 1, 2025
21 checks passed
@github-project-automation github-project-automation bot moved this from 🆕 New to ✅ Done in Dev Board Aug 1, 2025
@henrikmidtiby henrikmidtiby deleted the Typing_graphing_and_camera branch August 1, 2025 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typehints For adding/discussing typehints
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants