Skip to content
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

docs: update typing guidelines #3704

Merged
merged 5 commits into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions docs/source/contributing/docs/typings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ https://realpython.com/python-type-checking/#hello-types.
Typing standards
~~~~~~~~~~~~~~~~

Manim uses `mypy`_ to type check its codebase. You will find a list of
configuration values in the ``mypy.ini`` configuration file.

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

Expand Down Expand Up @@ -91,6 +89,32 @@ Typing guidelines

* Use ``typing.Iterable`` whenever the function works with *any* iterable, not a specific type.

* Prefer ``numpy.typing.NDArray`` over ``numpy.ndarray``

.. code:: py

import numpy as np

if TYPE_CHECKING:
import numpy.typing as npt


def foo() -> npt.NDArray[float]:
return np.array([1, 0, 1])

* If a method returns ``self``, use ``typing_extensions.Self``.
JasonGrace2282 marked this conversation as resolved.
Show resolved Hide resolved

.. code:: py

if TYPE_CHECKING:
from typing_extensions import Self


class CustomMobject:
def set_color(self, color: ManimColor) -> Self:
...
return self

* If the function returns a container of a specific length each time, consider using ``tuple`` instead of ``list``.

.. code:: py
Expand Down
Loading