|
| 1 | +================== |
| 2 | +Choosing Typehints |
| 3 | +================== |
| 4 | +In order to provide the best user experience, |
| 5 | +it's important that typehints are chosen correctly. |
| 6 | +With the large variety of types provided by Manim, choosing |
| 7 | +which one to use can be difficult. This guide aims to |
| 8 | +aid you in the process. |
| 9 | + |
| 10 | + |
| 11 | +The first step is figuring out which category your typehint fits into. |
| 12 | + |
| 13 | +Coordinates |
| 14 | +----------- |
| 15 | +Coordinates encompasses two "main" categories: points, and vectors. |
| 16 | + |
| 17 | + |
| 18 | +Points |
| 19 | +~~~~~~ |
| 20 | +The purpose of points is pretty straightforward: they represent a point |
| 21 | +in space. For example: |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + def status2D(coord: Point2D) -> None: |
| 26 | + x, y = coord |
| 27 | + print(f"Point at {x=},{y=}") |
| 28 | +
|
| 29 | + def status3D(coord: Point3D) -> None: |
| 30 | + x, y, z = coord |
| 31 | + print(f"Point at {x=},{y=},{z=}") |
| 32 | +
|
| 33 | + def get_statuses(coords: Point2D_Array | Point3D_Array) -> None: |
| 34 | + for coord in coords: |
| 35 | + if len(coord) == 2: |
| 36 | + # it's a Point2D |
| 37 | + status2D(coord) |
| 38 | + else: |
| 39 | + # it's a point3D |
| 40 | + status3D(coord) |
| 41 | +
|
| 42 | +It's important to realize that the status functions worked with both |
| 43 | +tuples/lists of the correct length, and ``NDArray``'s of the correct shape. |
| 44 | +If they only worked with ``NDArray``'s, we would use their "Internal" counterparts: |
| 45 | +``InternalPoint2D``, ``InternalPoint3D``, ``InternalPoint2D_Array`` and ``InternalPoint3D_Array``. |
| 46 | + |
| 47 | +In general, the type aliases prefixed with ``Internal`` should never be used on |
| 48 | +user-facing classes and functions, but should be reserved for internal behavior. |
| 49 | + |
| 50 | +Vectors |
| 51 | +~~~~~~~ |
| 52 | +Vectors share many similarities to points. However, they have a different |
| 53 | +connotation. Vectors should be used to represent direction. For example, |
| 54 | +consider this slightly contrived function: |
| 55 | + |
| 56 | +.. code-block:: python |
| 57 | +
|
| 58 | + def shift_mobject(mob: Mobject, direction: Vector3D, scale_factor: float = 1) -> mob: |
| 59 | + return mob.shift(direction * scale_factor) |
| 60 | +
|
| 61 | +Here we see an important example of the difference. ``direction`` can not, and |
| 62 | +should not, be typed as a ``Point3D`` because it does not work with something |
| 63 | +like ``direction=(0, 1, 0)``. You could type it as ``InternalPoint3D`` and |
| 64 | +the typechecker and linter would be happy; however, this makes the code harder |
| 65 | +to understand. |
| 66 | + |
| 67 | +As a general rule, if a parameter is called ``direction`` it should be typehinted as |
| 68 | +some form of ``Vector``. |
| 69 | + |
| 70 | +.. warning:: |
| 71 | + |
| 72 | + This is not always true. For example as of Manim 0.18.0, the direction |
| 73 | + parameter of the Vector mobject should be ``Point2D | Point3D`` |
| 74 | + as it can also accept ``tuple[float, float]`` and ``tuple[float, float, float]``. |
| 75 | + |
| 76 | +Colors |
| 77 | +------ |
| 78 | + |
| 79 | + |
| 80 | +Béziers |
| 81 | +------- |
| 82 | + |
| 83 | + |
| 84 | +Functions |
| 85 | +--------- |
| 86 | + |
| 87 | + |
0 commit comments