Skip to main content

defsort

CI

Defsort applies a configurable ordering policy to Python functions, classes, and methods. It makes definition order deterministic, so placement follows a project convention rather than accumulating from successive edits.

Run defsort to see how much would change across your project:

defsort summary of six Python files, with 15 of 26 definitions and methods moving

Then use defsort --preview to see where definitions move. Colors track definitions between the Current and Proposed columns. For example:

defsort preview showing function movement in catalog.py and method movement in delivery.py

Use the CLI to preview and apply changes, an agent hook to sort before commits, or the Python library to integrate sorting into your own tools.

Installation

defsort requires CPython 3.13 or later. Install the current checkout from the repository root with uv:

uv tool install .

To update an existing installation after pulling changes:

uv tool install --reinstall .

Git 2.42 or newer is required for agent hooks, incremental mode, and a configured reorder guard. Ordinary CLI sorting and the library do not require Git. See platform compatibility for runtime requirements and file-handling details.

Quick start

Start with a summary, preview movement, inspect the diff, then apply the changes:

defsort
defsort --preview
defsort --diff
defsort --write

The first three commands leave files unchanged. They exit 1 when changes or skipped files need attention; exit 2 indicates an error. A successful write exits 0 unless files were skipped.

Choose dependency or alphabetical ordering, with separate policies for top-level definitions and class methods. Automatic sorting trades control over individual placement for consistency. Dependency changes can produce large reorders; alphabetical order can separate related code. Use comment banners to preserve sections. See ordering policies for the choices and tradeoffs.

The defaults use dependency ordering, alphabetical ties, and lifecycle methods first. No configuration is required. To keep class methods in their authored order while sorting top-level definitions, add:

[tool.defsort]
method-order = "none"

Defsort checks for changes that could break definition-time references and preserves decorators, comments, and source layout. Its checks are bounded: classes whose behavior depends on method order should use method-order = "none". Read the safety model before adopting it in order-sensitive code.

For an existing codebase, the incremental guide explains how to establish sorted baselines file by file and limit later reordering.

Integrations

  • Coding agents: the recommended PreToolUse hook sorts staged Python content before supported commits and preserves partial staging. Follow the hook setup guide for Claude Code, Codex, or Antigravity. Choose either plugin installation or manual registration.
  • Multiple formatting hooks: use the composition guide to run modifying hooks in order. It includes the current Antigravity limitation.
  • pre-commit: use a local registration with an installed CLI as described in the pre-commit guide.

Library API

import defsort

source = 'def zebra(): pass\n\ndef alpha(): pass\n'
result = defsort.check(source)
if result.status == 'changed':
    source = defsort.sort(source)

Use check() to inspect skipped or invalid source before deciding what to do. The library reference describes configuration, results, and the public adapter API.

Documentation

If you want to... Read
Choose an ordering policy and understand its limits Concepts and safety
Preview, apply, or automate a sort CLI reference
Configure agent hooks or pre-commit Hook guide
Control reordering during adoption Incremental mode
Call defsort from Python Library reference
Check runtime, encoding, or path behavior Platform compatibility

Development

See the development guide for pinned CI dependencies and lock updates.

Provide the tools in [dependency-groups].dev through your environment or package manager. make install and make reinstall install only the CLI through uv.

make quality # read-only quality checks
make check   # quality checks and one full test run
make format  # apply repository formatters

To contribute an agent integration, see adding a bundled agent.

License

defsort is distributed under the MIT License.

Metadata

Release files for defsort 0.1.0

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

Source distribution (sdist)

Source distribution for defsort 0.1.0
File Size Uploaded
defsort-0.1.0.tar.gz 145.4 kB Details

Built distribution (wheel)

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

Total release size: 313.2 kB

Release files / defsort-0.1.0.tar.gz

Download URL defsort-0.1.0.tar.gz
Size 145.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b9290d8dba064380919d4e6753ffc9779fbe45b85a79565eb7fbe15ac60b1084
BLAKE2b-256 checksum
How to use checksums
e2416089c8d80d4f29a6eec3144b110a43a4bf8a89b78a2dab43d7c96510f98e
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 Sep 30, 2026.

Transparency log

Release files / defsort-0.1.0-py3-none-any.whl

Download URL defsort-0.1.0-py3-none-any.whl
Size 167.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa2743957991e5e7473a4dc1ea1f23c864e85704507a4ac4bbc6fce055bf7ff6
BLAKE2b-256 checksum
How to use checksums
f2dfacfed91cbfeabdbbdfaff7f361a911d15176e22f4939a5d788ab65cdf573
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 Sep 30, 2026.

Transparency log

Release history Release notifications | RSS feed

0.2.0

2 release files

This release

0.1.0 This release

2 release files

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