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 vscode extension

µ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.8 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==22.6.0
ufmt==2.0.0
usort==1.0.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> ...]

Integrations

See the user guide for details on each integration.

GitHub Actions

µfmt provides a GitHub Action that can be added to an existing workflow, or as a separate workflow or job, to enforce proper formatting in pull requests:

jobs:
  ufmt:
    runs-on: ubuntu-latest
    steps:
      - uses: omnilib/ufmt@action-v1
        with:
          path: <PATH TO CHECK>
          requirements: requirements-fmt.txt
          python-version: "3.x"

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: v2.0.0
    hooks:
      - id: ufmt
        additional_dependencies: 
          - black == 22.6.0
          - usort == 1.0.4

Visual Studio Code

µfmt has an official VS Code extension to use µfmt as a formatter for Python files. Once installed, µfmt can be set as the default formatter with the following settings:

"[python]": {
  "editor.defaultFormatter": "omnilib.ufmt"
}

µfmt can automatically format when saving with the following settings:

"[python]": {
  "editor.defaultFormatter": "omnilib.ufmt",
  "editor.formatOnSave": true
}

For more details, or to install the extension, see the Visual Studio Marketplace page:

VS Code extension marketplace Install VS Code extension now

License

ufmt is copyright Amethyst 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-2.7.3.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

ufmt-2.7.3-py3-none-any.whl (28.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ufmt-2.7.3.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for ufmt-2.7.3.tar.gz
Algorithm Hash digest
SHA256 b661c575bfb95a617d04fa4fe5cde550d21eb848094b3f9c438165459bc04f77
MD5 f761ca0201323c1cd667b3a215b3afd4
BLAKE2b-256 ef248393cb5e96e640a858ac11c2ac65b28221b09c4a11f83210d2ab9d6108e5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ufmt-2.7.3-py3-none-any.whl
  • Upload date:
  • Size: 28.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for ufmt-2.7.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1a3d2515e3108ba7a68425ebdeb4cbb1fdfbcffbd031dac8f56a1f386a7b396e
MD5 0532ab2fdbfedc473f4fc55b9bb9c54e
BLAKE2b-256 d035df52ce427d847396a3574887fef32f11f169834523493010b32b5600867c

See more details on using hashes here.

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