Skip to content

Commit 5139765

Browse files
docs: update typing guidelines (#3704)
* Update typing guidelines * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix formatting * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci
1 parent 04bfa22 commit 5139765

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

docs/source/contributing/docs/typings.rst

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ https://realpython.com/python-type-checking/#hello-types.
1818
Typing standards
1919
~~~~~~~~~~~~~~~~
2020

21-
Manim uses `mypy`_ to type check its codebase. You will find a list of
22-
configuration values in the ``mypy.ini`` configuration file.
23-
21+
Manim uses `mypy`_ to type check its codebase. You will find a list of configuration values in the ``mypy.ini`` configuration file.
2422
To be able to use the newest typing features not available in the lowest
2523
supported Python version, make use of `typing_extensions`_.
2624

@@ -91,6 +89,32 @@ Typing guidelines
9189
9290
* Use ``typing.Iterable`` whenever the function works with *any* iterable, not a specific type.
9391

92+
* Prefer ``numpy.typing.NDArray`` over ``numpy.ndarray``
93+
94+
.. code:: py
95+
96+
import numpy as np
97+
98+
if TYPE_CHECKING:
99+
import numpy.typing as npt
100+
101+
102+
def foo() -> npt.NDArray[float]:
103+
return np.array([1, 0, 1])
104+
105+
* If a method returns ``self``, use ``typing_extensions.Self``.
106+
107+
.. code:: py
108+
109+
if TYPE_CHECKING:
110+
from typing_extensions import Self
111+
112+
113+
class CustomMobject:
114+
def set_color(self, color: ManimColor) -> Self:
115+
...
116+
return self
117+
94118
* If the function returns a container of a specific length each time, consider using ``tuple`` instead of ``list``.
95119

96120
.. code:: py

0 commit comments

Comments
 (0)