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

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.32.0-py2.py3-none-win_amd64.whl (77.9 MB view details)

Uploaded Python 2Python 3Windows x86-64

sourcery-1.32.0-py2.py3-none-manylinux1_x86_64.whl (98.2 MB view details)

Uploaded Python 2Python 3

sourcery-1.32.0-py2.py3-none-macosx_10_9_universal2.whl (77.7 MB view details)

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

File details

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

File metadata

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

File hashes

Hashes for sourcery-1.32.0-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 9b3cce1522685bdd21a4086b97f67795faf68954bad6a88980a7c0239c9cb45d
MD5 48f17af87bad00fdaae5f36166c4b9ed
BLAKE2b-256 d51df6155df15731a5b333fc1ee39cb70a91a5c2dc43da5227c5ce1cf5a81530

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sourcery-1.32.0-py2.py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8787cfc9b16c23c05cf8998b28a9fe97c5e2e96e021debbef25b92b07e562a06
MD5 3ce8e3a157289a0ff9ed3fcf726d6621
BLAKE2b-256 78f78559847c4d46626983d079964c22b9c44d54b725075f570b36b4a71ba060

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sourcery-1.32.0-py2.py3-none-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 5cac2d767908f0806e09aea59d98d47b0241283d328c90f9d796c9f5888b082a
MD5 bed7aca06e0190a26ee19b69799be8a9
BLAKE2b-256 5635a6d57270aa656acb64d3063a485e09497beeb7a3943835206b7d678baa59

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 Sentry Error logging StatusPage Status page