Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add info about black to code_style.rst #5537

Merged
merged 5 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5537.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add information about how to install and run `black` on the codebase to code_style.rst.
87 changes: 42 additions & 45 deletions docs/code_style.rst
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
- Everything should comply with PEP8. Code should pass
``pep8 --max-line-length=100`` without any warnings.
# Code Style

- **Indenting**:
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.

- NEVER tabs. 4 spaces to indent.
The necessary tools are detailed below.

- follow PEP8; either hanging indent or multiline-visual indent depending
on the size and shape of the arguments and what makes more sense to the
author. In other words, both this::
## Formatting tools

print("I am a fish %s" % "moo")
The Synapse codebase uses [black](https://pypi.org/project/black/) as an
opinionated code formatter, ensuring all comitted code is properly
formatted.

and this::
First install ``black`` with::

print("I am a fish %s" %
"moo")
pip install --upgrade black

and this::
Have ``black`` auto-format your code (it shouldn't change any
functionality) with::

print(
"I am a fish %s" %
"moo",
)
black . --exclude="\.tox|build|env"

...are valid, although given each one takes up 2x more vertical space than
the previous, it's up to the author's discretion as to which layout makes
most sense for their function invocation. (e.g. if they want to add
comments per-argument, or put expressions in the arguments, or group
related arguments together, or want to deliberately extend or preserve
vertical/horizontal space)
- **flake8**

- **Line length**:
``flake8`` is a code checking tool. We require code to pass ``flake8`` before being merged into the codebase.

Max line length is 79 chars (with flexibility to overflow by a "few chars" if
the overflowing content is not semantically significant and avoids an
explosion of vertical whitespace).
Install ``flake8`` with::

Use parentheses instead of ``\`` for line continuation where ever possible
(which is pretty much everywhere).
pip install --upgrade flake8

- **Naming**:
Check all application and test code with::

- Use camel case for class and type names
- Use underscores for functions and variables.
flake8 synapse tests

- Use double quotes ``"foo"`` rather than single quotes ``'foo'``.
- **isort**

``isort`` ensures imports are nicely formatted, and can suggest and
auto-fix issues such as double-importing.

- **Blank lines**:
Install ``isort`` with::

- There should be max a single new line between:
pip install --upgrade isort

- statements
- functions in a class
Auto-fix imports with::

- There should be two new lines between:
isort -rc synapse tests

- definitions in a module (e.g., between different classes)
``-rc`` means to recursively search the given directories.

- **Whitespace**:
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.

There should be spaces where spaces should be and not where there shouldn't
be:
## General rules

- a single space after a comma
- a single space before and after for '=' when used as assignment
- no spaces before and after for '=' for default values and keyword arguments.
- **Naming**:

- Use camel case for class and type names
- Use underscores for functions and variables.

- Use double quotes ``"foo"`` rather than single quotes ``'foo'``.

- **Comments**: should follow the `google code style
<http://google.github.io/styleguide/pyguide.html?showone=Comments#Comments>`_.
Expand All @@ -76,7 +73,7 @@

- **Imports**:

- Prefer to import classes and functions than packages or modules.
- Prefer to import classes and functions rather than packages or modules.

Example::

Expand Down