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.37.1.dev0
    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

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

sourcery_nightly-1.40.0-py2.py3-none-win_amd64.whl (101.3 MB view details)

Uploaded Python 2Python 3Windows x86-64

sourcery_nightly-1.40.0-py2.py3-none-manylinux1_x86_64.whl (124.2 MB view details)

Uploaded Python 2Python 3

sourcery_nightly-1.40.0-py2.py3-none-macosx_10_9_universal2.whl (101.7 MB view details)

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

File details

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

File metadata

File hashes

Hashes for sourcery_nightly-1.40.0-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 692f8c560f42e45849999184ff35c9d12d053dfa7d382a4f770c81af257d4344
MD5 caac448d6ed343d186c3e4eda5b06cc1
BLAKE2b-256 e62c40a1461c66244f3160a236823296fe24d9a756f7a0f4bc36af68914ba99c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sourcery_nightly-1.40.0-py2.py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ff4ca931e398591eacc50ffe0539c6f4c04ed0f7be5a5d1e03a17f42ea7160b4
MD5 7b07f8249b17a74c96f0d3d2f9e71ec8
BLAKE2b-256 78d363b4f803f368b2dfaed02b7619bbe142018c777c82c5da9f18b478898951

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sourcery_nightly-1.40.0-py2.py3-none-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 c5972d39d57ee56411906c79ce09e89d95413f0de73acc670628569390c22bd6
MD5 f154e8189d4f69c0dd86f82c996a7d63
BLAKE2b-256 e5ded9ac32becbe3153273c8d89971888268e06ddd4df61d69b63e181957b8f8

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