Magically refactor Python
Project description
Sourcery Command Line Interface
Installation and usage
Installation
Sourcery command line interface can be installed by running:
pip install sourcery
Login
Once installed you need to log in with:
sourcery login
which will open up a browser tab and ask for confirmation. This is only needed once per computer.
You can also log in via providing your token:
sourcery login --token YOUR_TOKEN
Usage
To review a file or a directory (recursively) with Sourcery, use the
sourcery review
command:
sourcery review example_file.py
To apply the changes suggested by Sourcery, use the --fix
option:
sourcery review --fix {file_or_directory}
Command Line Options
Sourcery provides a few options for running. You can list them by running
sourcery review --help
sourcery review --help
Usage: sourcery review [OPTIONS] [SRC]...
Review SRC files/directories. Reads from stdin when SRC is -
Options:
--diff TEXT Run only on changed code with diff command (e.g.
"git diff")
--enable TEXT Only run the specified rule or tag. This option
can be used multiple times.
--disable TEXT Skip the specified rule or tag. This option can
be used multiple times.
--check Return exit code 1 if unsolved issues found.
--fix Automatically fix issues where possible.
--config FILE Location of the Sourcery YAML config file. Can be
a file or URL.
--csv Output in CSV format
--verbose Verbose output with explanation and code
snippets.
--summary / --no-summary Flag to determine whether to print a summary of
the review. Default: true.
-h, --help Show this message and exit.
Configuration
Sourcery reads configuration settings from .sourcery.yaml
in the project
directory. Full details are described
here.
Pre-commit Hook
Sourcery works great with pre-commit. Once you
have it installed, add this to the
.pre-commit-config.yaml
in your repository:
repos:
- repo: https://github.com/sourcery-ai/sourcery
rev: v1.23.1b30
hooks:
- id: sourcery
# The best way to use Sourcery in a pre-commit hook:
# * review only changed lines:
# * omit the summary
args: [--diff=git diff HEAD, --no-summary]
To review all changes compared to the main
branch:
args: [--diff=git diff main]
If you want Sourcery to automatically apply the suggested changes,add the
--fix
option:
args: [--diff=git diff HEAD, --fix]
If Sourcery is the first pre-commit hook that you've added to your project,
you'll also need to run pre-commit install
.
Continuous Integration
For the majority of projects, the best usage of Sourcery in the CI is to review only the code of the current PR.
You can use the script below to detect Sourcery violations that are present in
the current code but not in the main
branch:
pip install sourcery
# Store your Sourcery token as a secret in your CI environment.
sourcery login --token $SOURCERY_TOKEN
sourcery review --diff "git diff main" .
Instead of main
, you can pick another branch as well. For example, in GitHub
Actions you can use
--diff="git diff ${{ github.event.pull_request.base.sha }}"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for sourcery_nightly-1.23.1b30-py2.py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0180306948756e995d08963509dfa09989503ca0bd7e7548560366504f94dc0d |
|
MD5 | 55110ba367b70fd126f029e6a4fb0dad |
|
BLAKE2b-256 | 4d61b698b41b6090f008530ef45e10991ad3ceb415af04e67daa23602cf78f85 |
Hashes for sourcery_nightly-1.23.1b30-py2.py3-none-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6c798c4fe7830b9af03baff0d1f0d25698e71fdbf72dc67dc7e095314b2936a |
|
MD5 | aa75d73c426965adf18dc9ddd77ae0a7 |
|
BLAKE2b-256 | df0af8158cde1295390c3aef5b135ff57985c76ec89666985249f3115a4ec35d |
Hashes for sourcery_nightly-1.23.1b30-py2.py3-none-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bec63cfa0a8c95a1e58fc228a63db4b4f784cc0847e5de6b2707acfac0c3b103 |
|
MD5 | 1aff7ba6f50a0bcbe8290ef50a74f6fe |
|
BLAKE2b-256 | 0fbec5ca4b07d0dc51b53c11bf8e5dab08e9b06a6e1647c2bf95e615a5c800e3 |