Skip to main content

A small, safe import sorter

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.0.2.tar.gz (78.1 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.0.2-py3-none-any.whl (23.3 kB view details)

Uploaded Python 3

File details

Details for the file usort-1.0.2.tar.gz.

File metadata

  • Download URL: usort-1.0.2.tar.gz
  • Upload date:
  • Size: 78.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.3

File hashes

Hashes for usort-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f0dbdfcf18b117323dff3a03df804957ba3b755c1069d2cf98bee133592bd369
MD5 39847e3ccb255e2fb9712698c8bc1c29
BLAKE2b-256 ecf60cf8fd139f3deab8f180a03062eb09d0bcfc380baf0df1ae24f82496577d

See more details on using hashes here.

File details

Details for the file usort-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: usort-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 23.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.3

File hashes

Hashes for usort-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0e7ee0702902d4d54fdd35cbc81f5590df2573db29e72aeb6eddaa9e9d01cef9
MD5 bed689d3133144da50ea7b19171a47b8
BLAKE2b-256 1b9b2967208157a740d24b5a059bc1371a97863fb4c392a6766fd473cf11fea9

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