mirror of
https://github.com/ManimCommunity/manim.git
synced 2026-06-22 10:01:47 +00:00
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
This commit is contained in:
parent
04bfa22107
commit
5139765ff4
1 changed files with 27 additions and 3 deletions
|
|
@ -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`_.
|
||||
|
||||
|
|
@ -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``.
|
||||
|
||||
.. 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue