Skip to content

Google Python Style Guide Rules

Sourcery ships with a set of optional rules that, combined, implement a portion of the Google Python Style Guide.

It also defines several subsets of optional rules that you can switch on and off individually:

The rules can be found in the sourcery-rules public repo

Usage

One-off Review in the CLI

To review your project with all these rules in the command line, you can run:

sourcery review --enable gpsg .

You can also pick only a subset of these rules. For example, to review your project only with the import rules, you can run:

sourcery review --enable gpsg-import .

Add the Google Python Style Guide to Your Sourcery Config

You can add the whole GPSG rule set or a subset of it to your project's .sourcery.yaml config file. This way, Sourcery will use these rules anytime it reviews your project in the CLI or in an IDE.

GPSG in VSCode

To configure the whole Google Python Style Guide:

rule_settings:
  enable:
    - default
    - gpsg

To configure only the import rules from the Google Python Style Guide:

rule_settings:
  enable:
    - default
    - gpsg-import

You can also copy any of the rules into your project's .sourcery.yaml. file. This way, you can also tweak the rules. For example, you can configure them to be applied only in specific directories.

Rule Tags

Import Rules

Rule tag: gpsg-import

The rules in this section keep imports intuitive and consistent.

They flag:

  • relative imports
  • wildcard imports

Further info:

Standard Aliases For Packages

Rule tag: gpsg-standard-import-alias

Use import y as z only when z is a standard abbreviation.

Standard aliases enforced by these rules:

  • pandas => pd
  • numpy => np
  • matplotlib.pyplot => plt
  • tensorflow => tf
  • datetime => dt
  • tkinter => tk
  • multiprocessing => mp

Further info:

Lambda Rules

Rule tag: gpsg-lambda

The rules in this section ensure that lambdas are kept simple and short, or suggest the use of other, more Pythonic constructs.

  • Lambdas should be max. 80 characters.
  • Prefer generator expressions over map and filter with a lambda.

Further info:

Type Annotations

Rule tag: gpsg-type-annotations

This group of rules requires type annotations for:

  • function arguments
  • return values

Further info:

Docstrings

Rule tag: gpsg-docstrings

This group of rules requires docstrings for:

  • modules
  • classes
  • public functions

Further info:

Naming Rules

Rule tag: gpsg-naming

These rules flag the following anti-patterns:

  • single-character names
  • type suffix for simple, built-in types
  • all the rules in gpsg-naming-pep8

Further info:

Naming Rules According To PEP-8

Rule tag: gpsg-naming-pep8

These rules enforce naming conventions that are endorsed not just by the Google Python Style Guide, but also by PEP-8:

Snake case for:

  • function names
  • function arguments
  • variable names

Camel case for class names.

Further info:

See Also