Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/facebook/usort
Safe, minimal import sorting for Python projects.
https://github.com/facebook/usort
Last synced: about 1 month ago
JSON representation
Safe, minimal import sorting for Python projects.
- Host: GitHub
- URL: https://github.com/facebook/usort
- Owner: facebook
- License: mit
- Created: 2020-01-23T18:53:16.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-05-01T14:26:46.000Z (7 months ago)
- Last Synced: 2024-05-21T01:14:52.922Z (6 months ago)
- Language: Python
- Homepage: https://usort.readthedocs.io
- Size: 526 KB
- Stars: 174
- Watchers: 18
- Forks: 22
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-python-code-formatters - usort
README
# μsort
**Safe, minimal import sorting for Python projects.**
[![documentation](https://readthedocs.org/projects/usort/badge/?version=stable)](https://usort.readthedocs.io/en/stable/?badge=stable)
[![version](https://img.shields.io/pypi/v/usort.svg)](https://pypi.org/project/usort)
[![changelog](https://img.shields.io/badge/change-log-blue.svg)](https://usort.readthedocs.io/en/latest/changelog.html)
[![license](https://img.shields.io/pypi/l/usort.svg)](https://github.com/facebook/usort/blob/main/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:```py
import re
from pathlib import Path
from typing import Iterable
from unittest.mock import call, Mock, patchimport aiohttp
from aiosqlite import connectimport foo
from bar import barfrom .main import main
```Blocks are inferred from a number of real world conditions, including any intermediate
statements between imports:```py
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.8 or newer to run. Install µsort with:
```shell-session
$ pip install usort
```## Usage
To format one or more files or directories in-place:
```shell-session
$ usort format [ ...]
```To generate a diff of changes without modifying files:
```shell-session
$ usort diff
```To just validate that files are formatted correctly, like during CI:
```shell-session
$ usort check
```### pre-commit
µsort provides a [pre-commit](https://pre-commit.com/) hook. To enforce sorted
imports before every commit, add the following to your `.pre-commit-config.yaml`
file:```yaml
- repo: https://github.com/facebook/usort
rev: v1.0.7
hooks:
- id: usort
```## License
μsort is MIT licensed, as found in the [LICENSE][] file.
[LICENSE]: https://github.com/facebook/usort/tree/main/LICENSE
[User Guide]: https://usort.readthedocs.io/en/stable/guide.html