| 1 |
============================ |
| 2 |
Documentation and Type Hints |
| 3 |
============================ |
| 4 |
|
| 5 |
The pyglet documentation is written in `reStructuredText`_ (RST), and built with `Sphinx`_. |
| 6 |
If you are not familiar with reStructuredText, it is similar to MarkDown but offers more |
| 7 |
robust functionality for creating technical documentation. This is the default language |
| 8 |
used in Sphinx. Most of the documentation is written in plain ascii text, so you can still |
| 9 |
modify or add to it without needing to know much RST syntax. |
| 10 |
|
| 11 |
The documentation can be found as part of the pyglet repository, under the ``doc`` |
| 12 |
subfolder. The main index is located at ``pyglet/doc/index.rst``, which includes the |
| 13 |
main landing page, and defines three toctrees: |
| 14 |
|
| 15 |
* The programming guide |
| 16 |
* The API docs |
| 17 |
* The development guide |
| 18 |
|
| 19 |
|
| 20 |
programming guide |
| 21 |
----------------- |
| 22 |
The programming guide is made up of many individual `.rst` files, which are located |
| 23 |
in the repository under the ``pyglet/doc/programming_guide/`` folder. All files in this |
| 24 |
folder are also defined in the index.rst, in order to includes them in the build process. |
| 25 |
|
| 26 |
|
| 27 |
API documentation |
| 28 |
----------------- |
| 29 |
The API documentation is generated directly from the source code docstrings by `autodoc`_, |
| 30 |
a plugin extension that is included with Sphinx. The generation itself is mostly automatic, |
| 31 |
but declaration files are necessary to instruct Sphinx how and which modules and classes |
| 32 |
should be included. Look through the existing files at ``pyglet/doc/modules/`` to get an |
| 33 |
idea of what this looks like. |
| 34 |
|
| 35 |
docstring rules |
| 36 |
^^^^^^^^^^^^^^^ |
| 37 |
- Google Format with no typing. |
| 38 |
- Docstrings are meant primarily for user or general information, not implementation details. |
| 39 |
- Hash (``#``) comments should be used for developers or contributors when needing to provide information to other developers. |
| 40 |
- Any methods or functions without a docstring will not appear in the API documentation. |
| 41 |
- Documentation will hide private attributes unless otherwise overridden in the rst. |
| 42 |
- Parameter descriptions are not required if straight forward enough, unless a clarification is needed. |
| 43 |
- If you have to add a parameter description, you must add one for each in that function. (None or All) |
| 44 |
- A return description is optional. |
| 45 |
- All classes and functions should have a header with at least a one line succinct description. |
| 46 |
- For class attributes, use ``Attributes:`` in the header. Type hints may be specified, as currently they are not picked up automatically. |
| 47 |
- Required: You will also need to add these ``:exclude-members: <attribute>`` in the rst files manually. Otherwise, autodoc will pick it up twice. |
| 48 |
- Class attributes are generally meant to be used for: |
| 49 |
a. Changing global behavior to the class in some form. (Example: ``Texture.default_mag_filter``) |
| 50 |
b. Simplifying subclassing for users. (Example: group_class for Sprite) |
| 51 |
c. Troubleshooting purposes. |
| 52 |
|
| 53 |
- Parameter descriptions should be indented on a new line for clarity. |
| 54 |
- Parameter descriptions can have one line of space between each for clarity. |
| 55 |
- For class instance attributes, these should be documented with a #: <description> comment. |
| 56 |
- For class instance attributes you wish to hide from the docs. Either: |
| 57 |
a. Consider making them private (via ``_name``) if it makes sense. |
| 58 |
b. Mark them with a comment in this format: #: :meta private: |
| 59 |
|
| 60 |
typing rules |
| 61 |
^^^^^^^^^^^^ |
| 62 |
- Do not put type information in docstrings, rely on annotations. |
| 63 |
- If you are using class annotations, you must have ``from __future__ import annotations`` at the top of the module. |
| 64 |
- When adding annotations, if a class type is not used in the file, it must be imported only within TYPE_CHECKING |
| 65 |
- No need to go overboard, some rules can be set to ignored if there is not yet an adequate way to do it. |
| 66 |
- Return types always need to be specified even if ``None``. |
| 67 |
|
| 68 |
example |
| 69 |
^^^^^^^ |
| 70 |
|
| 71 |
.. code-block:: python |
| 72 |
|
| 73 |
class Class1(): |
| 74 |
"""Short description of the class |
| 75 |
|
| 76 |
This is a good place for a general high level description |
| 77 |
of what the class is, and what it is used for. For example: |
| 78 |
Class1 is used for X and Y, which makes Z possible, etc. |
| 79 |
""" |
| 80 |
|
| 81 |
#: This is the default thing. |
| 82 |
some_default_thing: float = 123.0 |
| 83 |
|
| 84 |
#: :meta private: |
| 85 |
dont_show_me: int = 456 |
| 86 |
|
| 87 |
def __init__(self, name: str, size: float): |
| 88 |
"""Constructor description |
| 89 |
|
| 90 |
Here is where you can describe how to make an instance of the |
| 91 |
class. If any of the arguments need further explanation, that |
| 92 |
detail can be added as necessary. For example: |
| 93 |
You must provide a name and a size, etc. etc. |
| 94 |
|
| 95 |
Args: |
| 96 |
name: The name of this object. |
| 97 |
size: The size, in centimeters. |
| 98 |
""" |
| 99 |
self.name = name |
| 100 |
self.size = size |
| 101 |
|
| 102 |
#: :This will show in the docs |
| 103 |
self.attribute_one: int = None |
| 104 |
|
| 105 |
self.attribute_two: str = "hello" |
| 106 |
|
| 107 |
def set_size(self, centimeters: float) -> None: |
| 108 |
"""Simple description + type hints are enough.""" |
| 109 |
self.size = centimeters |
| 110 |
|
| 111 |
def calculate_scaled_size(self, scale: float) -> float: |
| 112 |
"""Detailed method description |
| 113 |
|
| 114 |
You can add more details here. |
| 115 |
|
| 116 |
Args: |
| 117 |
scale: The argument description. |
| 118 |
|
| 119 |
Returns: |
| 120 |
Describe what is being returned, if not already clear. |
| 121 |
""" |
| 122 |
# This is a developer comment, which is not intended for end users to |
| 123 |
# see. These can be used to explain to future developers why something |
| 124 |
# is implemented a certain way, or any other developer focused notes, etc. |
| 125 |
return self.size * scale |
| 126 |
|
| 127 |
|
| 128 |
def function_with_pep484_type_annotations(param1: int, param2: str) -> bool: |
| 129 |
"""Example function with PEP 484 type annotations. |
| 130 |
|
| 131 |
Args: |
| 132 |
param1: The first parameter. |
| 133 |
param2: The second parameter. |
| 134 |
|
| 135 |
Returns: |
| 136 |
When necessary, you can describe the return value in |
| 137 |
more detail. This can be skipped if it's obvious from |
| 138 |
the return type hint. |
| 139 |
""" |
| 140 |
... |
| 141 |
|
| 142 |
documentation tips |
| 143 |
^^^^^^^^^^^^^^^^^^ |
| 144 |
|
| 145 |
Sometimes you may, or may not want certain class attributes to show up in the API |
| 146 |
docs. Depending on how and where you put the annotations and/or comments, you can |
| 147 |
control what gets picked up during the documentation building. Some examples are |
| 148 |
shown below. |
| 149 |
|
| 150 |
The following will show in docs WITHOUT docstring:: |
| 151 |
|
| 152 |
class Test: |
| 153 |
blah: int |
| 154 |
|
| 155 |
The following will show in docs WITH docstring:: |
| 156 |
|
| 157 |
class Test: |
| 158 |
#: :My description. |
| 159 |
blah: int |
| 160 |
|
| 161 |
The following will NOT show in the docs:: |
| 162 |
|
| 163 |
class Test: |
| 164 |
def __init__(self): |
| 165 |
self.blah: int = 0 |
| 166 |
|
| 167 |
|
| 168 |
The following will show in the docs:: |
| 169 |
|
| 170 |
class Test: |
| 171 |
def __init__(self): |
| 172 |
#: :This is documented. |
| 173 |
self.blah: int = 0 |
| 174 |
|
| 175 |
|
| 176 |
Developer reference |
| 177 |
------------------- |
| 178 |
Developer focused documentation is located in the ``pyglet/doc/internal/`` folder. |
| 179 |
This contains various pages about tools that are used with developing pyglet, the |
| 180 |
documentation page that you're reading now, information on unit tests, etc. These |
| 181 |
pages are useful for anyone who wants to contribute. |
| 182 |
|
| 183 |
building |
| 184 |
^^^^^^^^ |
| 185 |
Building the documentation locally requires a few dependencies. See :doc:`virtualenv` |
| 186 |
for more information on how to install them. Once you have Sphinx and it's dependencies |
| 187 |
installed, you can proceed with the build. The first way to build is by using pyglet's |
| 188 |
included `make.py` utility. This is found in the root of the repository, and includes |
| 189 |
some helper functions for common build and distribution related tasks. For docs, execute:: |
| 190 |
|
| 191 |
./make.py docs --open |
| 192 |
|
| 193 |
|
| 194 |
If the build succeeds, a browser window will open. You can skip this by omitting `--open`. |
| 195 |
The generated static web pages will be in ``doc/_build/html``. |
| 196 |
|
| 197 |
You can also build the documentation by using Sphinx's ``Makefile``: |
| 198 |
|
| 199 |
.. code:: bash |
| 200 |
|
| 201 |
cd pyglet/doc |
| 202 |
make html # Posix |
| 203 |
make.bat html # Windows |
| 204 |
|
| 205 |
|
| 206 |
Note that there are also additional build targets beyond HTML, but |
| 207 |
they may require installing additional dependencies. Run ``make help`` |
| 208 |
to see a list of them: |
| 209 |
|
| 210 |
.. code:: console |
| 211 |
|
| 212 |
$ make help |
| 213 |
Please use `make <target>' where <target> is one of |
| 214 |
html to make standalone HTML files |
| 215 |
dirhtml to make HTML files named index.html in directories |
| 216 |
singlehtml to make a single large HTML file |
| 217 |
pickle to make pickle files |
| 218 |
json to make JSON files |
| 219 |
htmlhelp to make HTML files and a HTML help project |
| 220 |
qthelp to make HTML files and a qthelp project |
| 221 |
devhelp to make HTML files and a Devhelp project |
| 222 |
epub to make an epub |
| 223 |
latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter |
| 224 |
latexpdf to make LaTeX files and run them through pdflatex |
| 225 |
text to make text files |
| 226 |
man to make manual pages |
| 227 |
texinfo to make Texinfo files |
| 228 |
info to make Texinfo files and run them through makeinfo |
| 229 |
gettext to make PO message catalogs |
| 230 |
changes to make an overview of all changed/added/deprecated items |
| 231 |
linkcheck to check all external links for integrity |
| 232 |
doctest to run all doctests embedded in the documentation (if enabled) |
| 233 |
|
| 234 |
|
| 235 |
auto-generated details |
| 236 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 237 |
|
| 238 |
.. include:: build.rst |
| 239 |
|
| 240 |
|
| 241 |
.. _Sphinx: https://sphinx-doc.org |
| 242 |
|
| 243 |
.. _reStructuredText: https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html |
| 244 |
|
| 245 |
.. _autodoc: https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html |