Skip to content
Jukka Lehtosalo edited this page Mar 30, 2016 · 7 revisions

Python conventions

Follow PEP 8.

We use the flake8 linter to enforce style rules. This greatly simplifies code reviews.

Exceptions:

  • We use 99 characters as the maximum line length.
  • Use spaces around = signs in default parameter values in functions with annotations:
def f(x: int = 1) -> None: ...  # OK, since f is annotated

def f(x=1) -> None: ...         # OK, fall back to PEP 8 if there is no annotation

Stub conventions

Stubs mostly follow Python conventions (see above). We use some modified conventions, mostly to make stubs more compact:

  • Use literal ellipsis (...) instead of pass in empty function and class bodies. You can also use it for default argument values and variable/attribute initializers.
  • Only have a single blank line between top-level class definitions. For very small classes, no blank line is needed between classes.
  • No empty lines between function definitions.
  • You can add extra empty lines for grouping (sparingly).
  • Prefer to have the ... on the same line as the function definition.
  • No docstrings.
  • Add comment such as # Stubs for foobar (followed by an empty line) to the top so that it's obvious that this is not a normal source file.