You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

code_style.md 4.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Code Style
  2. ## Formatting tools
  3. The Synapse codebase uses a number of code formatting tools in order to
  4. quickly and automatically check for formatting (and sometimes logical)
  5. errors in code.
  6. The necessary tools are:
  7. - [black](https://black.readthedocs.io/en/stable/), a source code formatter;
  8. - [isort](https://pycqa.github.io/isort/), which organises each file's imports;
  9. - [ruff](https://github.com/charliermarsh/ruff), which can spot common errors; and
  10. - [mypy](https://mypy.readthedocs.io/en/stable/), a type checker.
  11. See [the contributing guide](development/contributing_guide.md#run-the-linters) for instructions
  12. on how to install the above tools and run the linters.
  13. It's worth noting that modern IDEs and text editors can run these tools
  14. automatically on save. It may be worth looking into whether this
  15. functionality is supported in your editor for a more convenient
  16. development workflow. It is not, however, recommended to run `mypy`
  17. on save as it takes a while and can be very resource intensive.
  18. ## General rules
  19. - **Naming**:
  20. - Use `CamelCase` for class and type names
  21. - Use underscores for `function_names` and `variable_names`.
  22. - **Docstrings**: should follow the [google code
  23. style](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings).
  24. See the
  25. [examples](http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html)
  26. in the sphinx documentation.
  27. - **Imports**:
  28. - Imports should be sorted by `isort` as described above.
  29. - Prefer to import classes and functions rather than packages or
  30. modules.
  31. Example:
  32. ```python
  33. from synapse.types import UserID
  34. ...
  35. user_id = UserID(local, server)
  36. ```
  37. is preferred over:
  38. ```python
  39. from synapse import types
  40. ...
  41. user_id = types.UserID(local, server)
  42. ```
  43. (or any other variant).
  44. This goes against the advice in the Google style guide, but it
  45. means that errors in the name are caught early (at import time).
  46. - Avoid wildcard imports (`from synapse.types import *`) and
  47. relative imports (`from .types import UserID`).
  48. ## Configuration code and documentation format
  49. When adding a configuration option to the code, if several settings are grouped into a single dict, ensure that your code
  50. correctly handles the top-level option being set to `None` (as it will be if no sub-options are enabled).
  51. The [configuration manual](usage/configuration/config_documentation.md) acts as a
  52. reference to Synapse's configuration options for server administrators.
  53. Remember that many readers will be unfamiliar with YAML and server
  54. administration in general, so it is important that when you add
  55. a configuration option the documentation be as easy to understand as possible, which
  56. includes following a consistent format.
  57. Some guidelines follow:
  58. - Each option should be listed in the config manual with the following format:
  59. - The name of the option, prefixed by `###`.
  60. - A comment which describes the default behaviour (i.e. what
  61. happens if the setting is omitted), as well as what the effect
  62. will be if the setting is changed.
  63. - An example setting, using backticks to define the code block
  64. For boolean (on/off) options, convention is that this example
  65. should be the *opposite* to the default. For other options, the example should give
  66. some non-default value which is likely to be useful to the reader.
  67. - There should be a horizontal rule between each option, which can be achieved by adding `---` before and
  68. after the option.
  69. - `true` and `false` are spelt thus (as opposed to `True`, etc.)
  70. Example:
  71. ---
  72. ### `modules`
  73. Use the `module` sub-option to add a module under `modules` to extend functionality.
  74. The `module` setting then has a sub-option, `config`, which can be used to define some configuration
  75. for the `module`.
  76. Defaults to none.
  77. Example configuration:
  78. ```yaml
  79. modules:
  80. - module: my_super_module.MySuperClass
  81. config:
  82. do_thing: true
  83. - module: my_other_super_module.SomeClass
  84. config: {}
  85. ```
  86. ---
  87. Note that the sample configuration is generated from the synapse code
  88. and is maintained by a script, `scripts-dev/generate_sample_config.sh`.
  89. Making sure that the output from this script matches the desired format
  90. is left as an exercise for the reader!