Coder Social home page Coder Social logo

usort's Introduction

μsort

Safe, minimal import sorting for Python projects.

documentation version changelog license

μsort is a safe, minimal import sorter. Its primary goal is to make no "dangerous" changes to code. This is achieved by detecting distinct "blocks" of imports that are the most likely to be safely interchangeable, and only reordering imports within these blocks without altering formatting. Code style is left as an exercise for linters and formatters.

Within a block, µsort will follow common Python conventions for grouping imports based on source (standard library, third-party, first-party, or relative), and then sorting lexicographically within each group. This will commonly look like:

import re
from pathlib import Path
from typing import Iterable
from unittest.mock import call, Mock, patch

import aiohttp
from aiosqlite import connect

import foo
from bar import bar

from .main import main

Blocks are inferred from a number of real world conditions, including any intermediate statements between imports:

import warnings
warnings.filterwarnings(...)

import re
import sys

In this case, µsort detects two blocks–separated by the call to filterwarnings(), and will only sort imports inside of each block. Running µsort on this code will generate no changes, because each block is already sorted.

Imports can be excluded from blocks using the #usort:skip directive, or with #isort:skip for compatibility with existing codebases. µsort will leave these imports unchanged, and treat them as block separators.

See the User Guide for more details about how blocks are detected, and how sorting is performed.

Install

µsort requires Python 3.8 or newer to run. Install µsort with:

$ pip install usort

Usage

To format one or more files or directories in-place:

$ usort format <path> [<path> ...]

To generate a diff of changes without modifying files:

$ usort diff <path>

To just validate that files are formatted correctly, like during CI:

$ usort check <path>

pre-commit

µsort provides a pre-commit hook. To enforce sorted imports before every commit, add the following to your .pre-commit-config.yaml file:

- repo: https://github.com/facebook/usort
  rev: v1.0.7
  hooks:
    - id: usort

License

μsort is MIT licensed, as found in the LICENSE file.

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.