1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
.. _styleguide:
Translate Styleguide
====================
The Translate styleguide is the styleguide for all Translate projects,
including Translate Toolkit, Pootle, Virtaal and others. Patches are required
to follow these guidelines.
pre-commit hooks
----------------
The Translate styleguide can be checked by `pre-commit`_. The Translate toolkit
repository repository contains configuration for it to verify the committed
files are sane. After installing it (it is already included in the
:file:`pyproject.toml`) turn it on by running ``pre-commit install`` in
Translate toolkit checkout. This way all your changes will be automatically
checked.
You can also trigger check manually, to check all files run:
.. code-block:: sh
pre-commit run --all
.. _pre-commit: https://pre-commit.com/
.. _styleguide-python:
Python
------
The Python code follows :pep:`8` and is linted and formatted using `ruff
<https://docs.astral.sh/ruff/>`_. It is included the above-mentioned pre-commit
hooks.
Any new code should utilize :pep:`484` type hints. We're not checking this in
our CI yet as existing code does not yet include them.
.. _styleguide-docs:
Documentation
-------------
We use Sphinx_ to generate our API and user documentation. Read the
`reStructuredText primer`_ and `Sphinx documentation`_ as needed.
Special roles
^^^^^^^^^^^^^
We introduce a number of special roles for documentation:
* ``:issue:`` -- links to a toolkit issue Github.
* ``:issue:`234``` gives: :issue:`234`
* ``:issue:`broken <234>``` gives: :issue:`broken <234>`
* ``:opt:`` -- mark command options and command values.
* ``:opt:`-P``` gives :opt:`-P`
* ``:opt:`--progress=dots``` gives :opt:`--progress=dots`
* ``:opt:`dots``` gives :opt:`dots`
* ``:man:`` -- link to a Linux man page.
* ``:man:`msgfmt``` gives :man:`msgfmt`
Code and command line highlighting
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
All code examples and format snippets should be highlighted to make them easier
to read. By default Sphinx uses Python highlighting of code snippets (but it
doesn't always work). You will want to change that in these situations:
.. highlight:: rest
* The examples are not Python e.g. talking about INI file parsing. In which
case set the file level highlighting using::
.. highlight:: ini
* There are multiple different code examples in the document, then use::
.. code-block:: ruby
before each code block.
* Python code highlighting isn't working, then force Python highlighting using::
.. code-block:: python
.. note:: Generally we prefer explicit markup as this makes it easier for those
following you to know what you intended. So use ``.. code-block:: python``
even though in some cases this is not required.
With *command line examples*, to improve readability use::
.. code-block:: console
Add ``$`` command prompt markers and ``#`` comments as required, as shown in
this example:
.. code-block:: console
$ cd docs
$ make html # Build all Sphinx documentation
$ make linkcheck # Report broken links
.. highlight:: python
User documentation
------------------
This is documentation found in ``docs/`` and that is published on Read the
Docs. The target is the end user so our primary objective is to make accessible,
readable and beautiful documents for them.
Docstrings
----------
All docstrings should follow :pep:`257` (Docstring Conventions) and be formatted
with reStructuredText as understood by Sphinx.
Basic formatting:
Depending on the number of lines in the docstring, they are laid out
differently. If it's just one line, the closing triple quote is on the same
line as the opening, otherwise the text is on the same line as the opening
quote and the triple quote that closes the string on its own line:
.. code-block:: python
def foo():
"""This is a simple docstring."""
def bar():
"""This is a longer docstring with so much information in there
that it spans three lines. In this case the closing triple quote
is on its own line.
"""
Key guidelines from :pep:`257`:
- A docstring should have a brief one-line summary, ending with a period. Use
the imperative mood: ``Do this``, ``Return that`` rather than ``Does ...``,
``Returns ...``.
- If there are more details there should be a blank line between the one-line
summary and the rest of the text. Use paragraphs and formatting as needed.
- Use proper capitalisation and punctuation.
- All public modules, functions, classes, and methods should have docstrings.
Type annotations and parameters:
**Use type annotations instead of documenting parameters in docstrings.**
For new code, always add :pep:`484` type hints to function signatures. This
makes parameter types and return types explicit and machine-readable:
.. code-block:: python
def addunit(self, unit: TranslationUnit) -> None:
"""Append the given unit to the object's list of units.
This method should always be used rather than trying to modify the
list manually.
"""
self.units.append(unit)
Type annotations are preferred over `reST field lists`_ for documenting
parameter types. Only use field lists when additional explanation beyond the
type is necessary, or when working with legacy code that doesn't have type
annotations:
.. code-block:: python
def legacy_function(bar):
"""Simple docstring for legacy code without type annotations.
:param str bar: Description of what bar represents
:return: Description of return value
:rtype: int
"""
Cross-referencing code:
When talking about other objects, methods, functions and variables,
cross-reference them using Sphinx's `Python cross-referencing`_ syntax:
- ```:class:`ClassName````` -- reference a class
- ```:func:`function_name````` -- reference a function
- ```:meth:`method_name````` -- reference a method
- ```:mod:`module_name````` -- reference a module
- ```:attr:`attribute_name````` -- reference an attribute
Example:
.. code-block:: python
def process_unit(unit: TranslationUnit) -> None:
"""Process a translation unit.
This delegates to :func:`validate_unit` and updates the
:class:`TranslationStore`.
"""
Linking to external documentation:
Use standard reStructuredText syntax for external links in docstrings:
- Inline links: ``Link text <URL>`_``
- Reference links: Define once with ``.. _label: URL`` and reference with
``label_``
Other directives:
Use `paragraph-level markup`_ when needed, such as:
- ``.. note::`` for important information
- ``.. warning::`` for warnings
- ``.. versionadded::`` and ``.. deprecated::`` for version information
Module header:
The module header consists of a utf-8 encoding declaration, copyright
attribution, license block and a standard docstring:
.. code-block:: python
#
... LICENSE BLOCK...
"""A brief description"""
.. """
package.module
~~~~~~~~~~~~~~
.. A brief description goes here.
.. :copyright: (c) YEAR by AUTHOR.
:license: LICENSE_NAME, see LICENSE_FILE for more details.
"""
Deprecation:
Document the deprecation and version when deprecating features:
.. code-block:: python
from translate.misc.deprecation import deprecated
@deprecated("Use util.run_fast() instead.")
def run_slow():
"""Run fast
.. deprecated:: 1.5
Use :func:`run_fast` instead.
"""
run_fast()
.. _reST field lists: http://sphinx-doc.org/domains.html#info-field-lists
.. _Python cross-referencing: http://sphinx-doc.org/domains.html#cross-referencing-python-objects
.. _Sphinx: http://sphinx-doc.org/
.. _reStructuredText primer: http://sphinx-doc.org/rest.html
.. _Sphinx documentation: http://sphinx-doc.org/contents.html
.. _paragraph-level markup: http://sphinx-doc.org/markup/para.html#paragraph-level-markup
|