Skip to content

Commit c2ebbde

Browse files
Improve based on feedback
1 parent e85f876 commit c2ebbde

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed
Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
==================
2-
Choosing Typehints
3-
==================
1+
===================
2+
Choosing Type Hints
3+
===================
44
In order to provide the best user experience,
5-
it's important that typehints are chosen correctly.
5+
it's important that type hints are chosen correctly.
66
With the large variety of types provided by Manim, choosing
77
which one to use can be difficult. This guide aims to
88
aid you in the process of choosing the right type for the scenario.
99

1010

11-
The first step is figuring out which category your typehint fits into.
11+
The first step is figuring out which category your type hint fits into.
1212

1313
Coordinates
1414
-----------
15-
Coordinates encompasses two "main" categories: points, and vectors.
15+
Coordinates encompass two main categories: points, and vectors.
1616

1717

1818
Points
@@ -41,10 +41,10 @@ in space. For example:
4141
# it's a point3D
4242
status3D(coord)
4343
44-
It's important to realize that the status functions worked with both
44+
It's important to realize that the status functions accepted both
4545
tuples/lists of the correct length, and ``NDArray``'s of the correct shape.
46-
If they only worked with ``NDArray``'s, we would use their "Internal" counterparts:
47-
``InternalPoint2D``, ``InternalPoint3D``, ``InternalPoint2D_Array`` and ``InternalPoint3D_Array``.
46+
If they only accepted ``NDArray``'s, we would use their ``Internal`` counterparts:
47+
:class:`~.typing.InternalPoint2D`, :class:`~.typing.InternalPoint3D`, :class:`~.typing.InternalPoint2D_Array` and :class:`~.typing.InternalPoint3D_Array`.
4848

4949
In general, the type aliases prefixed with ``Internal`` should never be used on
5050
user-facing classes and functions, but should be reserved for internal behavior.
@@ -61,41 +61,41 @@ consider this slightly contrived function:
6161
return mob.shift(direction * scale_factor)
6262
6363
Here we see an important example of the difference. ``direction`` can not, and
64-
should not, be typed as a ``Point3D`` because it does not work with something
65-
like ``direction=(0, 1, 0)``. You could type it as ``InternalPoint3D`` and
66-
the typechecker and linter would be happy; however, this makes the code harder
64+
should not, be typed as a :class:`~.typing.Point3D` because the function does not accept tuples/lists,
65+
like ``direction=(0, 1, 0)``. You could type it as :class:`~.typing.InternalPoint3D` and
66+
the type checker and linter would be happy; however, this makes the code harder
6767
to understand.
6868

6969
As a general rule, if a parameter is called ``direction`` or ``axis``,
70-
it should be typehinted as some form of ``Vector``.
70+
it should be type hinted as some form of :class:`~.VectorND`.
7171

7272
.. warning::
7373

74-
This is not always true. For example as of Manim 0.18.0, the direction
75-
parameter of the Vector mobject should be ``Point2D | Point3D``
74+
This is not always true. For example, as of Manim 0.18.0, the direction
75+
parameter of the :class:`.Vector` Mobject should be ``Point2D | Point3D``,
7676
as it can also accept ``tuple[float, float]`` and ``tuple[float, float, float]``.
7777

7878
Colors
7979
------
80-
The interface manim provides for working with colors is :class:`.ManimColor`.
80+
The interface Manim provides for working with colors is :class:`.ManimColor`.
8181
The main color types Manim supports are RGB, RGBA, and HSV. You will want
82-
to typehint a function depending on which type it uses. If any color will work,
82+
to add type hints to a function depending on which type it uses. If any color will work,
8383
you will need something like:
8484

8585
.. code-block:: python
8686
8787
if TYPE_CHECKING:
8888
from manim.utils.color import ParsableManimColor
8989
90-
# typehint stuff with ParsableManimColor
90+
# type hint stuff with ParsableManimColor
9191
9292
9393
9494
Béziers
9595
-------
96-
Manim internally represents a ``Mobject`` by a collection of points. These
97-
points represent Bézier curves, which are a way of representing a curve using a
98-
sequence of points.
96+
Manim internally represents a :class:`.Mobject` by a collection of points. In the case of :class:`.VMobject`,
97+
the most commonly used subclass of :class:`.Mobject`, these points represent Bézier curves,
98+
which are a way of representing a curve using a sequence of points.
9999

100100
.. note::
101101

@@ -105,30 +105,30 @@ sequence of points.
105105
Manim supports two different renderers, which each have different representations of
106106
Béziers: Cairo uses cubic Bézier curves, while OpenGL uses quadratic Bézier curves.
107107

108-
Typehints like ``BezierPoints`` represent a single bezier curve, and ``BezierPath`` is
109-
essentially a sequence of ``BezierPoints``. A ``Spline`` is when a ``BezierPath``
110-
forms a closed curve. Manim also provides more specific type aliases when working with
111-
quadratic or cubic curves, and they are prefixed with their respective type (e.g. ``CubicBezierPoints``,
112-
which is a ``BezierPoints`` that specifically applies to cubic Bézier curves).
108+
type hints like :class:`~.typing.BezierPoints` represent a single bezier curve, and :class:`~.typing.BezierPath`
109+
represents multiple Bézier curves. A :class:`~.typing.Spline` is when the Bézier curves in a :class:`~.typing.BezierPath`
110+
forms a single connected curve. Manim also provides more specific type aliases when working with
111+
quadratic or cubic curves, and they are prefixed with their respective type (e.g. :class:`~.typing.CubicBezierPoints`,
112+
is a :class:`~.typing.BezierPoints` consisting of exactly 4 points representing a cubic Bézier curve).
113113

114114

115115
Functions
116116
---------
117117
Throughout the codebase, many different types of functions are used. The most obvious example
118118
is a rate function, which takes in a float and outputs a float (``Callable[[float], float]``).
119119
Another example is for overriding animations. One will often need to map a :class:`.Mobject`
120-
to an overridden :class:`Animation`, and for that we have the ``FunctionOverride`` typehint.
120+
to an overridden :class:`.Animation`, and for that we have the :class:`~.typing.FunctionOverride` type hint.
121121

122-
``PathFuncType`` and ``MappingFunction`` are more niche, but are related to moving objects
122+
:class:`~.typing.PathFuncType` and :class:`~.typing.MappingFunction` are more niche, but are related to moving objects
123123
along a path, or applying functions. If you need to use it, you'll know.
124124

125125

126126
Images
127127
------
128-
There are several representations of images in manim. The most common is
129-
the representation as a numpy array of floats representing the pixels of an image.
128+
There are several representations of images in Manim. The most common is
129+
the representation as a NumPy array of floats representing the pixels of an image.
130130
This is especially common when it comes to the OpenGL renderer.
131131

132-
This is the usecase of the ``Image`` typehint. Sometimes, manim may use ``PIL.Image``,
133-
in which case one should use that typehint instead.
134-
Of course if a more specific type of image is needed, it can be annotated as such.
132+
This is the use case of the :class:`~.typing.Image` type hint. Sometimes, Manim may use ``PIL.Image``,
133+
in which case one should use that type hint instead.
134+
Of course, if a more specific type of image is needed, it can be annotated as such.

0 commit comments

Comments
 (0)