Skip to main content

Magically refactor Python

Project description

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.25.1b1
    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


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

sourcery_nightly-1.25.1b1-py2.py3-none-win_amd64.whl (101.4 MB view details)

Uploaded Python 2 Python 3 Windows x86-64

sourcery_nightly-1.25.1b1-py2.py3-none-manylinux1_x86_64.whl (133.0 MB view details)

Uploaded Python 2 Python 3

sourcery_nightly-1.25.1b1-py2.py3-none-macosx_10_9_universal2.whl (103.7 MB view details)

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

File details

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

File metadata

File hashes

Hashes for sourcery_nightly-1.25.1b1-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 ae5a6361a7ca355f36168541cb98beb498ec496d80deb6dbcc8e9cd749962723
MD5 82cc5000f22c42c0713a3edd5e339e0c
BLAKE2b-256 5bfb81c17729ae426adc6655ba2fd56836d420bea6f362d66920218422877da3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sourcery_nightly-1.25.1b1-py2.py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 773de7e8296d419a1c373ea7e85b7ef0e76bcdeafa1a888f8f3b3e21790115d0
MD5 27eec1166c50c9def30b753de3d999ce
BLAKE2b-256 c6678eac734bce08ed70b4c9b3e0caff33bfb0d70ea002da933086db64d21e35

See more details on using hashes here.

File details

Details for the file sourcery_nightly-1.25.1b1-py2.py3-none-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for sourcery_nightly-1.25.1b1-py2.py3-none-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 f3d5a63c8785b5b74cbcb7a68c9a93a9dcb249783dcbdd2d0710fa52d8a69fb7
MD5 d791f2f3d59d8cef93eb8e6af895859e
BLAKE2b-256 a6357c250495adedba3427ba3c03fee7f8c485547c1996340eeab9603dbd3b8b

See more details on using hashes here.

Supported by

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