Skip to content

Commit 9b18a86

Browse files
JasonGrace2282pre-commit-ci[bot]Viicos
authored
Finish TODO's in contributing/typings.rst (#3545)
* Updated typing docs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added link for protocols * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added object vs Any * Fix Typo * Rephrase TypeVar Co-authored-by: Viicos <[email protected]> * Compare between tuple vs list Co-authored-by: Viicos <[email protected]> * typing -> collections.abc Co-authored-by: Viicos <[email protected]> * typing -> collections.abc Co-authored-by: Viicos <[email protected]> * change method to attr Co-authored-by: Viicos <[email protected]> * clarify object typehint Co-authored-by: Viicos <[email protected]> * Fix code typo Co-authored-by: Viicos <[email protected]> * Added if TYPE_CHECKING section * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix reST for inline code * Elaborate on if TYPE_CHECKING Co-authored-by: Viicos <[email protected]> * functions -> collections Co-authored-by: Viicos <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Viicos <[email protected]>
1 parent 34e7d68 commit 9b18a86

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

docs/source/contributing/typings.rst

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Typing guidelines
4040
pending shape support, using the correct type aliases will help users understand
4141
which shape should be used.
4242

43+
* For typings of generic collections, check out `this <https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes>`_
44+
link.
45+
4346
* Always use a type hint of ``None`` for functions that does not return
4447
a value (this also applies to ``__init__``), e.g.:
4548

@@ -57,6 +60,8 @@ Typing guidelines
5760
* Following `PEP 484 <https://peps.python.org/pep-0484/#the-numeric-tower>`_,
5861
use ``float`` instead of ``int | float``.
5962

63+
* Use ``x | y`` instead of ``Union[x, y]``
64+
6065
* Mobjects have the typehint ``Mobject``, e.g.:
6166

6267
.. code:: py
@@ -66,16 +71,55 @@ Typing guidelines
6671
return self.set_color(mobject.get_color())
6772
6873
* Always parametrize generics (``list[int]`` instead of ``list``,
69-
``type[Any]`` instead of ``type``, etc.). This also applies to callables:
74+
``type[Any]`` instead of ``type``, etc.). This also applies to callables.
7075

7176
.. code:: py
7277
7378
rate_func: Callable[[float], float] = lambda t: smooth(1 - t)
7479
80+
* Use ``TypeVar`` when you want to "link" type hints as being the same type.
81+
Consider ``Mobject.copy``, which returns a new instance of the same class.
82+
It would be type-hinted as:
83+
84+
.. code:: py
85+
86+
T = TypeVar("T")
87+
88+
89+
def copy(self: T) -> T:
90+
...
91+
92+
* Use ``typing.Iterable`` whenever the function works with *any* iterable, not a specific type.
93+
94+
* If the function returns a container of a specific length each time, consider using ``tuple`` instead of ``list``.
95+
96+
.. code:: py
97+
98+
def foo() -> tuple[float, float, float]:
99+
return (0, 0, 0)
100+
101+
* If a function works with a parameter as long as said parameter has a ``__getitem__``, ``__iter___`` and ``__len__`` method,
102+
the typehint of the parameter should be ``collections.abc.Mapping``. If it also supports ``__setitem__`` and/or ``__delitem__``, it
103+
should be marked as ``collections.abc.MutableMapping``.
104+
105+
* Typehinting something as ``object`` means that only attributes available on every Python object should be accessed,
106+
like ``__str__`` and so on. On the other hand, literally any attribute can be accessed on a variable with the ``Any`` typehint -
107+
it's more freeing than the ``object`` typehint, and makes mypy stop typechecking the variable. Note that whenever possible,
108+
try to keep typehints as specific as possible.
109+
110+
* If objects are imported purely for type hint purposes, keep it under an ``if typing.TYPE_CHECKING`` guard, to prevent them from
111+
being imported at runtime (helps library performance). Do not forget to use the ``from __future__ import annotations`` import to avoid having runtime ``NameError`` exceptions.
112+
113+
.. code:: py
114+
115+
from typing import TYPE_CHECKING
116+
117+
if TYPE_CHECKING:
118+
from manim.typing import Vector3
119+
# type stuff with Vector3
120+
75121
Missing Sections for typehints are:
76122
-----------------------------------
77123

78124
* Mypy and numpy import errors: https://realpython.com/python-type-checking/#running-mypy
79-
* When to use ``object`` vs ``Any``
80-
* The use of a TypeVar on the type hints for ``copy()``.
81-
* The definition and use of Protocols (like ``Sized``, ``Sequence``, ``Iterable``...)
125+
* Explain ``mypy.ini`` (see above link)

0 commit comments

Comments
 (0)