Safe, atomic formatting with black and µsort
Project description
µfmt
Safe, atomic formatting with black and µsort
µfmt is a safe, atomic code formatter for Python built on top of black and µsort:
Black makes code review faster by producing the smallest diffs possible. Blackened code looks the same regardless of the project you’re reading.
μsort is a safe, minimal import sorter. Its primary goal is to make no “dangerous” changes to code, and to make no changes on code style.
µfmt formats files in-memory, first with µsort and then with black, before writing any changes back to disk. This enables a combined, atomic step in CI/CD workflows for checking or formatting files, without any with conflict or intermediate changes between the import sorter and the code formatter.
Install
µfmt requires Python 3.6 or newer. You can install it from PyPI:
$ pip install ufmt
If you want to prevent unexpected formatting changes that can break your CI workflow, make sure to pin your transitive dependencies–including black, µsort, and µfmt–to your preferred versions.
If you use requirements.txt
, this might look like:
black==21.12b0
ufmt==1.3.1
usort==0.6.4
Usage
To format one or more files or directories in place:
$ ufmt format <path> [<path> ...]
To validate files are formatted correctly, like for CI workflows:
$ ufmt check <path> [<path> ...]
To validate formatting and generate a diff of necessary changes:
$ ufmt diff <path> [<path> ...]
pre-commit hook
µfmt provides a pre-commit hook. To format your diff before
every commit, add the following to your .pre-commit-config.yaml
file:
- repo: https://github.com/omnilib/ufmt
rev: v1.3.1
hooks:
- id: ufmt
You can change the rev
to any version >= 1.3.0
. To pin black
and usort
, use the
additional_dependencies
option:
hooks:
- id: ufmt
additional_dependencies:
- black == 21.12b0
- usort == 0.6.4
License
µfmt is copyright John Reese, and licensed under
the MIT license. I am providing code in this repository to you under an open
source license. This is my personal repository; the license you receive to
my code is from me and not from my employer. See the LICENSE
file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ufmt-1.3.1.tar.gz
.
File metadata
- Download URL: ufmt-1.3.1.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.26.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 199d6bb51de50325484ea4e4d9f415553d16ccc7c00a500ba61469559f06e3db |
|
MD5 | f496fda6a8f4d8684cf8c2979ac18707 |
|
BLAKE2b-256 | 2ac2a10e397b77050ae3d43a6be8ea5ca8f59212a217ace9d2652dd4a3d3dc86 |
File details
Details for the file ufmt-1.3.1-py3-none-any.whl
.
File metadata
- Download URL: ufmt-1.3.1-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.26.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aaf89903759a52f8b4f0f437b58151a638ba320e07d0001d0139e25478c57dc6 |
|
MD5 | be7db15065ba56d8a03fc1454952e9c8 |
|
BLAKE2b-256 | 6176c3ad1677caaa647cf4d66c748faf7be66566aba7b8106d95eb2e91f04119 |