|
| 1 | +================= |
| 2 | +Adding Docstrings |
| 3 | +================= |
| 4 | + |
| 5 | +A docstring is a string literal that is used right after the definition |
| 6 | +of a module, function, class, or method. They are used to document our code. |
| 7 | +This page will give you a set of guidelines to write efficient and correct docstrings. |
| 8 | + |
| 9 | + |
| 10 | +Formatting the Docstrings |
| 11 | +------------------------- |
| 12 | + |
| 13 | +Please begin the description of the class/function in the same line as |
| 14 | +the 3 quotes: |
| 15 | + |
| 16 | +.. code:: py |
| 17 | +
|
| 18 | + def do_this(): |
| 19 | + """This is correct. |
| 20 | + (...) |
| 21 | + """ |
| 22 | +
|
| 23 | +
|
| 24 | + def dont_do_this(): |
| 25 | + """ |
| 26 | + This is incorrect. |
| 27 | + (...) |
| 28 | + """ |
| 29 | +
|
| 30 | +NumPy Format |
| 31 | +------------ |
| 32 | +The Manim Community uses numpy format for the documentation. |
| 33 | + |
| 34 | +Use the numpy format for sections and formatting - see |
| 35 | +https://numpydoc.readthedocs.io/en/latest/format.html. |
| 36 | + |
| 37 | +This includes: |
| 38 | + |
| 39 | +1. The usage of ``Attributes`` to specify ALL ATTRIBUTES that a |
| 40 | + class can have and a brief (or long, if |
| 41 | + needed) description. |
| 42 | + |
| 43 | +Also, ``__init__`` parameters should be specified as ``Parameters`` **on |
| 44 | +the class docstring**, *rather than under* ``__init__``. Note that this |
| 45 | +can be omitted if the parameters and the attributes are the same |
| 46 | +(i.e., dataclass) - priority should be given to the ``Attributes`` |
| 47 | +section, in this case, which must **always be present**, unless the |
| 48 | +class specifies no attributes at all. (See more on Parameters in number |
| 49 | +2 of this list.) |
| 50 | + |
| 51 | +Example: |
| 52 | + |
| 53 | +.. code:: py |
| 54 | +
|
| 55 | + class MyClass: |
| 56 | + """My cool class. Long or short (whatever is more appropriate) description here. |
| 57 | +
|
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + name |
| 61 | + The class's name. |
| 62 | + id |
| 63 | + The class's id. |
| 64 | + mobj |
| 65 | + The mobject linked to this instance. Defaults to `Mobject()` \ |
| 66 | + (is set to that if `None` is specified). |
| 67 | +
|
| 68 | + Attributes |
| 69 | + ---------- |
| 70 | + name |
| 71 | + The user's name. |
| 72 | + id |
| 73 | + The user's id. |
| 74 | + singleton |
| 75 | + Something. |
| 76 | + mobj |
| 77 | + The mobject linked to this instance. |
| 78 | + """ |
| 79 | +
|
| 80 | + def __init__(name: str, id: int, singleton: MyClass, mobj: Mobject = None): |
| 81 | + ... |
| 82 | +
|
| 83 | +2. The usage of ``Parameters`` on functions to specify how |
| 84 | + every parameter works and what it does. This should be excluded if |
| 85 | + the function has no parameters. Note that you **should not** specify |
| 86 | + the default value of the parameter on the type. On the documentation |
| 87 | + render, this is already specified on the function's signature. If you |
| 88 | + need to indicate a further consequence of value omission or simply |
| 89 | + want to specify it on the docs, make sure to **specify it in the |
| 90 | + parameter's DESCRIPTION**. |
| 91 | + |
| 92 | +See an example on list item 4. |
| 93 | + |
| 94 | +.. note:: |
| 95 | + |
| 96 | + When documenting varargs (args and kwargs), make sure to |
| 97 | + document them by listing the possible types of each value specified, |
| 98 | + like this: |
| 99 | + |
| 100 | +:: |
| 101 | + |
| 102 | + Parameters |
| 103 | + ---------- |
| 104 | + args |
| 105 | + The args specified can be either an int or a float. |
| 106 | + kwargs |
| 107 | + The kwargs specified can only be a float. |
| 108 | + |
| 109 | +Note that, if the kwargs expect specific values, those can be specified |
| 110 | +in a section such as ``Other Parameters``: |
| 111 | + |
| 112 | +:: |
| 113 | + |
| 114 | + Other Parameters |
| 115 | + ---------------- |
| 116 | + kwarg_param_1 |
| 117 | + Parameter documentation here |
| 118 | + (etc) |
| 119 | + |
| 120 | +3. The usage of ``Returns`` to indicate what is the type of this |
| 121 | + function's return value and what exactly it returns (i.e., a brief - |
| 122 | + or long, if needed - description of what this function returns). Can |
| 123 | + be omitted if the function does not explicitly return (i.e., always |
| 124 | + returns ``None`` because ``return`` is never specified, and it is |
| 125 | + very clear why this function does not return at all). In all other |
| 126 | + cases, it should be specified. |
| 127 | + |
| 128 | +See an example on list item 4. |
| 129 | + |
| 130 | +4. The usage of ``Examples`` in order to specify an example of usage of |
| 131 | + a function **is highly encouraged** and, in general, should be |
| 132 | + specified for *every function* unless its usage is **extremely |
| 133 | + obvious**, which can be debatable. Even if it is, it's always a good |
| 134 | + idea to add an example in order to give a better orientation to the |
| 135 | + documentation user. Use the following format for Python code: |
| 136 | + |
| 137 | + .. code:: rst |
| 138 | +
|
| 139 | + :: |
| 140 | +
|
| 141 | + # python code here |
| 142 | +
|
| 143 | +.. note:: |
| 144 | + Also, if this is a video- or animation-related change, please |
| 145 | + try to add an example GIF or video if possible for demonstration |
| 146 | + purposes. |
| 147 | + |
| 148 | +Make sure to be as explicit as possible in your documentation. We all |
| 149 | +want the users to have an easier time using this library. |
| 150 | + |
| 151 | +Example: |
| 152 | + |
| 153 | +.. code:: py |
| 154 | +
|
| 155 | + def my_function( |
| 156 | + thing: int, |
| 157 | + other: np.ndarray, |
| 158 | + name: str, |
| 159 | + *, |
| 160 | + d: "SomeClassFromFarAway", |
| 161 | + test: Optional[int] = 45 |
| 162 | + ) -> "EpicClassInThisFile": # typings are optional for now |
| 163 | + """My cool function. Builds and modifies an :class:`EpicClassInThisFile` instance with the given |
| 164 | + parameters. |
| 165 | +
|
| 166 | + Parameters |
| 167 | + ---------- |
| 168 | + thing |
| 169 | + Specifies the index of life. |
| 170 | + other |
| 171 | + Specifies something cool. |
| 172 | + name |
| 173 | + Specifies my name. |
| 174 | + d |
| 175 | + Sets thing D to this value. |
| 176 | + test |
| 177 | + Defines the number of times things should be tested. \ |
| 178 | + Defaults to 45, because that is almost the meaning of life. |
| 179 | +
|
| 180 | + Returns |
| 181 | + ------- |
| 182 | + :class:`EpicClassInThisFile` |
| 183 | + The generated EpicClass with the specified attributes and modifications. |
| 184 | +
|
| 185 | + Examples |
| 186 | + -------- |
| 187 | + Normal usage:: |
| 188 | +
|
| 189 | + my_function(5, np.array([1, 2, 3]), "Chelovek", d=SomeClassFromFarAway(cool=True), test=5) |
| 190 | + """ |
| 191 | + # code... |
| 192 | + pass |
0 commit comments