blob: c136bec4bcef82fa46827980be3de6d6af69d422 [file] [log] [blame] [view]
Christopher Wileyfe2ea872016-02-02 12:12:18 -08001# Coding style for autotest in Chrome OS / Android / Brillo
Christopher Wiley2f011d92016-02-04 09:35:32 -08002These rules elaborate on, but rarely deviate from PEP-8. When in doubt, go
3with PEP-8.
mbligh71d338a2007-10-08 05:05:50 +00004
5
Christopher Wileyfe2ea872016-02-02 12:12:18 -08006## Language
Christopher Wiley2f011d92016-02-04 09:35:32 -08007 * Use Python where possible
8 * Prefer writing more Python to a smaller amount of shell script in host
9 commands. In practice, the Python tends to be easier to maintain.
10 * Some tests use C/C++ in test dependencies, and this is also ok.
mbligh2ac475b2008-09-09 21:37:40 +000011
12
Christopher Wileyfe2ea872016-02-02 12:12:18 -080013## Indentation & whitespace
mbligh71d338a2007-10-08 05:05:50 +000014
15Format your code for an 80 character wide screen.
16
Christopher Wiley2f011d92016-02-04 09:35:32 -080017Indentation is 4 spaces, as opposed to hard tabs (which it used to be).
mblighc960fcf2008-06-18 19:58:57 +000018This is the Python standard.
mbligh71d338a2007-10-08 05:05:50 +000019
Tan Gao8aac17b2013-04-16 08:46:01 -070020For hanging indentation, use 8 spaces plus all args should be on the new line.
21
Christopher Wileyfe2ea872016-02-02 12:12:18 -080022```
Christopher Wiley2f011d92016-02-04 09:35:32 -080023
Tan Gao8aac17b2013-04-16 08:46:01 -070024 # Either of the following hanging indentation is considered acceptable.
25YES: return 'class: %s, host: %s, args = %s' % (
26 self.__class__.__name__, self.hostname, self.args)
27
28YES: return 'class: %s, host: %s, args = %s' % (
29 self.__class__.__name__,
30 self.hostname,
31 self.args)
32
33 # Do not use 4 spaces for hanging indentation
34NO: return 'class: %s, host: %s, args = %s' % (
35 self.__class__.__name__, self.hostname, self.args)
36
37 # Do put all args on new line
38NO: return 'class: %s, host: %s, args = %s' % (self.__class__.__name__,
39 self.hostname, self.args)
Christopher Wileyfe2ea872016-02-02 12:12:18 -080040```
Tan Gao8aac17b2013-04-16 08:46:01 -070041
mbligh71d338a2007-10-08 05:05:50 +000042Don't leave trailing whitespace, or put whitespace on blank lines.
Simran Basic7330bd2013-05-31 09:23:50 -070043
mbligh71d338a2007-10-08 05:05:50 +000044
Christopher Wileyfe2ea872016-02-02 12:12:18 -080045## Variable names and UpPeR cAsE
Christopher Wiley2f011d92016-02-04 09:35:32 -080046 * Use descriptive variable names where possible
47 * Use `variable_names_like_this`
48 * Use `method_and_function_names_like_this`
49 * Use `UpperCamelCase` for class names
mbligh71d338a2007-10-08 05:05:50 +000050
mblighd876f452008-12-03 15:09:17 +000051
Christopher Wileyfe2ea872016-02-02 12:12:18 -080052## Importing modules
mbligh7654c822008-04-04 15:12:48 +000053
54The order of imports should be as follows:
55
Christopher Wiley2f011d92016-02-04 09:35:32 -080056 * Standard python modules
57 * Non-standard python modules
58 * Autotest modules
mbligh7654c822008-04-04 15:12:48 +000059
60Within one of these three sections, all module imports using the from
61keyword should appear after regular imports.
Christopher Wileyd7445ef2014-12-05 13:26:05 -080062Each module should be imported on its own line.
Christopher Wiley2f011d92016-02-04 09:35:32 -080063Do not use Wildcard imports (`from x import *`) if possible.
64
65Import modules, not classes. For example:
Christopher Wileyfe2ea872016-02-02 12:12:18 -080066
67```
mbligh7654c822008-04-04 15:12:48 +000068from common_lib import error
Christopher Wiley2f011d92016-02-04 09:35:32 -080069
70def foo():
71 raise error.AutoservError(...)
Christopher Wileyfe2ea872016-02-02 12:12:18 -080072```
73
74and not:
75
76```
mbligh7654c822008-04-04 15:12:48 +000077from common_lib.error import AutoservError
Christopher Wileyfe2ea872016-02-02 12:12:18 -080078```
mbligh7654c822008-04-04 15:12:48 +000079
80For example:
Christopher Wileyfe2ea872016-02-02 12:12:18 -080081
82```
Christopher Wileyd7445ef2014-12-05 13:26:05 -080083import os
84import pickle
85import random
86import re
87import select
88import shutil
89import signal
90import subprocess
91
mbligh8bcd23a2009-02-03 19:14:06 +000092import common # Magic autotest_lib module and sys.path setup code.
93import MySQLdb # After common so that we check our site-packages first.
Christopher Wileyd7445ef2014-12-05 13:26:05 -080094
mbligh7654c822008-04-04 15:12:48 +000095from common_lib import error
Christopher Wileyfe2ea872016-02-02 12:12:18 -080096```
mbligh7654c822008-04-04 15:12:48 +000097
Christopher Wileyfe2ea872016-02-02 12:12:18 -080098## Testing None
mblighd876f452008-12-03 15:09:17 +000099
Christopher Wiley2f011d92016-02-04 09:35:32 -0800100Use `is None` rather than `== None` and `is not None` rather than `!= None`.
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800101This way you'll never run into a case where someone's `__eq__` or `__ne__`
Christopher Wiley2f011d92016-02-04 09:35:32 -0800102method does the wrong thing.
mblighd876f452008-12-03 15:09:17 +0000103
mbligh71d338a2007-10-08 05:05:50 +0000104
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800105## Comments
mbligh71d338a2007-10-08 05:05:50 +0000106
107Generally, you want your comments to tell WHAT your code does, not HOW.
108We can figure out how from the code itself (or if not, your code needs fixing).
109
110Try to describle the intent of a function and what it does in a triple-quoted
111(multiline) string just after the def line. We've tried to do that in most
mbligh52207362009-09-03 20:54:29 +0000112places, though undoubtedly we're not perfect. A high level overview is
mbligh71d338a2007-10-08 05:05:50 +0000113incredibly helpful in understanding code.
114
115
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800116## Hardcoded String Formatting
Simran Basic7330bd2013-05-31 09:23:50 -0700117
Dana Goyette9cf68d12019-07-16 10:30:18 -0700118Strings should use only single quotes for hardcoded strings in the code.
119Double quotes are acceptable when single quote is used as part of the string.
Christopher Wiley2f011d92016-02-04 09:35:32 -0800120Multiline strings should not use '\' but wrap the string using parentheseses.
Simran Basic7330bd2013-05-31 09:23:50 -0700121
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800122```
Simran Basic7330bd2013-05-31 09:23:50 -0700123REALLY_LONG_STRING = ('This is supposed to be a really long string that is '
124 'over 80 characters and does not use a slash to '
125 'continue.')
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800126```
Simran Basic7330bd2013-05-31 09:23:50 -0700127
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800128## Docstrings
mbligh5cad50f2009-06-08 16:50:51 +0000129
130Docstrings are important to keep our code self documenting. While it's not
131necessary to overdo documentation, we ask you to be sensible and document any
132nontrivial function. When creating docstrings, please add a newline at the
showard25f056a2009-11-23 20:22:50 +0000133beginning of your triple quoted string and another newline at the end of it. If
134the docstring has multiple lines, please include a short summary line followed
135by a blank line before continuing with the rest of the description. Please
136capitalize and punctuate accordingly the sentences. If the description has
137multiple lines, put two levels of indentation before proceeding with text. An
138example docstring:
mbligh5cad50f2009-06-08 16:50:51 +0000139
Dana Goyette9cf68d12019-07-16 10:30:18 -0700140```python
mbligh5cad50f2009-06-08 16:50:51 +0000141def foo(param1, param2):
mbligh52207362009-09-03 20:54:29 +0000142 """
showard25f056a2009-11-23 20:22:50 +0000143 Summary line.
144
mbligh52207362009-09-03 20:54:29 +0000145 Long description of method foo.
mbligh5cad50f2009-06-08 16:50:51 +0000146
Dana Goyette9cf68d12019-07-16 10:30:18 -0700147 @param param1: A Spam object that has methods bar() and baz()
148 which raise SpamError if something goes awry.
149 @type param1: Spam
Simran Basic7330bd2013-05-31 09:23:50 -0700150
Dana Goyette9cf68d12019-07-16 10:30:18 -0700151 @return: a list of integers describing changes in a source tree
152 @rtype: list
Simran Basic7330bd2013-05-31 09:23:50 -0700153
Dana Goyette9cf68d12019-07-16 10:30:18 -0700154 @raise SpamError: when some stated condition occurs.
Simran Basic7330bd2013-05-31 09:23:50 -0700155
mbligh52207362009-09-03 20:54:29 +0000156 """
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800157```
mbligh5cad50f2009-06-08 16:50:51 +0000158
159The tags that you can put inside your docstring are tags recognized by systems
Dana Goyette9cf68d12019-07-16 10:30:18 -0700160like epydoc. Not all places need all tags defined, so choose them wisely while
161writing code. Generally always list any parameters, the return value (if there
162is one), and exceptions that can be raised.
mbligh5cad50f2009-06-08 16:50:51 +0000163
Dana Goyette9cf68d12019-07-16 10:30:18 -0700164| Tag | Description
165|----------|-------------------------------------------------------------------
166| @author | Code author
167| @cvar | Class variable (defined on `self.__class__.`)
168| @ivar | Instance variable for a class (defined on `self.`)
169| @param | Parameter description
170| @raise | Exception type the function can throw, and the conditions for it
171| @return | Return value description
172| @rtype | The type the method returns
173| @see | Reference to other parts of the codebase
174| @type | The type of a given parameter or variable
175| @warning | Call attention to potential problems with the code
176| @version | Version string
mbligh5cad50f2009-06-08 16:50:51 +0000177
Dana Goyette9cf68d12019-07-16 10:30:18 -0700178For additional information and fields, refer to the epydoc manual:
179http://epydoc.sourceforge.net/manual-fields.html
mbligh3bdba922010-05-03 18:02:54 +0000180
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800181## Simple code
mbligh71d338a2007-10-08 05:05:50 +0000182
183Keep it simple; this is not the right place to show how smart you are. We
184have plenty of system failures to deal with without having to spend ages
185figuring out your code, thanks ;-) Readbility, readability, readability.
Christopher Wiley2f011d92016-02-04 09:35:32 -0800186Strongly prefer readability and simplicity over compactness.
mbligh71d338a2007-10-08 05:05:50 +0000187
188"Debugging is twice as hard as writing the code in the first place. Therefore,
mbligh52207362009-09-03 20:54:29 +0000189if you write the code as cleverly as possible, you are, by definition, not
mbligh71d338a2007-10-08 05:05:50 +0000190smart enough to debug it." Brian Kernighan
191
192
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800193## Function length
mbligh71d338a2007-10-08 05:05:50 +0000194
195Please keep functions short, under 30 lines or so if possible. Even though
Christopher Wiley2f011d92016-02-04 09:35:32 -0800196you are amazingly clever, and can cope with it, the rest of us are busy and
197stupid, so be nice and help us out. To quote the Linux Kernel coding style:
mbligh71d338a2007-10-08 05:05:50 +0000198
199Functions should be short and sweet, and do just one thing. They should
200fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
201as we all know), and do one thing and do that well.
202
203
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800204## Exceptions
mbligh900b6c12008-01-14 16:56:47 +0000205
206When raising exceptions, the preferred syntax for it is:
207
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800208```
mbligh900b6c12008-01-14 16:56:47 +0000209raise FooException('Exception Message')
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800210```
mbligh900b6c12008-01-14 16:56:47 +0000211
212Please don't raise string exceptions, as they're deprecated and will be removed
213from future versions of python. If you're in doubt about what type of exception
214you will raise, please look at http://docs.python.org/lib/module-exceptions.html
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800215and client/common\_lib/error.py, the former is a list of python built in
mbligh900b6c12008-01-14 16:56:47 +0000216exceptions and the later is a list of autotest/autoserv internal exceptions. Of
217course, if you really need to, you can extend the exception definitions on
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800218client/common\_lib/error.py.
mbligh900b6c12008-01-14 16:56:47 +0000219
220
Christopher Wileyfe2ea872016-02-02 12:12:18 -0800221## Submitting patches
mbligh71d338a2007-10-08 05:05:50 +0000222
Christopher Wiley2f011d92016-02-04 09:35:32 -0800223Submit changes through the Chrome OS gerrit instance. This process is
224documented on
225[chromium.org](http://dev.chromium.org/developers/contributing-code).