Difference between revisions of "Python style guide"

From Software Heritage Wiki
Jump to: navigation, search
m (1 revision: import public pages from the intranet wiki)
Line 37: Line 37:
  
  
 +
[[Category:Guidelines]]
 
[[Category:Software development]]
 
[[Category:Software development]]
 
[[Category:Python]]
 
[[Category:Python]]

Revision as of 07:09, 22 July 2016

Coding style and best practices for writing Python code for Software Heritage.

General rules

  • As a general rule, follow the Google Python Style Guide.
  • Target Python 3. Do not care about backward compatibility with Python 2.

Specific rules

As supplement/overrides to the above general rules, follow the additional recommendations below.

Lint

  • Make sure your code is flake8 clean.

Tests

  • use unittest for assertions, nosetests3 as test runner
  • put tests/ dir down deep in the module hierarchy, near to the code being tested
  • naming conventions:
    • tests/test_mymodule.py
    • class TestMyEntity(unittest.TestCase)
    • def behavior(self):
      • do not prepend test_ to all test methods; use nose's @istest decorator instead

Classes

Strings

  • Prefer 'single quotes' over "double quotes". Do otherwise only when needed, e.g., for strings that should contain single quotes

See also