Skip to content

Demo repository using Sourcery to enforce the Google Python Style Guide

Notifications You must be signed in to change notification settings

sourcery-ai/google-python-style-guide-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sourcery's Google Python Style Guide Rules Demo

Demonstration repository using Sourcery to enforce the Google Python Style Guide (GPSG).

Overview

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.

Getting Started

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.

Navigating the Examples

The directory examples contains example files with code that either violates or not one or more of the GPSG rules.

Using an IDE

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:

Exception rule example Import rule example Lambda rule example

Using the CLI

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.

Individual Rules

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:

Real-World Examples

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:

Integrations

Pre-commit

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!

Licensing

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.

About

Demo repository using Sourcery to enforce the Google Python Style Guide

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published