Skip to main content

Sourcery Logo

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 --in-place option:

sourcery review --in-place {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:
  -c, --code TEXT     Refactor the code passed in as a string.
  --diff TEXT         Run only on changed code with diff command (e.g. "git diff")
  --enable TEXT       Only run the specified rule/tag. This option can be used
                      multiple times.
  --disable TEXT      Do not run the specified rule/tag. This option can be used
                      multiple times.
  --check             Return exit code 1 if unsolved issues found.
  --in-place          Make changes to files in place
  --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.
  -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.0.0
    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 --in-place option:

args: [--diff=git diff HEAD, --in-place]

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 }}"

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sourcery-1.0.0-py2.py3-none-win_amd64.whl (62.4 MB view details)

Uploaded Python 2Python 3Windows x86-64

sourcery-1.0.0-py2.py3-none-manylinux1_x86_64.whl (99.7 MB view details)

Uploaded Python 2Python 3

sourcery-1.0.0-py2.py3-none-macosx_10_9_universal2.whl (70.2 MB view details)

Uploaded Python 2Python 3macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file sourcery-1.0.0-py2.py3-none-win_amd64.whl.

File metadata

  • Download URL: sourcery-1.0.0-py2.py3-none-win_amd64.whl
  • Upload date:
  • Size: 62.4 MB
  • Tags: Python 2, Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for sourcery-1.0.0-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 0ecc392b54b0934ceef1dfabe226df9aa9fb64b7a00badc65e0835c30fe4ea3d
MD5 9903075730aa28bcc605db9de8823e60
BLAKE2b-256 08102cc565553d0998f42a23d12ed9ddf762450432ba3d3a44046ede13a0fb42

See more details on using hashes here.

File details

Details for the file sourcery-1.0.0-py2.py3-none-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for sourcery-1.0.0-py2.py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a8626dd09275a5faa342e7ca73a898472e590b5761d49c83d5fc006b521c271f
MD5 3b60103ff9a3067e2fd7ced3b3df1489
BLAKE2b-256 2924fef6d7383df00ca60028a653320560d2ad07930580059e918296ccb596ae

See more details on using hashes here.

File details

Details for the file sourcery-1.0.0-py2.py3-none-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for sourcery-1.0.0-py2.py3-none-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 c7dea4a1820270da30fb9da4d83c66414aca3bab4c04fe367e4bd8e25591e56f
MD5 c4bf0f6571d6236a5ce0b70add0553a0
BLAKE2b-256 cf8b0a1fffb3532d9813b1acf0ec1ceba79697f8ed7a5d4d49c960680e572355

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page