MatrixSynapse/docs/code_style.rst

117 lines
3.3 KiB
ReStructuredText
Raw Normal View History

# Code Style
2017-10-26 11:42:06 +02:00
The Synapse codebase uses a number of code formatting tools in order to
quickly and automatically check for formatting (and sometimes logical) errors
in code.
2017-10-26 11:42:06 +02:00
The necessary tools are detailed below.
2017-10-26 11:42:06 +02:00
## Formatting tools
2017-10-26 11:42:06 +02:00
The Synapse codebase uses [black](https://pypi.org/project/black/) as an
opinionated code formatter, ensuring all comitted code is properly
formatted.
2017-10-26 11:42:06 +02:00
First install ``black`` with::
2017-10-26 11:42:06 +02:00
pip install --upgrade black
2017-10-26 11:42:06 +02:00
Have ``black`` auto-format your code (it shouldn't change any
functionality) with::
2017-10-26 11:42:06 +02:00
black . --exclude="\.tox|build|env"
2017-10-26 11:42:06 +02:00
- **flake8**
2017-10-26 11:42:06 +02:00
``flake8`` is a code checking tool. We require code to pass ``flake8`` before being merged into the codebase.
2017-10-26 11:42:06 +02:00
Install ``flake8`` with::
2017-10-26 11:42:06 +02:00
pip install --upgrade flake8
2017-10-26 11:42:06 +02:00
Check all application and test code with::
2017-10-26 11:42:06 +02:00
flake8 synapse tests
2017-10-26 11:42:06 +02:00
- **isort**
``isort`` ensures imports are nicely formatted, and can suggest and
auto-fix issues such as double-importing.
2017-10-26 11:42:06 +02:00
Install ``isort`` with::
2017-10-26 11:42:06 +02:00
pip install --upgrade isort
2017-10-26 11:42:06 +02:00
Auto-fix imports with::
2014-08-12 16:10:52 +02:00
isort -rc synapse tests
``-rc`` means to recursively search the given directories.
It's worth noting that modern IDEs and text editors can run these tools
automatically on save. It may be worth looking into whether this
functionality is supported in your editor for a more convenient development
workflow. It is not, however, recommended to run ``flake8`` on save as it
takes a while and is very resource intensive.
## General rules
- **Naming**:
- Use camel case for class and type names
- Use underscores for functions and variables.
- Use double quotes ``"foo"`` rather than single quotes ``'foo'``.
2017-10-26 11:42:06 +02:00
- **Comments**: should follow the `google code style
2017-10-26 11:28:41 +02:00
<http://google.github.io/styleguide/pyguide.html?showone=Comments#Comments>`_.
This is so that we can generate documentation with `sphinx
<http://sphinxcontrib-napoleon.readthedocs.org/en/latest/>`_. See the
`examples
<http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html>`_
in the sphinx documentation.
2017-10-26 11:58:34 +02:00
- **Imports**:
- Prefer to import classes and functions rather than packages or modules.
2017-10-26 11:58:34 +02:00
Example::
from synapse.types import UserID
...
user_id = UserID(local, server)
is preferred over::
from synapse import types
...
user_id = types.UserID(local, server)
(or any other variant).
This goes against the advice in the Google style guide, but it means that
errors in the name are caught early (at import time).
- Multiple imports from the same package can be combined onto one line::
from synapse.types import GroupID, RoomID, UserID
An effort should be made to keep the individual imports in alphabetical
order.
If the list becomes long, wrap it with parentheses and split it over
multiple lines.
- As per `PEP-8 <https://www.python.org/dev/peps/pep-0008/#imports>`_,
imports should be grouped in the following order, with a blank line between
each group:
1. standard library imports
2. related third party imports
3. local application/library specific imports
- Imports within each group should be sorted alphabetically by module name.
- Avoid wildcard imports (``from synapse.types import *``) and relative
imports (``from .types import UserID``).