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.40.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.44.0-py2.py3-none-win_amd64.whl (90.8 MB view details)

Uploaded Python 2Python 3Windows x86-64

sourcery_nightly-1.44.0-py2.py3-none-manylinux1_x86_64.whl (132.1 MB view details)

Uploaded Python 2Python 3

sourcery_nightly-1.44.0-py2.py3-none-macosx_11_0_arm64.whl (92.6 MB view details)

Uploaded Python 2Python 3macOS 11.0+ ARM64

sourcery_nightly-1.44.0-py2.py3-none-macosx_10_9_x86_64.whl (106.7 MB view details)

Uploaded Python 2Python 3macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for sourcery_nightly-1.44.0-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 3f2ca5424a49f5e7a22469d0b667e358c64cb636c12fab1896795afb3d328298
MD5 cabf41a9a111e27629644e99f82b92b5
BLAKE2b-256 30fde59fea3efeebc0fc279e39543a00a656511c1f8c4198bb5550798cc41d41

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourcery_nightly-1.44.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.44.0-py2.py3-none-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for sourcery_nightly-1.44.0-py2.py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cc9f11b12b862eb43f1faa663fb72cbff875cb175a98801eb2f0ad97d587e0bf
MD5 3f3b1868c64e95bb2723152f3a1bed12
BLAKE2b-256 628cdd701deaebee590284a87ad5e76b8285c8db34cb0535b24382e99707d926

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourcery_nightly-1.44.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.44.0-py2.py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sourcery_nightly-1.44.0-py2.py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 947f7152e956fb551ad07ee02cec4ea73b84df50b76fa2a7cddc0f127559b75a
MD5 08a4c05a0aa8dcf0507cc0cc2ca7e3a5
BLAKE2b-256 4e76cb51f0a03ef5160a70731028dca7f10112adb0ccea8f9bbc1eddb7462d16

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourcery_nightly-1.44.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.44.0-py2.py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for sourcery_nightly-1.44.0-py2.py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2a320243b4c5f58f05173113490f58459f7b2cb1645b952f101024049560e704
MD5 3e68c65c5b2b0c256efa9af57044b27e
BLAKE2b-256 a249e4df0cea2e6e429ed064e05cf63a7894312d598545e5a8c8a418e031370d

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourcery_nightly-1.44.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.

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