Debian Codegraph @

docs/devguide/style_guide.rst

raw
1 ================
2 Style Guidelines
3 ================
4
5 Python Code
6 -----------
7
8 * Generally follow Python's :pep:`8` style guidelines. We run Ruff to format
9 our code which should help ensure consistent style. Make sure to run the
10 following prior to committing changes:
11
12 .. code-block:: python
13
14 pre-commit install
15
16 You can also check all files prior to a commit by running:
17
18 .. code-block:: python
19
20 pre-commit run --all-files
21
22 * Break up logical blocks of related code with a newline. Specifically add a
23 blank newline after conditional or looping blocks.
24 * Don't comment what is obvious. Instead prefer meaningful names of functions
25 and variables:
26
27 .. code:: python
28
29 # Get the functions signal annotations <-- this comment is unnecessary
30 return_type, arg_types = get_signal_annotations(func)
31
32 * Use comments to explain non-obvious blocks and conditionals, magic,
33 workarounds (with bug references), or generally complex pieces of code.
34 Good examples:
35
36 .. code:: python
37
38 # If a property was defined with a decorator, it may already have
39 # a name; if it was defined with an assignment (prop = Property(...))
40 # we set the property's name to the member name
41 if not prop.name:
42 prop.name = name
43
44 .. code:: python
45
46 # Python causes MRO's to be calculated starting with the lowest
47 # base class and working towards the descendant, storing the result
48 # in __mro__ at each point. Therefore at this point we know that
49 # we already have our base class MRO's available to us, there is
50 # no need for us to (re)calculate them.
51 if hasattr(base, '__mro__'):
52 bases_of_subclasses += [list(base.__mro__)]
53
54
55 Python Doc Strings
56 ------------------
57
58 * Doc strings should generally follow
59 :pep:`257` unless noted here.
60 * Use `reStructuredText (reST) <https://www.sphinx-doc.org/en/master/usage/restructuredtext/>`__
61 annotations.
62 * Use three double quotes for doc strings (``"""``).
63 * Use a brief description on the same line as the triple quote.
64 * Include function parameter documentation (including types, returns, and
65 raises) between the brief description and the full description. Use a
66 newline with indentation for the parameters descriptions.
67
68 .. code:: python
69
70 def spam(amount):
71 """Creates a Spam object with the given amount.
72
73 :param int amount:
74 The amount of spam.
75 :returns:
76 A new Spam instance with the given amount set.
77 :rtype: Spam
78 :raises ValueError:
79 If amount is not a numeric type.
80
81 More complete description.
82 """
83
84 * For class documentation, use the classes doc string for an explanation of
85 what the class is used for and how it works, including Python examples.
86 Include ``__init__`` argument documentation after the brief description in
87 the classes doc string. The class ``__init__`` should generally be the first
88 method defined in a class putting it as close as possible (location wise) to
89 the class documentation.
90
91 .. code:: python
92
93 class Bacon(CookedFood):
94 """Bacon is a breakfast food.
95
96 :param CookingType cooking_type:
97 Enum for the type of cooking to use.
98 :param float cooking_time:
99 Amount of time used to cook the Bacon in minutes.
100
101 Use Bacon in combination with other breakfast foods for
102 a complete breakfast. For example, combine Bacon with
103 other items in a list to make a breakfast:
104
105 .. code-block:: python
106
107 breakfast = [Bacon(), Spam(), Spam(), Eggs()]
108
109 """
110 def __init__(self, cooking_type=CookingType.BAKE, cooking_time=15.0):
111 super(Bacon, self).__init__(cooking_type, cooking_time)