Skip to main content

A tool to sort class methods by visibility (public, protected, private)

Project description

undersort

A Python tool that automatically sorts class methods by visibility (public, protected, private) and type (class, static, instance).

Features

  • Automatically reorders class methods based on visibility and method type
  • Two-level sorting: primary by visibility, secondary by method type
  • Fully configurable ordering via pyproject.toml
  • Pre-commit hook integration
  • Colored output for better readability
  • Check mode for CI/CD validation
  • Diff mode to preview changes

Installation

# Using uv (recommended)
uv pip install -e .

# Using pip
pip install -e .

Configuration

Configure the method ordering in your pyproject.toml:

[tool.undersort]
# Method visibility ordering (primary sort)
# Options: "public", "protected", "private"
order = ["public", "protected", "private"]

# Method type ordering within each visibility level (secondary sort, optional)
# Options: "class" (classmethod), "static" (staticmethod), "instance" (regular methods)
# Default: ["class", "static", "instance"]
method_type_order = ["class", "static", "instance"]

Method Visibility Rules

  • Public methods: No underscore prefix (e.g., def method()) or magic methods (e.g., __init__, __str__)
  • Protected methods: Single underscore prefix (e.g., def _method())
  • Private methods: Double underscore prefix, not magic (e.g., def __method())

Method Type Rules

  • Class methods: Decorated with @classmethod
  • Static methods: Decorated with @staticmethod
  • Instance methods: Regular methods (no special decorator)

Sorting Behavior

Methods are sorted in two levels:

  1. Primary: By visibility (public → protected → private)
  2. Secondary: Within each visibility level, by method type (class → static → instance by default)

Example: With default configuration, the order would be:

  1. Public class methods
  2. Public static methods
  3. Public instance methods
  4. Protected class methods
  5. Protected static methods
  6. Protected instance methods
  7. Private class methods
  8. Private static methods
  9. Private instance methods

Usage

Command Line

# Sort a single file
undersort example.py

# Sort multiple files
undersort file1.py file2.py file3.py

# Sort all Python files in a directory (recursive by default)
undersort src/

# Sort all Python files in current directory and subdirectories
undersort .

# Non-recursive directory sorting (only files in the directory, not subdirectories)
undersort src/ --no-recursive

# Wildcards work too (expanded by shell)
undersort *.py
undersort src/**/*.py

# Check if files need sorting (useful for CI)
undersort --check example.py
undersort --check src/

# Show diff of changes
undersort --diff example.py

# Combine flags
undersort --check --diff src/

Note: By default, undersort excludes all dot-prefixed directories (e.g., .venv, .git, .pytest_cache) and common build directories (venv, __pycache__, node_modules) when scanning directories recursively.

Pre-commit Integration

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

repos:
  - repo: local
    hooks:
      - id: undersort
        name: undersort
        entry: uv run undersort
        language: system
        types: [python]

Then install the hook:

pip install pre-commit
pre-commit install

Example

Before

class Example:
    def _protected_instance(self):
        pass

    @staticmethod
    def public_static():
        pass

    def __init__(self):
        pass

    @classmethod
    def _protected_class(cls):
        pass

    def public_instance(self):
        pass

    def __private_method(self):
        pass

    @classmethod
    def public_class(cls):
        pass

After (with default config)

class Example:
    @classmethod
    def public_class(cls):
        pass

    @staticmethod
    def public_static():
        pass

    def __init__(self):
        pass

    def public_instance(self):
        pass

    @classmethod
    def _protected_class(cls):
        pass

    def _protected_instance(self):
        pass

    def __private_method(self):
        pass

The methods are now organized by:

  1. Visibility: public (including __init__) → protected → private
  2. Type (within each visibility): class → static → instance

Development

# Install dependencies
uv sync

# Run on example file
uv run undersort example.py

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

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

License

MIT

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

undersort-0.1.1.tar.gz (66.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

undersort-0.1.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file undersort-0.1.1.tar.gz.

File metadata

  • Download URL: undersort-0.1.1.tar.gz
  • Upload date:
  • Size: 66.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for undersort-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5f1eddf5e5e13bc35103dbf5f6f0a3614752a22f73cfe162a4b0cb50e0eea94a
MD5 72e2e2e54b24123c825049df13d159f4
BLAKE2b-256 5d1b6272cec1ad69c8460f57228791987d3837aeedc3f0b4e2a06dabbc7f04a7

See more details on using hashes here.

File details

Details for the file undersort-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: undersort-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for undersort-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 432c602b73494e0fa96a3bac42a755c4b19f54f33620254c24a42898c3a73290
MD5 08abedd73998dd5de079548c857662a0
BLAKE2b-256 c3646ed251d3b119fb8bc767251887a835deb4c1c26b042e9491bcaeae6363e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page