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>

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.

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.8.post1.tar.gz (83.2 kB view details)

Uploaded Source

Built Distribution

usort-1.0.8.post1-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file usort-1.0.8.post1.tar.gz.

File metadata

  • Download URL: usort-1.0.8.post1.tar.gz
  • Upload date:
  • Size: 83.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/42.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.2.0 tqdm/4.66.2 importlib-metadata/7.0.1 keyring/24.3.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.10

File hashes

Hashes for usort-1.0.8.post1.tar.gz
Algorithm Hash digest
SHA256 68def75f2b20b97390c552c503e071ee06c65ad502c5f94f3bd03f095cf4dfe6
MD5 b6b5c7d8f07934be71f4d86e288e9d4c
BLAKE2b-256 9bf43ef48b43f2645f2cb4a37d6007e611bc669af44eecfee953c5dd57433011

See more details on using hashes here.

File details

Details for the file usort-1.0.8.post1-py3-none-any.whl.

File metadata

  • Download URL: usort-1.0.8.post1-py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/42.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.2.0 tqdm/4.66.2 importlib-metadata/7.0.1 keyring/24.3.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.10

File hashes

Hashes for usort-1.0.8.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c57cdf17b458c79f8a61eb3ce8bf3f93e36d3c2edd602b9b2aa16b6875d3255
MD5 ff5f42cbf2608111abcc20c344066756
BLAKE2b-256 5f55cc51ceb3d93763b9d28def24615bc485212525550967ce9e992a455f9ab5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page