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
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:
pre-commit run --all
Python¶
The Python code follows PEP 8 and is linted and formatted using 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.
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.:opt:– mark command options and command values.:opt:`-P`gives-P:opt:`--progress=dots`gives--progress=dots:opt:`dots`givesdots
:man:– link to a Linux man page.:man:`msgfmt`gives 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:
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:
$ cd docs
$ make html # Build all Sphinx documentation
$ make linkcheck # Report broken links
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:
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 thatrather thanDoes ...,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:
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:
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:
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: URLand reference withlabel_
- 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:
# ... LICENSE BLOCK... """A brief description"""
- Deprecation:
Document the deprecation and version when deprecating features:
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()