manim icon indicating copy to clipboard operation
manim copied to clipboard

Type annotation work in manim/mobject/geometry/

Open henrikmidtiby opened this issue 1 year ago • 11 comments

Overview: What does this pull request change?

The pull request is meant as a first step towards getting the automatic type annotations (mypy) to work on the codebase.

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

I found this issue listed as as "good first issue" https://github.com/ManimCommunity/manim/issues/2121

This lead to this issue https://github.com/ManimCommunity/manim/issues/3375

Further Information and Comments

The main issue that I am struggling with at the moment, are type errors like the one listed below

manim/mobject/geometry/arc.py:180: error: Unsupported left operand type for - ("tuple[float, float, float]")  [operator]
manim/mobject/geometry/arc.py:180: note: Both left and right operands are unions

The issue is that mypy do not think that two Point3D objects can be subtracted from each other. This operation is performed in multiple locations of the codebase, e.g. line 180 in arc.py

angles = cartesian_to_spherical(handle - anchor)

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

henrikmidtiby avatar Oct 17 '24 20:10 henrikmidtiby

Hey thanks for helping out with the typing!

The issue is that mypy do not think that two Point3D objects can be subtracted from each other.

Most likely, you shouldn't be using the Point3D hint, but rather the InternalPoint3D type hint which only includes numpy arrays :)

JasonGrace2282 avatar Oct 18 '24 20:10 JasonGrace2282

Thanks for the tip about InternalPoint3D. It enabled me to reduce the number of errors reported by mypy in the mobject directory to 1707. For reference my first run of mypy yesterday listed 1921 errors.

My plan is to focus on getting rid of most of these errors:

  • Function is missing a type annotation for one or more arguments
  • Function is missing a return type annotation
  • Function is missing a type annotation

Now the type of errors reported by mypy for the files in mobject/geometry looks like shown below. This seems more difficult to handle. Any ideas of how to proceed?

$ mypy | egrep "(mobject/geometry/|Found)"
manim/mobject/geometry/tips.py:152: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]]"  [no-any-return]
manim/mobject/geometry/tips.py:233: error: Argument 1 to "scale" of "Mobject" has incompatible type "floating[Any]"; expected "float"  [arg-type]
manim/mobject/geometry/shape_matchers.py:116: error: Signature of "set_style" incompatible with supertype "VMobject"  [override]
manim/mobject/geometry/shape_matchers.py:116: note:      Superclass:
manim/mobject/geometry/shape_matchers.py:116: note:          def set_style(self, fill_color: ManimColor | int | str | tuple[int, int, int] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | ndarray[Any, dtype[signedinteger[_64Bit]]] | ndarray[Any, dtype[floating[_64Bit]]] | None = ..., fill_opacity: float | None = ..., stroke_color: ManimColor | int | str | tuple[int, int, int] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | ndarray[Any, dtype[signedinteger[_64Bit]]] | ndarray[Any, dtype[floating[_64Bit]]] | None = ..., stroke_width: float | None = ..., stroke_opacity: float | None = ..., background_stroke_color: ManimColor | int | str | tuple[int, int, int] | tuple[float, float, float] | tuple[int, int, int, int] | tuple[float, float, float, float] | ndarray[Any, dtype[signedinteger[_64Bit]]] | ndarray[Any, dtype[floating[_64Bit]]] | None = ..., background_stroke_width: float | None = ..., background_stroke_opacity: float | None = ..., sheen_factor: float | None = ..., sheen_direction: ndarray[Any, dtype[floating[_64Bit]]] | None = ..., background_image: Image | str | None = ..., family: bool = ...) -> BackgroundRectangle
manim/mobject/geometry/shape_matchers.py:116: note:      Subclass:
manim/mobject/geometry/shape_matchers.py:116: note:          def set_style(self, fill_opacity: float, **kwargs: Any) -> BackgroundRectangle
manim/mobject/geometry/shape_matchers.py:133: error: Returning Any from function declared to return "ManimColor"  [no-any-return]
manim/mobject/geometry/shape_matchers.py:133: error: Cannot determine type of "color"  [has-type]
manim/mobject/geometry/polygram.py:90: error: Incompatible types in assignment (expression has type "list[Any]", variable has type "ndarray[Any, dtype[floating[_64Bit]]]")  [assignment]
manim/mobject/geometry/polygram.py:117: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ...]", expected "ndarray[Any, dtype[floating[_64Bit]]]")  [return-value]
manim/mobject/geometry/polygram.py:119: error: "ndarray" expects 2 type arguments, but 1 given  [type-arg]
manim/mobject/geometry/polygram.py:218: error: Need type annotation for "new_points" (hint: "new_points: list[<type>] = ...")  [var-annotated]
manim/mobject/geometry/polygram.py:287: error: Argument 1 to "set_points" of "VMobject" has incompatible type "list[Any]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ...]"  [arg-type]
manim/mobject/geometry/polygram.py:323: error: Argument 1 to "__init__" of "Polygram" has incompatible type "tuple[ndarray[Any, dtype[floating[_64Bit]]], ...]"; expected "ndarray[Any, dtype[floating[_64Bit]]]"  [arg-type]
manim/mobject/geometry/polygram.py:414: error: Argument 1 to "__init__" of "Polygram" has incompatible type "*list[list[Any]]"; expected "ndarray[Any, dtype[floating[_64Bit]]]"  [arg-type]
manim/mobject/geometry/polygram.py:699: error: Incompatible return value type (got "floating[Any]", expected "float")  [return-value]
manim/mobject/geometry/polygram.py:778: error: Argument 1 to "force_direction" of "VMobject" has incompatible type "str"; expected "Literal['CW', 'CCW']"  [arg-type]
manim/mobject/geometry/line.py:101: error: Return value expected  [return-value]
manim/mobject/geometry/line.py:105: error: Unsupported operand types for > ("float" and "object")  [operator]
manim/mobject/geometry/line.py:106: error: Return value expected  [return-value]
manim/mobject/geometry/line.py:107: error: Unsupported operand types for / ("float" and "object")  [operator]
manim/mobject/geometry/line.py:144: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]", expected "ndarray[Any, dtype[floating[_64Bit]]]")  [return-value]
manim/mobject/geometry/line.py:146: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]", expected "ndarray[Any, dtype[floating[_64Bit]]]")  [return-value]
manim/mobject/geometry/line.py:154: error: Argument 1 of "put_start_and_end_on" is incompatible with supertype "Mobject"; supertype defines the argument type as "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"  [override]
manim/mobject/geometry/line.py:154: note: This violates the Liskov substitution principle
manim/mobject/geometry/line.py:154: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
manim/mobject/geometry/line.py:154: error: Argument 2 of "put_start_and_end_on" is incompatible with supertype "Mobject"; supertype defines the argument type as "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"  [override]
manim/mobject/geometry/line.py:206: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]]"  [no-any-return]
manim/mobject/geometry/line.py:209: error: Returning Any from function declared to return "float"  [no-any-return]
manim/mobject/geometry/line.py:223: error: Argument 1 to "scale" of "Mobject" has incompatible type "floating[Any]"; expected "float"  [arg-type]
manim/mobject/geometry/line.py:305: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]", expected "ndarray[Any, dtype[floating[_64Bit]]]")  [return-value]
manim/mobject/geometry/line.py:320: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]", expected "ndarray[Any, dtype[floating[_64Bit]]]")  [return-value]
manim/mobject/geometry/line.py:334: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]]"  [no-any-return]
manim/mobject/geometry/line.py:346: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]]"  [no-any-return]
manim/mobject/geometry/line.py:398: error: Argument 1 to "scale" of "Mobject" has incompatible type "floating[Any]"; expected "float"  [arg-type]
manim/mobject/geometry/line.py:539: error: "__init__" of "Line" gets multiple values for keyword argument "buff"  [misc]
manim/mobject/geometry/line.py:546: error: Signature of "scale" incompatible with supertype "Mobject"  [override]
manim/mobject/geometry/line.py:546: note:      Superclass:
manim/mobject/geometry/line.py:546: note:          def scale(self, scale_factor: float, **kwargs: Any) -> Arrow
manim/mobject/geometry/line.py:546: note:      Subclass:
manim/mobject/geometry/line.py:546: note:          def scale(self, factor: float, scale_tips: bool = ..., **kwargs: Any) -> Arrow
manim/mobject/geometry/line.py:626: error: Incompatible return value type (got "SupportsDunderLT[Any] | SupportsDunderGT[Any]", expected "float")  [return-value]
manim/mobject/geometry/line.py:632: error: Unexpected keyword argument "recurse" for "set_stroke" of "VMobject"  [call-arg]
manim/mobject/geometry/line.py:63: error: Argument "path_arc" to "set_points_by_ends" of "Line" has incompatible type "float | None"; expected "float"  [arg-type]
manim/mobject/geometry/line.py:90: error: Argument "angle" to "ArcBetweenPoints" has incompatible type "float | None"; expected "float"  [arg-type]
manim/mobject/geometry/line.py:93: error: Argument 1 to "set_points_as_corners" of "VMobject" has incompatible type "list[ndarray[Any, dtype[floating[_64Bit]]]]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ...]"  [arg-type]
manim/mobject/geometry/line.py:607: error: Unsupported left operand type for - ("tuple[float, float, float]")  [operator]
manim/mobject/geometry/line.py:607: note: Both left and right operands are unions
manim/mobject/geometry/line.py:633: error: Argument "width" to "set_stroke" of "VMobject" has incompatible type "SupportsDunderLT[Any] | SupportsDunderGT[Any]"; expected "float | None"  [arg-type]
manim/mobject/geometry/line.py:638: error: Argument "width" to "set_stroke" of "VMobject" has incompatible type "SupportsDunderLT[Any] | SupportsDunderGT[Any]"; expected "float | None"  [arg-type]
manim/mobject/geometry/line.py:983: error: Incompatible types in assignment (expression has type "Arc", variable has type "Elbow")  [assignment]
manim/mobject/geometry/boolean_ops.py:8: error: Skipping analyzing "pathops": module is installed, but missing library stubs or py.typed marker  [import-untyped]
manim/mobject/geometry/boolean_ops.py:8: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
manim/mobject/geometry/boolean_ops.py:56: error: Incompatible types in assignment (expression has type "list[Any]", variable has type "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float], ...]")  [assignment]
manim/mobject/geometry/boolean_ops.py:59: error: Unsupported target for indexed assignment ("ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float], ...]")  [index]
manim/mobject/geometry/boolean_ops.py:60: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float], ...]", expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ...]")  [return-value]
manim/mobject/geometry/boolean_ops.py:98: error: Incompatible types in assignment (expression has type "Generator[ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ...], None, None]", variable has type "list[ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ...]]")  [assignment]
manim/mobject/geometry/boolean_ops.py:106: error: Argument 1 to "consider_points_equals_2d" of "VMobject" has incompatible type "Any | ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float]"  [arg-type]
manim/mobject/geometry/boolean_ops.py:106: error: Argument 2 to "consider_points_equals_2d" of "VMobject" has incompatible type "Any | ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float]"  [arg-type]
manim/mobject/geometry/boolean_ops.py:130: error: Incompatible types in assignment (expression has type "Any | ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]", variable has type "ndarray[Any, dtype[Any]]")  [assignment]
manim/mobject/geometry/boolean_ops.py:135: error: Argument 1 to "add_cubic_bezier_curve_to" of "VMobject" has incompatible type "Any | ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]]"  [arg-type]
manim/mobject/geometry/boolean_ops.py:135: error: Argument 2 to "add_cubic_bezier_curve_to" of "VMobject" has incompatible type "Any | ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]]"  [arg-type]
manim/mobject/geometry/boolean_ops.py:135: error: Argument 3 to "add_cubic_bezier_curve_to" of "VMobject" has incompatible type "Any | ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]]"  [arg-type]
manim/mobject/geometry/boolean_ops.py:143: error: Argument 1 to "add_quadratic_bezier_curve_to" of "VMobject" has incompatible type "Any | ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]]"  [arg-type]
manim/mobject/geometry/boolean_ops.py:143: error: Argument 2 to "add_quadratic_bezier_curve_to" of "VMobject" has incompatible type "Any | ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float], ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]]"  [arg-type]
manim/mobject/geometry/boolean_ops.py:180: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
manim/mobject/geometry/boolean_ops.py:219: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
manim/mobject/geometry/boolean_ops.py:261: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
manim/mobject/geometry/boolean_ops.py:314: error: Function is missing a type annotation for one or more arguments  [no-untyped-def]
manim/mobject/geometry/arc.py:186: error: Argument 1 to "cartesian_to_spherical" has incompatible type "ndarray[Any, dtype[floating[Any]]]"; expected "Sequence[float]"  [arg-type]
manim/mobject/geometry/arc.py:265: error: Returning Any from function declared to return "VMobject"  [no-any-return]
manim/mobject/geometry/arc.py:271: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"  [no-any-return]
manim/mobject/geometry/arc.py:274: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"  [no-any-return]
manim/mobject/geometry/arc.py:278: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]", expected "ndarray[Any, dtype[floating[_64Bit]]]")  [return-value]
manim/mobject/geometry/arc.py:280: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]", expected "ndarray[Any, dtype[floating[_64Bit]]]")  [return-value]
manim/mobject/geometry/arc.py:284: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]", expected "ndarray[Any, dtype[floating[_64Bit]]]")  [return-value]
manim/mobject/geometry/arc.py:286: error: Incompatible return value type (got "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]", expected "ndarray[Any, dtype[floating[_64Bit]]]")  [return-value]
manim/mobject/geometry/arc.py:330: error: Argument 1 to "shift" of "Mobject" has incompatible type "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]]"  [arg-type]
manim/mobject/geometry/arc.py:344: error: Argument 1 to "shift" of "Mobject" has incompatible type "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]]"  [arg-type]
manim/mobject/geometry/arc.py:479: error: Incompatible types in assignment (expression has type "floating[Any]", variable has type "float")  [assignment]
manim/mobject/geometry/arc.py:652: error: Argument 1 to "perpendicular_bisector" has incompatible type "ndarray[Any, dtype[Any]]"; expected "Sequence[ndarray[Any, Any]]"  [arg-type]
manim/mobject/geometry/arc.py:653: error: Argument 1 to "perpendicular_bisector" has incompatible type "ndarray[Any, dtype[Any]]"; expected "Sequence[ndarray[Any, Any]]"  [arg-type]
manim/mobject/geometry/arc.py:656: error: Argument "radius" to "Circle" has incompatible type "floating[Any]"; expected "float | None"  [arg-type]
manim/mobject/geometry/arc.py:774: error: Incompatible types in assignment (expression has type "SingleStringMathTex | Text", variable has type "MathTex")  [assignment]
manim/mobject/geometry/arc.py:964: error: Incompatible types in assignment (expression has type "float | None", variable has type "float")  [assignment]
manim/mobject/geometry/arc.py:970: error: Argument 1 to "shift" of "Mobject" has incompatible type "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]]"  [arg-type]
manim/mobject/geometry/arc.py:1109: error: Incompatible types in assignment (expression has type "list[dict[Any, Any]]", variable has type "repeat[dict[str, float]]")  [assignment]
manim/mobject/geometry/arc.py:1112: error: Argument 2 to "ArcBetweenPoints" has incompatible type "**dict[str, float]"; expected "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"  [arg-type]
Found 1707 errors in 40 files (checked 163 source files)

henrikmidtiby avatar Oct 18 '24 21:10 henrikmidtiby

A lot of those are quite difficult (mostly because Manim was not designed with typing in mind!)

I would just # type: ignore a lot of the ones are related to the Liskov substitution principle or mismatches between parent and child classes.

For ones about returning Any, you might have to track down why it thinks the return value is `Any.

If it's being annoying about numpy arrays being a Sequence you can type ignore that. If you have like "list[...] expected NDArray[...]" I might try using np.asarray(...) to get it to fit properly.

If you need any more help or the PR is ready to review feel free to tag me :)

P.S. this might be useful https://docs.manim.community/en/latest/contributing/docs/types.html

JasonGrace2282 avatar Oct 19 '24 01:10 JasonGrace2282

@JasonGrace2282

Ok, status is that 21 errors are currently reported by mypy in the manim/mobject/geometry directory. So now the real detective work starts ...

$ mypy
manim/mobject/geometry/tips.py:152: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]]"  [no-any-return]
manim/mobject/geometry/tips.py:233: error: Argument 1 to "scale" of "Mobject" has incompatible type "floating[Any]"; expected "float"  [arg-type]
manim/mobject/geometry/shape_matchers.py:133: error: Returning Any from function declared to return "ManimColor"  [no-any-return]
manim/mobject/geometry/shape_matchers.py:133: note: Error code "no-any-return" not covered by "type: ignore" comment
manim/mobject/geometry/polygram.py:90: error: Incompatible types in assignment (expression has type "list[Any]", variable has type "ndarray[Any, dtype[floating[_64Bit]]]")  [assignment]
manim/mobject/geometry/polygram.py:119: error: "ndarray" expects 2 type arguments, but 1 given  [type-arg]
manim/mobject/geometry/polygram.py:323: error: Argument 1 to "__init__" of "Polygram" has incompatible type "tuple[ndarray[Any, dtype[floating[_64Bit]]], ...]"; expected "ndarray[Any, dtype[floating[_64Bit]]]"  [arg-type]
manim/mobject/geometry/polygram.py:414: error: Argument 1 to "__init__" of "Polygram" has incompatible type "*list[list[Any]]"; expected "ndarray[Any, dtype[floating[_64Bit]]]"  [arg-type]
manim/mobject/geometry/polygram.py:699: error: Incompatible return value type (got "floating[Any]", expected "float")  [return-value]
manim/mobject/geometry/polygram.py:778: error: Argument 1 to "force_direction" of "VMobject" has incompatible type "str"; expected "Literal['CW', 'CCW']"  [arg-type]
manim/mobject/geometry/line.py:209: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]]"  [no-any-return]
manim/mobject/geometry/line.py:212: error: Returning Any from function declared to return "float"  [no-any-return]
manim/mobject/geometry/line.py:338: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]]"  [no-any-return]
manim/mobject/geometry/line.py:350: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]]"  [no-any-return]
manim/mobject/geometry/line.py:543: error: "__init__" of "Line" gets multiple values for keyword argument "buff"  [misc]
manim/mobject/geometry/line.py:636: error: Unexpected keyword argument "recurse" for "set_stroke" of "VMobject"  [call-arg]
manim/mobject/types/vectorized_mobject.py:331: note: "set_stroke" of "VMobject" defined here
manim/mobject/geometry/line.py:91: error: Argument "angle" to "ArcBetweenPoints" has incompatible type "float | None"; expected "float"  [arg-type]
manim/mobject/geometry/arc.py:187: error: Argument 1 to "cartesian_to_spherical" has incompatible type "ndarray[Any, dtype[floating[Any]]]"; expected "Sequence[float]"  [arg-type]
manim/mobject/geometry/arc.py:266: error: Returning Any from function declared to return "VMobject"  [no-any-return]
manim/mobject/geometry/arc.py:272: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"  [no-any-return]
manim/mobject/geometry/arc.py:275: error: Returning Any from function declared to return "ndarray[Any, dtype[floating[_64Bit]]] | tuple[float, float, float]"  [no-any-return]
manim/mobject/geometry/arc.py:291: error: Incompatible return value type (got "floating[Any]", expected "float")  [return-value]
Found 21 errors in 5 files (checked 163 source files)

henrikmidtiby avatar Oct 21 '24 09:10 henrikmidtiby

@JasonGrace2282 I would like a set of fresh eyes to look at this PR

The status is that mypy now reports three type errors (listed below). I think that finding proper solutions for these requires more knowledge about the inner structure of Manim than I currently have.

In the PR I have suggested to ignore a bunch of type errors. Is this an acceptable approach? In most cases I suspect that the issue is in a different location of the codebase that might be difficult to change, and thus ignoring the issues were my last resort.

$ mypy
manim/mobject/geometry/line.py:549: error: "__init__" of "Line" gets multiple values for keyword argument "buff"  [misc]
manim/mobject/geometry/line.py:642: error: Unexpected keyword argument "recurse" for "set_stroke" of "VMobject"  [call-arg]
manim/mobject/types/vectorized_mobject.py:331: note: "set_stroke" of "VMobject" defined here
manim/mobject/geometry/line.py:91: error: Argument "angle" to "ArcBetweenPoints" has incompatible type "float | None"; expected "float"  [arg-type]
Found 3 errors in 1 file (checked 163 source files)

henrikmidtiby avatar Oct 21 '24 21:10 henrikmidtiby

For the first error, I'm not quite sure why mypy complains - does it think that buff=buff conflicts with **kwargs? I think it's safe to ignore that error.

You can type: ignore the second error- that's yet another case where mypy doesn't understand our metaclass behavior with config.renderer and ConvertToOpengl

The third looks like a bug in Manim (self.path_arc should probably just be path_arc). However, to prevent a behavior change inside a typing PR, I would just encourage a

assert self.path_arc is not None

before creating the ArcBetweenPoints.

JasonGrace2282 avatar Oct 22 '24 11:10 JasonGrace2282

For the first error, I'm not quite sure why mypy complains - does it think that buff=buff conflicts with **kwargs? I think it's safe to ignore that error.

As I read the code, the Arrow class is derived from the Line class. The Line.__init__ method does not accept any positional arguments (which the code tries to pass over with *args). When I remove *args in the call to super().__init__(...) the type error disappears, but it triggers a few errors in the testing build.

You can type: ignore the second error- that's yet another case where mypy doesn't understand our metaclass behavior with config.renderer and ConvertToOpengl

Fine.

The third looks like a bug in Manim (self.path_arc should probably just be path_arc). However, to prevent a behavior change inside a typing PR, I would just encourage a

assert self.path_arc is not None

before creating the ArcBetweenPoints.

Perfect, a solution is then found!

Thanks for the input. I will push my changes in a moment.

henrikmidtiby avatar Oct 22 '24 11:10 henrikmidtiby

@JasonGrace2282 Current status is that I have zero type errors according to mypy (version 1.11.1) on my computer.

When I earlier activated mypy on the CI workflow link, it found a lot of errors. Therefore it was deactivated again.

The Code scanning results / CodeQL fails with a warning and a failure.

The docs/readthedocs.org:manimce build also fails.

henrikmidtiby avatar Oct 22 '24 12:10 henrikmidtiby

Looking at the CI/CodeQL comments, I think it's fine to ignore them. About the docs build, that was recently fixed - you might have to resolve conflicts and then merge main into this branch. With any luck, I'll be able to review this PR soon. Thanks for your effort with cleaning up a LOT of mypy errors :)

JasonGrace2282 avatar Oct 23 '24 16:10 JasonGrace2282

I did leave some comments/questions, so it would be nice to work towards getting those worked out so that everyone can enjoy the benefits of IDE completion :)

Thanks for all the suggestions. I have added a comment to each of the suggestions. In most cases I have implemented the suggestion. In the cases where I haven't implemented the suggestion a detailed comment explains why.

henrikmidtiby avatar Oct 25 '24 07:10 henrikmidtiby

@JasonGrace2282 I think we are close to merging the pull request. Will you take a look?

It seems like the CI flow does not activate the tests, they are listed as "Expected -- Waiting for status to be reported".

henrikmidtiby avatar Oct 26 '24 07:10 henrikmidtiby