code_style.rst 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. - Everything should comply with PEP8. Code should pass
  2. ``pep8 --max-line-length=100`` without any warnings.
  3. - **Indenting**:
  4. - NEVER tabs. 4 spaces to indent.
  5. - follow PEP8; either hanging indent or multiline-visual indent depending
  6. on the size and shape of the arguments and what makes more sense to the
  7. author. In other words, both this::
  8. print("I am a fish %s" % "moo")
  9. and this::
  10. print("I am a fish %s" %
  11. "moo")
  12. and this::
  13. print(
  14. "I am a fish %s" %
  15. "moo",
  16. )
  17. ...are valid, although given each one takes up 2x more vertical space than
  18. the previous, it's up to the author's discretion as to which layout makes
  19. most sense for their function invocation. (e.g. if they want to add
  20. comments per-argument, or put expressions in the arguments, or group
  21. related arguments together, or want to deliberately extend or preserve
  22. vertical/horizontal space)
  23. - **Line length**:
  24. Max line length is 79 chars (with flexibility to overflow by a "few chars" if
  25. the overflowing content is not semantically significant and avoids an
  26. explosion of vertical whitespace).
  27. Use parentheses instead of ``\`` for line continuation where ever possible
  28. (which is pretty much everywhere).
  29. - **Naming**:
  30. - Use camel case for class and type names
  31. - Use underscores for functions and variables.
  32. - Use double quotes ``"foo"`` rather than single quotes ``'foo'``.
  33. - **Blank lines**:
  34. - There should be max a single new line between:
  35. - statements
  36. - functions in a class
  37. - There should be two new lines between:
  38. - definitions in a module (e.g., between different classes)
  39. - **Whitespace**:
  40. There should be spaces where spaces should be and not where there shouldn't
  41. be:
  42. - a single space after a comma
  43. - a single space before and after for '=' when used as assignment
  44. - no spaces before and after for '=' for default values and keyword arguments.
  45. - **Comments**: should follow the `google code style
  46. <http://google.github.io/styleguide/pyguide.html?showone=Comments#Comments>`_.
  47. This is so that we can generate documentation with `sphinx
  48. <http://sphinxcontrib-napoleon.readthedocs.org/en/latest/>`_. See the
  49. `examples
  50. <http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html>`_
  51. in the sphinx documentation.
  52. - **Imports**:
  53. - Prefer to import classes and functions than packages or modules.
  54. Example::
  55. from synapse.types import UserID
  56. ...
  57. user_id = UserID(local, server)
  58. is preferred over::
  59. from synapse import types
  60. ...
  61. user_id = types.UserID(local, server)
  62. (or any other variant).
  63. This goes against the advice in the Google style guide, but it means that
  64. errors in the name are caught early (at import time).
  65. - Multiple imports from the same package can be combined onto one line::
  66. from synapse.types import GroupID, RoomID, UserID
  67. An effort should be made to keep the individual imports in alphabetical
  68. order.
  69. If the list becomes long, wrap it with parentheses and split it over
  70. multiple lines.
  71. - As per `PEP-8 <https://www.python.org/dev/peps/pep-0008/#imports>`_,
  72. imports should be grouped in the following order, with a blank line between
  73. each group:
  74. 1. standard library imports
  75. 2. related third party imports
  76. 3. local application/library specific imports
  77. - Imports within each group should be sorted alphabetically by module name.
  78. - Avoid wildcard imports (``from synapse.types import *``) and relative
  79. imports (``from .types import UserID``).