Skip to main content

A small, safe import sorter

Project description

μsort

Safe, minimal import sorting for Python projects.

version changelog license code style

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

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-0.6.1.tar.gz (36.3 kB view details)

Uploaded Source

Built Distribution

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

usort-0.6.1-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: usort-0.6.1.tar.gz
  • Upload date:
  • Size: 36.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for usort-0.6.1.tar.gz
Algorithm Hash digest
SHA256 b3efe8ec38f668c67e00f0f517a092244feb7d94d5267cdca5fb17adc0f4b9c1
MD5 c0e541752eada8e4e6341ac68f468d04
BLAKE2b-256 d9043bca35d42fed4906b143a287714d2fcce76375290caea81aa1e0c252d8e1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: usort-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for usort-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 750eed27d7d1e6c2cde4ad370b06f218f554db1db063a0a66c4cd858e4c8a1b0
MD5 eb0c458921b96b46e6297f49d63dfe79
BLAKE2b-256 973278c1aef3f6114e80d2c2dad5c104ea82fc86f37cf1efb7f67633ee7a7041

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