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. The JSON schema lists every field it accepts.

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.45.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_nightly-1.45.0-py2.py3-none-win_amd64.whl (93.1 MB view details)

Uploaded Python 2Python 3Windows x86-64

sourcery_nightly-1.45.0-py2.py3-none-manylinux1_x86_64.whl (135.2 MB view details)

Uploaded Python 2Python 3

sourcery_nightly-1.45.0-py2.py3-none-macosx_11_0_arm64.whl (95.1 MB view details)

Uploaded Python 2Python 3macOS 11.0+ ARM64

sourcery_nightly-1.45.0-py2.py3-none-macosx_10_9_x86_64.whl (109.5 MB view details)

Uploaded Python 2Python 3macOS 10.9+ x86-64

File details

Details for the file sourcery_nightly-1.45.0-py2.py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for sourcery_nightly-1.45.0-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 58347bcaa4833eebf16ff9e0a44e81d2d851497ab57bda98a3923e68f75ea183
MD5 67723cfd773418155eaab96867d1a04a
BLAKE2b-256 bead2c1f0cad8b891dee5bfb7952e993e98d3ac578d5777fa769f2c9cab5ca5e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourcery_nightly-1.45.0-py2.py3-none-win_amd64.whl:

Publisher: build_sourcery.yml on sourcery-ai/core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sourcery_nightly-1.45.0-py2.py3-none-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for sourcery_nightly-1.45.0-py2.py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 57e33135e70898125ee9803bf66b87f0972e94126a35b0c908b25ca835c2335a
MD5 69a292f2b58640b82b438be06937be98
BLAKE2b-256 ee0ec5fba57f168a089589fe9c8b2c44422e82cd32c503e9d5fd160ddb17a80a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourcery_nightly-1.45.0-py2.py3-none-manylinux1_x86_64.whl:

Publisher: build_sourcery.yml on sourcery-ai/core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sourcery_nightly-1.45.0-py2.py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sourcery_nightly-1.45.0-py2.py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 58a77dece18b287b89ce88bcf6f629c438dba4f1b50f85292e01516bc289a46a
MD5 342f893b8ba3814a2baa649f1f8881f6
BLAKE2b-256 615a8e2cc803774ba69ce638b00893651f591f52aaa2834ad62102cd71d6a892

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourcery_nightly-1.45.0-py2.py3-none-macosx_11_0_arm64.whl:

Publisher: build_sourcery.yml on sourcery-ai/core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sourcery_nightly-1.45.0-py2.py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for sourcery_nightly-1.45.0-py2.py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 99ff6f34fb3c51ed9dac5c884ecf383d14a7ccf639f15aa95c1ddc28130b10b9
MD5 8d4648a5e0fde3f8671b231a8af3f3be
BLAKE2b-256 6e09a494d64fef85fad5f3c60a6ff911a65b854e08cc7d0cb21d16c1eb6e4823

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourcery_nightly-1.45.0-py2.py3-none-macosx_10_9_x86_64.whl:

Publisher: build_sourcery.yml on sourcery-ai/core

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.45.0 This release

4 files

1.44.0

4 files

1.40.0

3 files

1.39.0

3 files

1.38.0

3 files

1.37.0

3 files

1.36.0

3 files

1.35.0

3 files

1.34.0

3 files

1.33.0

3 files

1.32.0

3 files

1.31.0

3 files

1.30.0

3 files

1.29.0

3 files

1.28.0

3 files

1.27.0

3 files

1.26.0

3 files

1.25.0

3 files

1.24.0

3 files

1.23.0

3 files

1.22.0

3 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page