Skip to main content
sdsort – gets your methods sorted

Sorts top-level functions and class methods in Python files according to the step-down rule, as described in Robert C. Martin's Clean Code. More concretely, functions and methods are ordered in a depth-first-traversal order of the dependency tree, so higher-level code appears before lower-level code.

Installation

pip install sdsort

Or run directly with uv:

uvx sdsort <path>

Usage

To target individual files, run the sdsort command, followed by the paths to the files that should be sorted:

sdsort <file_1> <file_2>

To sort all *.py files in a directory, and all of its subdirectories, run the sdsort command followed by the directory path:

sdsort <directory_path>

To check if files are already sorted without modifying them, use the --check flag:

sdsort --check <file_or_directory>

This will exit with code 1 if any files would be re-arranged, making it suitable for CI pipelines and pre-commit hooks.

Parallelism

Larger runs are sorted across parallel worker processes. Use --jobs/-j to control how many:

sdsort --jobs 4 <directory_path>

0 (the default) picks one worker per available CPU; 1 disables parallelism and sorts every file in the current process. Runs of fewer than 50 files always stay serial regardless of this setting, since spawning workers costs more than it saves at that scale.

Files that cannot be parsed

If sdsort cannot parse a file, it reports the file on stderr and moves on. The rest of the files are still sorted, and the exit code is unaffected.

Configuration

Method order

Within a class, methods are sorted by call order — the step-down rule — unless told otherwise. Methods can also be grouped by visibility, or ordered alphabetically, via a [tool.sdsort] table in the pyproject.toml file nearest to the file being sorted (the closest one at or above it in the directory tree):

[tool.sdsort]
method-order = ["visibility", "call", "name"]

method-order lists the attributes to sort by, most significant first. The example above groups methods by visibility, orders each group by call order, and falls back to alphabetical order for methods no call relationship ranks. The available attributes are:

  • call — the step-down rule: a method appears before the methods it calls.
  • visibility — grouped by visibility, in the order given by visibility-order (see below).
  • name — alphabetically by method name.

The default is ["call"]. Only methods within classes are affected; top-level functions are always sorted by call order.

Visibility order

A method's visibility is determined by its name:

Visibility Name Example
dunder leading and trailing double underscore __init__
private leading double underscore __cache
protected single leading underscore _helper
public no leading underscore run

visibility-order lists the visibilities in the order their groups should appear:

[tool.sdsort]
method-order = ["visibility", "call"]
visibility-order = ["dunder", "public", "protected", "private"]

That is also the default. Any visibility left out of the list is placed last, keeping whatever order the remaining method-order attributes give it. To place the omitted ones somewhere else, name their position with "*":

visibility-order = ["dunder", "*", "private"]

Dunder methods come first and private methods last, with everything else in between. visibility-order has no effect unless "visibility" appears in method-order.

Invalid configuration

If a [tool.sdsort] table holds something sdsort cannot honour — an unknown key or value, a duplicate, a value that is not a list — sdsort reports every problem it found and exits with code 1.

Skipping a file

To prevent sdsort from modifying a particular file, add a # sdsort: skip_file comment somewhere within the file.

pre-commit

sdsort can be used as a pre-commit hook. Add the following to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/eirikurt/sdsort
    rev: v1.3.1
    hooks:
      - id: sdsort

Metadata

Release files for sdsort 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sdsort 1.3.1
File Size Uploaded Provenance Actions
sdsort-1.3.1.tar.gz 15.8 kB With provenance Details for sdsort-1.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sdsort 1.3.1
File Interpreter ABI Platform Actions
sdsort-1.3.1-py3-none-any.whl Python 3 none any Details for sdsort-1.3.1-py3-none-any.whl

Total release size: 36.3 kB

Release files / sdsort-1.3.1.tar.gz

Download URL sdsort-1.3.1.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0ccb512fb61e3def8aeba96f9b441a831eece6263f12e8b4ac9db0c5eaf73d89
BLAKE2b-256 checksum
How to use checksums
ee22778bb2021347de4dee58998552be832e97a2373df6ff03c1992a47002b25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.

Transparency log

Release files / sdsort-1.3.1-py3-none-any.whl

Download URL sdsort-1.3.1-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e594ca52e06536564e8328b86f246d04ed70ec4963579dd92e6982b00a79f97b
BLAKE2b-256 checksum
How to use checksums
831a9df409ad3bca4848dd4f448b301680de83393861eba50171838d3e163e97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.

Transparency log

Security for sdsort 1.3.1

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sdsort-1.3.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.

Transparency log

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page