Skip to main content

Safe, minimal import sorting

Project description

μ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.6 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>

License

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

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

usort-1.1.0b1.tar.gz (80.8 kB view details)

Uploaded Source

Built Distribution

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

usort-1.1.0b1-py3-none-any.whl (39.5 kB view details)

Uploaded Python 3

File details

Details for the file usort-1.1.0b1.tar.gz.

File metadata

  • Download URL: usort-1.1.0b1.tar.gz
  • Upload date:
  • Size: 80.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for usort-1.1.0b1.tar.gz
Algorithm Hash digest
SHA256 69dbb40f820615649ce7d8537ee6a4ca9a872df119b596ce3d2d407f6da22cd0
MD5 b4341b5d3b5d5c1d56d00366fe8ff485
BLAKE2b-256 02cf2cb4841fd66c1a18e81d733beeaec33ba737071642c8ed1cab022f18965c

See more details on using hashes here.

File details

Details for the file usort-1.1.0b1-py3-none-any.whl.

File metadata

  • Download URL: usort-1.1.0b1-py3-none-any.whl
  • Upload date:
  • Size: 39.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for usort-1.1.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 f91432b8d72a641a4bdafaa93c6d8af8feb565590b4681965b01d40793c4cbd3
MD5 c19a2c3c6c8ea2affe396524d19025b1
BLAKE2b-256 643fd43fbc920aed9ce2ea2e4787bcf4a9cdbebe66ab0c490037fbc6f52082f6

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