Giter Site home page Giter Site logo

ufmt's Introduction

µ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.7 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

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"

See the user guide for details.

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

See the user guide for details.

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

µfmt 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.

ufmt's People

Contributors

akx avatar amyreese avatar dependabot[bot] avatar mkniewallner avatar pmeier avatar thatch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.