When writing code for snapcraft, we try to follow a set of rules that will lead to consistency and readability.
This is a permanent work in progress, and sometimes being too strict with the rules can end up making things actually less readable. So when you disagree with one of the rules, please talk to us and help us making it better.
Some of the rules are enforced with static tests. You can read the TESTING document for more information and details about how to run the static suite of tests. Some other rules are only socially enforced during code reviews.
This code base adheres to black2.
You can snap install black --beta --devmode
to install the linter and formatter onto your host.
- Always check for expected value e.g.;
if foo is True
instead ofif foo
orif foo is not None
.
- Return only once from a method unless it is through a guard.
- Method names should start with a =verb= unless it is a
@property
. - Attribute names should be a
noun
.
classmethods
should be defined before__init__
.- Serialization/marshalling/dumping shall use methods named
marshal
andunmarshal
but if specific types are serialized we append the information type, e.g.;unmarshal_dict
.
- Always use generators, list comprehensions, reduce and functional resolution when possible.
For multiline strings, we prefer to use textwrap.dedent
:
# end first line with \ to avoid the empty line!
s = textwrap.dedent("""\
hello
world
""")
print(repr(s)) # prints 'hello\n world\n'
(from https://docs.python.org/3/library/textwrap.html#textwrap.dedent)
Error messages must say what happened, why it happened and what you can do to fix it.
-
When asserting for equality, we prefer to use the
Equals
matcher from testtools:self.assertThat(actual, Equals(expected))
-
When writing unit tests that raise errors, the tests should only check the class of the exception raised and it's attributes, not the format of the error message. The formatting of the exception as a string should be tested only once, in the module tests/unit/test_errors.py
Generally speaking, all comments should end with some punctuation.