Demonstration repository using Sourcery to enforce the Google Python Style Guide (GPSG).
This repository was created by us at Sourcery to demonstrate our implementation of the Google Python Style Guide (GPSG) as Sourcery rules that you can incorporate into your project.
By bringing in one of the best Python style guides available out there and automatically enforcing it with Sourcery, you help your entire team to follow the best programming practices, increasing your code quality, speeding up development and reducing the chances of introducing bugs.
Here, we provide examples of Python code that intentionally violate the style guide as well as examples of code that follow it, together with a set of custom rules that allows Sourcery to identify and flag them.
The first step to try this demo out is to clone and open it:
git clone https://github.com/sourcery-ai/google-python-style-guide-demo.git
cd google-python-style-guide-demo
To check out this project in an IDE, first make sure that you have the Sourcery extension installed in your favorite IDE. If you prefer to test out the Sourcery CLI instead, make sure you have it installed in your current environment. Do not refrain from testing both the IDE integrations and the CLI!
If you already have everything set up, go ahead and check out the examples in the directory examples
.
After you have checked out what the Sourcery implementation of the Google Python Style Guide can do, take a look at the Sourcery configuration file to understand how it does it.
The directory examples
contains example files with code that either violates or not one or more of the GPSG rules.
Try opening those files in your IDE with the Sourcery extension enabled to see inline diagnostics of the violations those files contain.
The following images illustrate some of the diagnostics you may find:
Trying out Sourcery CLI in the examples is as simple as running the following command:
sourcery review examples
Sourcery will then show all of the violations to the GPSG it finds together with a summary at the end. The following block is a truncated example of the output you may see:
$ sourcery review examples
examples/individual_rules/conditional_expression_rules.py:19 no-complex-if-expressions
examples/individual_rules/conditional_expression_rules.py:24 no-complex-if-expressions
examples/individual_rules/decorator_rules.py:15-21 do-not-use-staticmethod
examples/individual_rules/default_iterator_and_operator_rules.py:17 do-not-use-has-key
examples/individual_rules/default_iterator_and_operator_rules.py:28 do-not-use-has-key
examples/individual_rules/default_iterator_and_operator_rules.py:40-43 do-not-use-has-key
... # this example is truncated!!
examples/individual_rules/type_annotations_rules.py:11-13 require-parameter-annotation
examples/individual_rules/type_annotations_rules.py:18-20 require-parameter-annotation
examples/individual_rules/type_annotations_rules.py:28-30 require-parameter-annotation
examples/individual_rules/type_annotations_rules.py:52-54 require-return-annotation
examples/individual_rules/type_annotations_rules.py:59-61 require-return-annotation
─────────────────────────────────────── Overview ───────────────────────────────────────
• 12 files scanned.
• 70 issues detected.
Issues by Type
• 7 issues could be fixed by Sourcery. Run sourcery review --in-place
• 63 issues need to be fixed manually.
Issues by Rule ID
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━┓
┃ Rule ID ┃ Count ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━┩
│ snake-case-arguments │ 8 │
│ snake-case-variable-declarations │ 8 │
│ upper-camel-case-classes │ 5 │
│ errors-named-error │ 4 │
│ no-complex-if-expressions │ 4 │
│ no-relative-imports │ 4 │
│ avoid-single-character-names-functions │ 3 │
│ do-not-use-has-key │ 3 │
│ docstrings-for-classes │ 3 │
│ no-wildcard-imports │ 3 │
│ require-parameter-annotation │ 3 │
│ avoid-global-variables │ 2 │
│ avoid-single-character-names-variables │ 2 │
│ docstrings-for-functions │ 2 │
│ filter-lambda-to-generator │ 2 │
│ lambdas-should-be-short │ 2 │
│ map-lambda-to-generator │ 2 │
│ require-return-annotation │ 2 │
│ avoid-trivial-properties │ 1 │
│ do-not-use-staticmethod │ 1 │
│ docstrings-for-modules │ 1 │
│ name-type-suffix │ 1 │
│ no-long-functions │ 1 │
│ use-standard-name-for-aliases-numpy │ 1 │
│ use-standard-name-for-aliases-pandas │ 1 │
│ use-standard-name-for-aliases-tensorflow │ 1 │
├──────────────────────────────────────────┼───────┤
│ Total │ 70 │
└──────────────────────────────────────────┴───────┘
📝 We may add more cool examples in the future, so the actual issue count that you see may be even higher than in this example.
The directory examples/individual_rules
contains individual examples for each Google Python Style Guide rule defined in the root Sourcery configuration file .sourcery.yaml
. You can use those examples to see each rule in action separately and decide how well they fit into your team's requirements.
The examples are grouped in files according to the Google Python Style Guide sections they correspond to:
- 2.2 Import Rules:
import_rules.py
- 2.4 Exception Rules:
exception_rules.py
- 2.5 Global Variable Rules:
global_variable_rules.py
- 2.8 Default Iterator and Operator Rules:
default_iterator_and_operator_rules.py
- 2.10 Lambda Function Rules:
lambda_function_rules.py
- 2.11 Conditional Expression Rules:
conditional_expression_rules.py
- 2.17 Decorator Rules:
decorator_rules.py
- 2.21 Type Annotation Rules:
type_annotations_rules.py
- 3.8 Docstring and Comment Rules:
docstring_and_comment_rules.py
- 3.15 Getter and Setter Rules:
getter_and_setter_rules.py
- 3.16 Naming Rules:
naming_rules.py
- 3.18 Function Length Rules:
function_length_rules.py
The directory examples/real_world
contains real-world examples extracted from real projects out there - actually, from projects written by us at Sourcery.
This way, you can see how the Google Python Style Guide helps us to find issues in actual code that people have actually written.
Currently, that directory contains code extracted from:
sourcery-ai/sourcery-analytics
: (yes, we at Sourcery violate the GPSG once in a while)
The file .pre-commit-config.yaml
configures pre-commit to run the Sourcery pre-commit hook.
Make sure you have pre-commit installed together with its git hook scripts. The first time the Sourcery pre-commit hook is executed, you will be prompted to log in to Sourcery if you are not already logged in. Please follow the login instructions from our docs to get Sourcery set up and ready to work with pre-commit!
This repository contains an implementation of the Google Python Style Guide as Sourcery rules. The style guide itself was created by Google and published under the CC BY 3.0 license.