code_style.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. Code Style
  2. ==========
  3. Formatting tools
  4. ----------------
  5. The Synapse codebase uses a number of code formatting tools in order to
  6. quickly and automatically check for formatting (and sometimes logical) errors
  7. in code.
  8. The necessary tools are detailed below.
  9. - **black**
  10. The Synapse codebase uses `black <https://pypi.org/project/black/>`_ as an
  11. opinionated code formatter, ensuring all comitted code is properly
  12. formatted.
  13. First install ``black`` with::
  14. pip install --upgrade black
  15. Have ``black`` auto-format your code (it shouldn't change any functionality)
  16. with::
  17. black . --exclude="\.tox|build|env"
  18. - **flake8**
  19. ``flake8`` is a code checking tool. We require code to pass ``flake8`` before being merged into the codebase.
  20. Install ``flake8`` with::
  21. pip install --upgrade flake8
  22. Check all application and test code with::
  23. flake8 synapse tests
  24. - **isort**
  25. ``isort`` ensures imports are nicely formatted, and can suggest and
  26. auto-fix issues such as double-importing.
  27. Install ``isort`` with::
  28. pip install --upgrade isort
  29. Auto-fix imports with::
  30. isort -rc synapse tests
  31. ``-rc`` means to recursively search the given directories.
  32. It's worth noting that modern IDEs and text editors can run these tools
  33. automatically on save. It may be worth looking into whether this
  34. functionality is supported in your editor for a more convenient development
  35. workflow. It is not, however, recommended to run ``flake8`` on save as it
  36. takes a while and is very resource intensive.
  37. General rules
  38. -------------
  39. - **Naming**:
  40. - Use camel case for class and type names
  41. - Use underscores for functions and variables.
  42. - **Docstrings**: should follow the `google code style
  43. <https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings>`_.
  44. This is so that we can generate documentation with `sphinx
  45. <http://sphinxcontrib-napoleon.readthedocs.org/en/latest/>`_. See the
  46. `examples
  47. <http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html>`_
  48. in the sphinx documentation.
  49. - **Imports**:
  50. - Imports should be sorted by ``isort`` as described above.
  51. - Prefer to import classes and functions rather than packages or modules.
  52. Example::
  53. from synapse.types import UserID
  54. ...
  55. user_id = UserID(local, server)
  56. is preferred over::
  57. from synapse import types
  58. ...
  59. user_id = types.UserID(local, server)
  60. (or any other variant).
  61. This goes against the advice in the Google style guide, but it means that
  62. errors in the name are caught early (at import time).
  63. - Avoid wildcard imports (``from synapse.types import *``) and relative
  64. imports (``from .types import UserID``).
  65. Configuration file format
  66. -------------------------
  67. The `sample configuration file <./sample_config.yaml>`_ acts as a reference to
  68. Synapse's configuration options for server administrators. Remember that many
  69. readers will be unfamiliar with YAML and server administration in general, so
  70. that it is important that the file be as easy to understand as possible, which
  71. includes following a consistent format.
  72. Some guidelines follow:
  73. * Sections should be separated with a heading consisting of a single line
  74. prefixed and suffixed with ``##``. There should be **two** blank lines
  75. before the section header, and **one** after.
  76. * Each option should be listed in the file with the following format:
  77. * A comment describing the setting. Each line of this comment should be
  78. prefixed with a hash (``#``) and a space.
  79. The comment should describe the default behaviour (ie, what happens if
  80. the setting is omitted), as well as what the effect will be if the
  81. setting is changed.
  82. Often, the comment end with something like "uncomment the
  83. following to \<do action>".
  84. * A line consisting of only ``#``.
  85. * A commented-out example setting, prefixed with only ``#``.
  86. For boolean (on/off) options, convention is that this example should be
  87. the *opposite* to the default (so the comment will end with "Uncomment
  88. the following to enable [or disable] \<feature\>." For other options,
  89. the example should give some non-default value which is likely to be
  90. useful to the reader.
  91. * There should be a blank line between each option.
  92. * Where several settings are grouped into a single dict, *avoid* the
  93. convention where the whole block is commented out, resulting in comment
  94. lines starting ``# #``, as this is hard to read and confusing to
  95. edit. Instead, leave the top-level config option uncommented, and follow
  96. the conventions above for sub-options. Ensure that your code correctly
  97. handles the top-level option being set to ``None`` (as it will be if no
  98. sub-options are enabled).
  99. * Lines should be wrapped at 80 characters.
  100. Example::
  101. ## Frobnication ##
  102. # The frobnicator will ensure that all requests are fully frobnicated.
  103. # To enable it, uncomment the following.
  104. #
  105. #frobnicator_enabled: true
  106. # By default, the frobnicator will frobnicate with the default frobber.
  107. # The following will make it use an alternative frobber.
  108. #
  109. #frobincator_frobber: special_frobber
  110. # Settings for the frobber
  111. #
  112. frobber:
  113. # frobbing speed. Defaults to 1.
  114. #
  115. #speed: 10
  116. # frobbing distance. Defaults to 1000.
  117. #
  118. #distance: 100
  119. Note that the sample configuration is generated from the synapse code and is
  120. maintained by a script, ``scripts-dev/generate_sample_config``. Making sure
  121. that the output from this script matches the desired format is left as an
  122. exercise for the reader!