usort


Nameusort JSON
Version 1.0.8.post1 PyPI version JSON
download
home_pagehttps://github.com/facebook/usort
SummaryA small, safe import sorter
upload_time2024-02-12 04:29:33
maintainer
docs_urlNone
authorTim Hatch, Facebook
requires_python>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # μ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, 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:

```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.6 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 <path> [<path> ...]
```

To generate a diff of changes without modifying files:

```shell-session
$ usort diff <path>
```

To just validate that files are formatted correctly, like during CI:

```shell-session
$ usort check <path>
```

### 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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/facebook/usort",
    "name": "usort",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Tim Hatch, Facebook",
    "author_email": "thatch@fb.com",
    "download_url": "https://files.pythonhosted.org/packages/9b/f4/3ef48b43f2645f2cb4a37d6007e611bc669af44eecfee953c5dd57433011/usort-1.0.8.post1.tar.gz",
    "platform": null,
    "description": "# \u03bcsort\n\n**Safe, minimal import sorting for Python projects.**\n\n[![documentation](https://readthedocs.org/projects/usort/badge/?version=stable)](https://usort.readthedocs.io/en/stable/?badge=stable)\n[![version](https://img.shields.io/pypi/v/usort.svg)](https://pypi.org/project/usort)\n[![changelog](https://img.shields.io/badge/change-log-blue.svg)](https://usort.readthedocs.io/en/latest/changelog.html)\n[![license](https://img.shields.io/pypi/l/usort.svg)](https://github.com/facebook/usort/blob/main/LICENSE)\n\n\u03bcsort is a safe, minimal import sorter. Its primary goal is to make no \"dangerous\"\nchanges to code. This is achieved by detecting distinct \"blocks\" of imports that are\nthe most likely to be safely interchangeable, and only reordering imports within these\nblocks without altering formatting. Code style is left as an exercise for linters\nand formatters.\n\nWithin a block, \u00b5sort will follow common Python conventions for grouping imports based\non source (standard library, third-party, first-party, or relative), and then sorting\nlexicographically within each group. This will commonly look like:\n\n```py\nimport re\nfrom pathlib import Path\nfrom typing import Iterable\nfrom unittest.mock import call, Mock, patch\n\nimport aiohttp\nfrom aiosqlite import connect\n\nimport foo\nfrom bar import bar\n\nfrom .main import main\n```\n\nBlocks are inferred from a number of real world conditions, including any intermediate\nstatements between imports:\n\n```py\nimport warnings\nwarnings.filterwarnings(...)\n\nimport re\nimport sys\n```\n\nIn this case, \u00b5sort detects two blocks\u2013separated by the call to `filterwarnings()`,\nand will only sort imports inside of each block. Running \u00b5sort on this code\nwill generate no changes, because each block is already sorted.\n\nImports can be excluded from blocks using the `#usort:skip` directive, or with\n`#isort:skip` for compatibility with existing codebases. \u00b5sort will leave\nthese imports unchanged, and treat them as block separators.\n\nSee the [User Guide][] for more details about how blocks are detected,\nand how sorting is performed.\n\n\n## Install\n\n\u00b5sort requires Python 3.6 or newer to run. Install \u00b5sort with:\n\n```shell-session\n$ pip install usort\n```\n\n\n## Usage\n\nTo format one or more files or directories in-place:\n\n```shell-session\n$ usort format <path> [<path> ...]\n```\n\nTo generate a diff of changes without modifying files:\n\n```shell-session\n$ usort diff <path>\n```\n\nTo just validate that files are formatted correctly, like during CI:\n\n```shell-session\n$ usort check <path>\n```\n\n### pre-commit\n\n\u00b5sort provides a [pre-commit](https://pre-commit.com/) hook. To enforce sorted\nimports before every commit, add the following to your `.pre-commit-config.yaml`\nfile:\n\n```yaml\n- repo: https://github.com/facebook/usort\n  rev: v1.0.7\n  hooks:\n    - id: usort\n```\n\n## License\n\n\u03bcsort is MIT licensed, as found in the [LICENSE][] file.\n\n[LICENSE]: https://github.com/facebook/usort/tree/main/LICENSE\n[User Guide]: https://usort.readthedocs.io/en/stable/guide.html\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A small, safe import sorter",
    "version": "1.0.8.post1",
    "project_urls": {
        "Homepage": "https://github.com/facebook/usort"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5f55cc51ceb3d93763b9d28def24615bc485212525550967ce9e992a455f9ab5",
                "md5": "ff5f42cbf2608111abcc20c344066756",
                "sha256": "6c57cdf17b458c79f8a61eb3ce8bf3f93e36d3c2edd602b9b2aa16b6875d3255"
            },
            "downloads": -1,
            "filename": "usort-1.0.8.post1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ff5f42cbf2608111abcc20c344066756",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 37281,
            "upload_time": "2024-02-12T04:29:31",
            "upload_time_iso_8601": "2024-02-12T04:29:31.693945Z",
            "url": "https://files.pythonhosted.org/packages/5f/55/cc51ceb3d93763b9d28def24615bc485212525550967ce9e992a455f9ab5/usort-1.0.8.post1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9bf43ef48b43f2645f2cb4a37d6007e611bc669af44eecfee953c5dd57433011",
                "md5": "b6b5c7d8f07934be71f4d86e288e9d4c",
                "sha256": "68def75f2b20b97390c552c503e071ee06c65ad502c5f94f3bd03f095cf4dfe6"
            },
            "downloads": -1,
            "filename": "usort-1.0.8.post1.tar.gz",
            "has_sig": false,
            "md5_digest": "b6b5c7d8f07934be71f4d86e288e9d4c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 83215,
            "upload_time": "2024-02-12T04:29:33",
            "upload_time_iso_8601": "2024-02-12T04:29:33.632650Z",
            "url": "https://files.pythonhosted.org/packages/9b/f4/3ef48b43f2645f2cb4a37d6007e611bc669af44eecfee953c5dd57433011/usort-1.0.8.post1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-12 04:29:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "facebook",
    "github_project": "usort",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "usort"
}
        
Elapsed time: 0.18626s