μsort
Safe, minimal import sorting for Python projects.
μ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.10 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.
Metadata
Release files for usort 1.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| usort-1.1.3.tar.gz | 84.7 kB | Without provenance | Details for usort-1.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| usort-1.1.3-py3-none-any.whl | Python 3 | none | any | Details for usort-1.1.3-py3-none-any.whl |
Total release size: 126.7 kB
Release files / usort-1.1.3.tar.gz
| Download URL | usort-1.1.3.tar.gz |
|---|---|
| Size | 84.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3928043b4644f35c80e417698b0e89cc7bb51a1b0a021f2ba55ceffb86326398
|
|
BLAKE2b-256 checksum How to use checksums |
66f4b10e2c565f6c79a06bb9e8f97834044eec8810897338e529dd537f4ba475
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.28.1
|
Release files / usort-1.1.3-py3-none-any.whl
| Download URL | usort-1.1.3-py3-none-any.whl |
|---|---|
| Size | 41.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
75c12bf442bc9529de6c5bcbf5c7fd55215b3ba8e9db6969e8e564abf119aa4c
|
|
BLAKE2b-256 checksum How to use checksums |
39581cb7ace637ca87642aaf6543d699dea701e5686787b5b32c9e1f0f686ec0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.28.1
|
Security for usort 1.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.