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
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.
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.
The rules in this section keep imports intuitive and consistent.
- relative imports
- wildcard imports
Standard Aliases For Packages¶
import y as zonly when
zis a standard abbreviation.
Standard aliases enforced by these rules:
- pandas => pd
- numpy => np
- matplotlib.pyplot => plt
- tensorflow => tf
- datetime => dt
- tkinter => tk
- multiprocessing => mp
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
filterwith a lambda.
This group of rules requires type annotations for:
- function arguments
- return values
This group of rules requires docstrings for:
- public functions
These rules flag the following anti-patterns:
- single-character names
- type suffix for simple, built-in types
- all the rules in
Naming Rules According To PEP-8¶
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.
- sourcery-rules repo
- Google Python Style Guide 3.16.4 Guidelines derived from Guido’s Recommendations