Skip to content

Fix wrong value of MObject width/height with empty VGroup #4088

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 5 commits into
base: main
Choose a base branch
from

Conversation

gvauvert
Copy link

@gvauvert gvauvert commented Jan 2, 2025

Overview: What does this pull request change?

Fixes #4087

When a Mobject contains an empty VGroup, its width or height can be wrong

Motivation and Explanation: Why and how do your changes improve the library?

Bug fix + tests

Links to added or changed documentation pages

No documentation page, bug fix only.

Further Information and Comments

  • add test
  • fix bug
  • fix another test which result depends on the bug

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

@@ -136,21 +136,19 @@ def test_mobject_dimensions_nested_mobjects():


def test_mobject_dimensions_mobjects_with_no_points_are_at_origin():
Copy link
Author

Choose a reason for hiding this comment

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

As the previous test was testing an unexpected behavior, it should have been linked to an issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree.

Now, given that the unexpected behavior is fixed and empty Mobjects are no longer considered as "at the origin", it would be nice to change the name of this test in order to reflect that:

Suggested change
def test_mobject_dimensions_mobjects_with_no_points_are_at_origin():
def test_mobject_dimensions_mobjects_with_no_points():

@@ -2149,10 +2153,11 @@ def get_nadir(self) -> Point3D:

def length_over_dim(self, dim: int) -> float:
Copy link
Author

Choose a reason for hiding this comment

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

As this function is meant to be used only internally, it should be private

Suggested change
def length_over_dim(self, dim: int) -> float:
def __length_over_dim(self, dim: int) -> float:

or at least protected

Suggested change
def length_over_dim(self, dim: int) -> float:
def _length_over_dim(self, dim: int) -> float:

Copy link
Contributor

@chopan050 chopan050 Jul 25, 2025

Choose a reason for hiding this comment

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

It makes sense. In that case, .reduce_across_dimension() is an even stronger case of a method which should be marked as internal.

Now, I would probably not mark them as internal in this PR, because there's already a PR which proposes marking some methods as "internal". It's worth checking it and proposing to include these methods in that list:
#3793

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 for this fix! I left some suggestions:

Comment on lines +1982 to 1983
def reduce_across_dimension(self, reduce_func: Callable, dim: int) -> float | None:
"""Find the min or max value from a dimension across all points in this and submobjects."""
Copy link
Contributor

Choose a reason for hiding this comment

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

As this method can now also return None, it would be nice to document this new behavior in the docstring. Something along the lines of:

Suggested change
def reduce_across_dimension(self, reduce_func: Callable, dim: int) -> float | None:
"""Find the min or max value from a dimension across all points in this and submobjects."""
def reduce_across_dimension(self, reduce_func: Callable, dim: int) -> float | None:
"""Find the min or max value from a dimension across all points in this Mobject and its
submobjects. This allows for using :meth:`~.length_over_dim` to calculate its length over
a dimension, i.e. its height, width or depth. If this Mobject is empty, return ``None``,
since this Mobject should not be taken into account when calculating lengths.
Parameters
----------
reduce_func
The reducer function to use in order to calculate a value over a dimension.
dim
The dimension to use. It should be 0, 1 or 2, representing the X, Y or Z coordinate,
respectively.
Returns
-------
float | None
The min or max value over the dimension specified by ``dim``, or ``None`` if this
Mobject is empty.
"""

@@ -2149,10 +2153,11 @@ def get_nadir(self) -> Point3D:

def length_over_dim(self, dim: int) -> float:
Copy link
Contributor

@chopan050 chopan050 Jul 25, 2025

Choose a reason for hiding this comment

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

It makes sense. In that case, .reduce_across_dimension() is an even stronger case of a method which should be marked as internal.

Now, I would probably not mark them as internal in this PR, because there's already a PR which proposes marking some methods as "internal". It's worth checking it and proposing to include these methods in that list:
#3793

@@ -136,21 +136,19 @@ def test_mobject_dimensions_nested_mobjects():


def test_mobject_dimensions_mobjects_with_no_points_are_at_origin():
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree.

Now, given that the unexpected behavior is fixed and empty Mobjects are no longer considered as "at the origin", it would be nice to change the name of this test in order to reflect that:

Suggested change
def test_mobject_dimensions_mobjects_with_no_points_are_at_origin():
def test_mobject_dimensions_mobjects_with_no_points():

@github-project-automation github-project-automation bot moved this from 🆕 New to 👀 In review in Dev Board Jul 25, 2025
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.

Wrong result of Mobject.length_over_dim()
2 participants