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

Uploaded Python 2Python 3Windows x86-64

sourcery-1.43.0-py2.py3-none-manylinux1_x86_64.whl (132.9 MB view details)

Uploaded Python 2Python 3

sourcery-1.43.0-py2.py3-none-macosx_11_0_arm64.whl (93.6 MB view details)

Uploaded Python 2Python 3macOS 11.0+ ARM64

sourcery-1.43.0-py2.py3-none-macosx_10_9_x86_64.whl (107.5 MB view details)

Uploaded Python 2Python 3macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for sourcery-1.43.0-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 2bd293af561261ffc0bccff82c9f764cd654247aaa29a0b46d4927e4711b0570
MD5 8c5fdcf9d1926557929f3db860ee4e01
BLAKE2b-256 68cf8ac262bd2d1c5527a9d8c437f5bdbabc5dc68690110dbf34b20be090798a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sourcery-1.43.0-py2.py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a142fb1156dfc205752b546fd24cd6e6bd405ebd7aec1097c339ed0e05724dcd
MD5 8e74e2002a678dc94ecfc94911affe32
BLAKE2b-256 86184075fe0f6b6a3692d80c9c490792d66f839ed91111afa7aea7869f6f1682

See more details on using hashes here.

File details

Details for the file sourcery-1.43.0-py2.py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sourcery-1.43.0-py2.py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 89034e4a800294093614c8c01317e0b051c35d2e7abf089e78f1830984516e04
MD5 749dce9d834ea199e3345e98a6e1b808
BLAKE2b-256 78f9058e1026479d2af655be608803d75d96297f4d0a2e98bb510488e384f415

See more details on using hashes here.

File details

Details for the file sourcery-1.43.0-py2.py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for sourcery-1.43.0-py2.py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6279f505406b9b3455dac2f9646c0e110fdc72857479dc05b788a2cfd28dfa4f
MD5 41f192e62858e5f34f5611ead2ecc467
BLAKE2b-256 c87a624a3539c5355de0ee9cec4cb9c6d2be6d36ab916c190b365c8462f0f0d5

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