Skip to main content

funcsort

A Python tool that automatically sorts class methods and module-level functions into configurable, regex-matched groups.

funcsort ships with a default configuration that reproduces its classic behaviour — sorting class methods by visibility (creational → dunder → public → protected → private) and type (instance → class → static) — but the engine underneath is fully generic: you define your own ordered groups, match member names with regular expressions, and control sorting at both class and module scope.

📖 Documentation

Features

  • Generic, configuration-driven engine with a behaviour-preserving default
  • Dependency-safe: never moves a definition above code that reads it at import time
  • Define your own groups and ordering; match member names with regex
  • Sort both class methods and module-level functions
  • Optionally sort module-level assignments/constants by opting a group into them
  • Per-group filters by member kind, method type and scope
  • Configurable via a dedicated funcsort.toml or [tool.funcsort] in pyproject.toml
  • Pre-commit hook integration, colored output, check mode (CI) and diff mode

Installation

# Using uv (recommended)
uv add funcsort

# Using pip
pip install funcsort

# For development
git clone https://github.com/HEROgold/funcsort
cd funcsort
uv sync

Configuration

funcsort reads configuration from a dedicated funcsort.toml if present, otherwise from the [tool.funcsort] table of pyproject.toml. Both use the same [tool.funcsort] section and keys.

[tool.funcsort]
# Method type ordering within each group (secondary sort, optional)
# Options: "instance", "class", "static"   Default: ["instance", "class", "static"]
method_type_order = ["instance", "class", "static"]

# Sort module-level functions too (default: true)
sort_module = true

# Avoids moving definitions that could raise NameErrors or break code dependencies
# (default: true). See the docs on dependency safety:
# https://herogold.github.io/funcsort/dependency-safety/
respect_dependencies = true

# Where module-level functions go relative to module-level classes
# Options: "after-classes", "before-classes", "interleaved"   Default: "after-classes"
function_placement = "after-classes"

# Exclude files/directories matching these glob patterns (optional)
# exclude = ["tests/*", "migrations/*.py"]

With no configuration, funcsort uses its built-in default groups. For full control, define an ordered list of [[tool.funcsort.groups]]; this replaces the built-in groups entirely, and the list order is the output order:

# Sort module-level UPPER_CASE constants to the very top.
[[tool.funcsort.groups]]
name = "constants"
match = "^[A-Z][A-Z0-9_]*$"
kind = ["assignment"]   # opt this group into assignments
scope = "module"        # only at module scope

# Catch-all so nothing is ever "unmatched".
[[tool.funcsort.groups]]
name = "everything_else"
match = ".*"

Every key, the built-in default groups and the per-group filters are documented under Configuration.

Usage

# Sort a single file, several files, or a directory (recursive by default)
funcsort example.py
funcsort file1.py file2.py
funcsort src/

# Non-recursive directory sorting
funcsort src/ --no-recursive

# Check if files need sorting (useful for CI), and show a diff
funcsort --check src/
funcsort --diff example.py

# Sort class methods only, leaving module-level functions untouched
funcsort --no-sort-module src/

# Sort by group order alone, ignoring load-time dependencies (can break imports)
funcsort --no-respect-dependencies src/

# Put module-level functions above classes instead of below them
funcsort --function-placement before-classes src/

# Exclude specific files or directories
funcsort --exclude "tests/*" --exclude "migrations/*.py" src/

Note: By default, funcsort excludes all dot-prefixed directories (e.g. .venv, .git, .pytest_cache) and common build directories (venv, __pycache__, node_modules) when scanning directories recursively. You can add custom exclusions via CLI flags or the config file.

Individual files, classes and members can opt out of sorting with a # nosort comment — see Skipping sorting.

Pre-commit Integration

Add to your .pre-commit-config.yaml:

repos:
  - repo: local
    hooks:
      - id: funcsort
        name: funcsort
        entry: funcsort
        language: python
        types: [python]
        additional_dependencies: ["funcsort"]

Then install the hook:

pip install pre-commit
pre-commit install

Documentation

Development

# Install dependencies
uv sync

# Run on example file
uv run funcsort example.py

# Test with check mode
uv run funcsort --check example.py

# View diff
uv run funcsort --diff example.py

# Build the documentation site locally
uv run --group docs zensical serve

License

MIT

Metadata

Release files for funcsort 2.0.2

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

Source distribution (sdist)

Source distribution for funcsort 2.0.2
File Size Uploaded Provenance Actions
funcsort-2.0.2.tar.gz 125.9 kB Without provenance Details for funcsort-2.0.2.tar.gz

Built distribution (wheel)

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

Total release size: 157.9 kB

Release files / funcsort-2.0.2.tar.gz

Download URL funcsort-2.0.2.tar.gz
Size 125.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c7b2af774b0d2c713d1118a38e80f31984aa54773729a8d4e6f75f7046b56ac9
BLAKE2b-256 checksum
How to use checksums
c6496815844d13254720032b60fcc1485c25383919a48b91de0b7b4d47e1483a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / funcsort-2.0.2-py3-none-any.whl

Download URL funcsort-2.0.2-py3-none-any.whl
Size 32.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c3ac97dc7e32f7857e2f45860102e8b04da25391c2830dbe5635d3299ed623d
BLAKE2b-256 checksum
How to use checksums
c53f5bf2e39429be5c0372b31b6b8429a2c06b310503be0ca6617f63af85796c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for funcsort 2.0.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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