You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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
+
defcopy(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
+
deffoo() -> 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 importTYPE_CHECKING
116
+
117
+
ifTYPE_CHECKING:
118
+
from manim.typing import Vector3
119
+
# type stuff with Vector3
120
+
75
121
Missing Sections for typehints are:
76
122
-----------------------------------
77
123
78
124
* 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``...)
0 commit comments