Skip to main content

Apply Black formatting only in regions changed since last commit

Project description

travis-badge license-badge pypi-badge downloads-badge black-badge changelog-badge

What?

This utility reformats and checks Python source code files in a Git repository. However, it only applies reformatting and reports errors in regions which have changed in the Git working tree since the last commit.

The reformatters and linters supported are:

  • Black for code reformatting

  • isort for sorting imports

  • Mypy for static type checking

  • Pylint for generic static checking of code

  • Flake8 for style guide enforcement

New in version 1.1.0: Support for Mypy, Pylint and other linters.

Why?

You want to start unifying code style in your project using black. Maybe you also like to standardize on how to order your imports, or do static type checking or other static analysis for your code.

However, instead of formatting the whole code base in one giant commit, you’d like to only change formatting when you’re touching the code for other reasons.

This can also be useful when contributing to upstream codebases that are not under your complete control.

However, partial formatting is not supported by black itself, for various good reasons, and it won’t be implemented either (134, 142, 245, 370, 511, 830).

This is where darker enters the stage. This tool is for those who want to do partial formatting anyway.

Note that this tool is meant for special situations when dealing with existing code bases. You should just use Black and isort as is when starting a project from scratch.

How?

To install, use:

pip install darker

The darker <myfile.py> command reads the original file, formats it using black, combines original and formatted regions based on edits, and writes back over the original file.

Alternatively, you can invoke the module directly through the python executable, which may be preferable depending on your setup. Use python -m darker instead of darker in that case.

By default, darker just runs Black to reformat the code. You can enable additional features with command line options:

  • -i / --isort: Reorder imports using isort

  • -L <linter> / --lint <linter>: Run a supported linter:

    • -L mypy: do static type checking using Mypy

    • -L pylint: analyze code using Pylint

    • -L flake8: enforce the Python style guide using Flake8

New in version 1.1.0: The -L / --lint option.

Example:

$ mkdir test && cd test && git init
Initialized empty Git repository in /tmp/test/.git/
$ echo "if True: print('hi')\n\nif False: print('there')" | tee test.py
if True: print('hi')

if False: print('there')
$ git add test.py && git commit -m "Initial commit"
[master (root-commit) a0c7c32] Initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 test.py
$ echo "if True: print('changed')\n\nif False: print('there')" | tee test.py
if True: print('changed')

if False: print('there')
$ darker test.py && cat test.py
if True:
    print("changed")

if False: print('there')

Customizing Black and isort behavior

Project-specific default options for Black and isort are read from the project’s pyproject.toml file in the repository root. isort also looks for a few other places for configuration.

For more details, see:

The following command line arguments can also be used to modify the defaults:

-r REVISION, --revision REVISION
                      Git revision against which to compare the working
                      tree. Tags, branch names, commit hashes, and other
                      expressions like HEAD~5 work here.

--diff                Don't write the files back, just output a diff for
                      each file on stdout. Highlight syntax on screen if
                      the `pygments` package is available.

--check               Don't write the files back, just return the status.
                      Return code 0 means nothing would change. Return code
                      1 means some files would be reformatted.

-i, --isort           Also sort imports using the `isort` package

-L CMD, --lint CMD    Also run a linter on changed files. CMD can be a name
                      of path of the linter binary, or a full quoted command
                      line
-c PATH, --config PATH
                      Ask `black` and `isort` to read configuration from PATH.
-S, --skip-string-normalization
                      Don't normalize string quotes or prefixes
--no-skip-string-normalization
                      Normalize string quotes or prefixes. This can be used
                      to override `skip_string_normalization = true` from a
                      configuration file.
-l LINE_LENGTH, --line-length LINE_LENGTH
                      How many characters per line to allow [default: 88]

New in version 1.0.0: The -c, -S and -l command line options.

New in version 1.0.0: isort is configured with -c and -l, too.

New in version 1.1.0: The -r / --revision command line option.

New in version 1.1.0: The --diff command line option.

New in version 1.1.0: The --check command line option.

New in version 1.1.0: The --no-skip-string-normalization command line option.

New in version 1.1.0: The -L / --lint command line option.

Editor integration

Many editors have plugins or recipes for integrating black. You may be able to adapt them to be used with darker. See editor integration in the black documentation.

PyCharm/IntelliJ IDEA

  1. Install darker:

    $ pip install darker
  2. Locate your darker installation folder.

    On macOS / Linux / BSD:

    $ which darker
    /usr/local/bin/darker  # possible location

    On Windows:

    $ where darker
    %LocalAppData%\Programs\Python\Python36-32\Scripts\darker.exe  # possible location
  3. Open External tools in PyCharm/IntelliJ IDEA

    On macOS:

    PyCharm -> Preferences -> Tools -> External Tools

    On Windows / Linux / BSD:

    File -> Settings -> Tools -> External Tools

  4. Click the + icon to add a new external tool with the following values:

    • Name: Darker

    • Description: Use Black to auto-format regions changed since the last git commit.

    • Program: <install_location_from_step_2>

    • Arguments: "$FilePath$"

    If you need any extra command line arguments like the ones which change Black behavior, you can add them to the Arguments field, e.g.:

    --config /home/myself/black.cfg "$FilePath$"
  5. Format the currently opened file by selecting Tools -> External Tools -> Darker.

    • Alternatively, you can set a keyboard shortcut by navigating to Preferences or Settings -> Keymap -> External Tools -> External Tools - Darker

  6. Optionally, run darker on every file save:

    1. Make sure you have the File Watcher plugin installed.

    2. Go to Preferences or Settings -> Tools -> File Watchers and click + to add a new watcher:

      • Name: Darker

      • File type: Python

      • Scope: Project Files

      • Program: <install_location_from_step_2>

      • Arguments: $FilePath$

      • Output paths to refresh: $FilePath$

      • Working directory: $ProjectFileDir$

    3. Uncheck “Auto-save edited files to trigger the watcher”

Visual Studio Code

  1. Install darker:

    $ pip install darker
  2. Locate your darker installation folder.

    On macOS / Linux / BSD:

    $ which darker
    /usr/local/bin/darker  # possible location

    On Windows:

    $ where darker
    %LocalAppData%\Programs\Python\Python36-32\Scripts\darker.exe  # possible location
  3. Add these configuration options to VS code, Cmd-Shift-P, Open Settings (JSON):

    "python.formatting.provider": "black",
    "python.formatting.blackPath": "<install_location_from_step_2>",
    "python.formatting.blackArgs": ["--diff"],

You can pass additional arguments to darker in the blackArgs option (e.g. ["--diff", "--isort"]), but make sure at least --diff is included.

How does it work?

Darker takes a git diff of your Python files, records which lines of current files have been edited or added since the last commit. It then runs black and notes which chunks of lines were reformatted. Finally, only those reformatted chunks on which edited lines fall (even partially) are applied to the edited file.

Also, in case the --isort option was specified, isort is run on each edited file before applying black. Similarly, each linter requested using the –lint <command> option is run, and only linting errors/warnings on modified lines are displayed.

License

BSD. See LICENSE.rst.

Prior art

GitHub stars trend

stargazers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

darker-1.1.0.tar.gz (31.9 kB view hashes)

Uploaded Source

Built Distribution

darker-1.1.0-py3-none-any.whl (36.4 kB view hashes)

Uploaded Python 3

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