Skip to main content

Safe, atomic formatting with black and µsort

Project description

µfmt

Safe, atomic formatting with black and µsort

version documentation changelog license build status code coverage

µ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 chance of 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


Download files

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

Source Distribution

ufmt-1.3.2.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

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

ufmt-1.3.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file ufmt-1.3.2.tar.gz.

File metadata

  • Download URL: ufmt-1.3.2.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for ufmt-1.3.2.tar.gz
Algorithm Hash digest
SHA256 00fa0f747123ef945e640ec3cfc036768cc35d1b59565e8a225c3fd30a4027bc
MD5 5c6b458e0b2062a6cdd9685081e2274a
BLAKE2b-256 4d11d80245e6ec761f82efb28b58410cd312469fa6bc9d6f1386e309e3975e91

See more details on using hashes here.

File details

Details for the file ufmt-1.3.2-py3-none-any.whl.

File metadata

  • Download URL: ufmt-1.3.2-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for ufmt-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0915d10fec31e025e5b1af66e85a9acc19f854f22ae829d31c9dc2179fc442ef
MD5 6d3d70bfb2f857745180535b0c9b7d29
BLAKE2b-256 7fe00a450e08eaf5879f6934980d3b85640a468931d092f28bcb6addffb3aa0c

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