Skip to main content

The python statement sorter

Project description

Build Status

The python source code sorter.

Sorts the contents of python modules so that statements are placed after the things they depend on, but leaves grouping to the programmer. Groups class members by type and enforces topological sorting of methods.

Makes old fashioned code navigation easier, you can always scroll up to see where something is defined, and reduces bikeshedding.

Compatible with and intended to complement isort and black.

Before:

from module import BaseClass

def function():
    return _dependency()

def _decorator(fn):
    return fn

@_decorator
def _dependency():
    return Class()

class Class(BaseClass):
    def public_method(self):
        return self

    def __init__(self):
        pass

After:

from module import BaseClass

class Class(BaseClass):
    def __init__(self):
        pass

    def public_method(self):
        return self

def _decorator(fn):
    return fn

@_decorator
def _dependency():
    return Class()

def function():
    return _dependency()

Installation

SSort can be installed manually using pip.

$ pip install ssort

Usage

To check that a file is correctly sorted use the –check flag. –diff can be passed to see what changes ssort would make.

$ ssort --check --diff path/to/python_module.py

To allow ssort to rearrange your file, simply invoke with no extra flags. If ssort needs to make changes to a black conformant file, the result will not necessarily be black conformant. The result of running black on an ssort conformant file will always be ssort conformant. We recommend that you reformat using isort and black immediately after running ssort.

$ ssort src/ tests/ setup.py; isort src/ tests/ setup.py; black src/ tests/ setup.py

You can also setup ssort to run automatically before commit by setting up pre-commit, and registering ssort in your .pre-commit-config.yaml.

repos:
# ...
- repo: https://github.com/bwhmather/ssort
  rev: 0.10.6
  hooks:
  - id: ssort
- repo: https://github.com/pycqa/isort
  rev: 5.10.1
  hooks:
  - id: isort
    name: isort (python)
    args: [--profile=black]
- repo: https://github.com/psf/black
  rev: 22.1.0
  hooks:
  - id: black

Output

ssort will sort top level statements and statements in classes.

When sorting top level statements, ssort follows three simple rules:
  • Statements must always be moved after the statements that they depend on, unless there is a cycle.

  • If there is a cycle, the order of statements within the cycle must not be changed.

  • If there is no dependency between statements then, to the greatest extent possible, the original order should be kept.

ssort is more opinionated about the order of statements in classes:
  • Class attributes should be moved to the top of the class and must always be kept in their original order.

  • Lifecycle (__init__, __new__, etc) methods, and the methods they depend on, should go next.

  • Regular methods follow, dependencies always ahead of the methods that depend on them.

  • Other d’under methods should go at the end in a fixed order.

License

The project is made available under the terms of the MIT license. See LICENSE for details.

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

ssort-0.10.6.tar.gz (21.0 kB view details)

Uploaded Source

File details

Details for the file ssort-0.10.6.tar.gz.

File metadata

  • Download URL: ssort-0.10.6.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for ssort-0.10.6.tar.gz
Algorithm Hash digest
SHA256 cda06b917d5625db5bfe1ef79066bc9dd1942784168113cc05fe795943c420e9
MD5 cbfdb50bf3370a1e4a49d9063aefe751
BLAKE2b-256 c249265008e02961bf67925cf513bf70158e6840d4a60bfee6e9232a053ef75b

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