Skip to content

Latest commit

 

History

History
51 lines (35 loc) · 1.6 KB

CODE_STYLE.md

File metadata and controls

51 lines (35 loc) · 1.6 KB

Snapcraft coding style guidelines

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.

PEP 8

We adhere to the Style Guide for Python Code documented in the PEP 8.

Multiline strings

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)

Errors

Error messages must say what happened, why it happened and what you can do to fix it.

Tests

  • 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 snapcraft/tests/unit/test_errors.py