Skip to main content

Pylint plugin to sort functions in Python code

Project description

pylint-sort-functions

A PyLint plugin that enforces alphabetical sorting of functions and methods within Python classes and modules, helping maintain consistent and predictable code organization.

Features

  • Function Organization: Enforces alphabetical sorting of functions within modules
  • Method Organization: Enforces alphabetical sorting of methods within classes
  • Public/Private Separation: Ensures public functions/methods come before private ones (underscore prefix)
  • Auto-fix Capability: Automatically reorder functions and methods with the included CLI tool
  • Comment Preservation: Comments move with their associated functions during sorting
  • Framework Integration: Supports decorator exclusions for Flask, Click, FastAPI, Django
  • Performance Optimized: Intelligent caching for large projects (100+ files)
  • Configurable Privacy Detection: Customizable patterns for public API identification
  • Enterprise Ready: 100% test coverage, comprehensive documentation

Installation

pip install pylint-sort-functions

Quick Start

1. Enable the Plugin

Add the plugin to your pylint configuration:

pylint --load-plugins=pylint_sort_functions your_module.py

Or add to your .pylintrc file:

[MASTER]
load-plugins = pylint_sort_functions

Or in pyproject.toml:

[tool.pylint.MASTER]
load-plugins = ["pylint_sort_functions"]

2. Auto-fix Violations

Use the included CLI tool to automatically reorder functions:

pylint-sort-functions path/to/file.py  # Fix single file
pylint-sort-functions src/            # Fix entire directory

Example

❌ Bad (will trigger warnings):

class MyClass:
    def public_method_b(self):
        pass

    def _private_method_a(self):
        pass

    def public_method_a(self):  # Out of order!
        pass

✅ Good (follows sorting rules):

class MyClass:
    # Public methods
    def public_method_a(self):
        pass

    def public_method_b(self):
        pass

    # Private methods
    def _private_method_a(self):
        pass

Message Codes

  • W9001: unsorted-functions - Functions not sorted alphabetically within their scope
  • W9002: unsorted-methods - Class methods not sorted alphabetically within their scope
  • W9003: mixed-function-visibility - Public and private functions not properly separated

Advanced Configuration

Framework Integration

Exclude framework decorators from sorting requirements:

[tool.pylint-sort-functions]
ignore-decorators = ["@app.route", "@*.command", "@pytest.fixture"]

Privacy Detection

Configure which functions are always considered public API:

[tool.pylint-sort-functions]
public-api-patterns = ["main", "run", "setup", "teardown", "handler"]
enable-privacy-detection = true

Documentation

For comprehensive documentation, including:

  • CLI Reference: Complete command-line tool documentation
  • Configuration Guide: PyLint integration and advanced options
  • Algorithm Details: How function sorting and privacy detection work
  • Framework Integration: Flask, Django, FastAPI, Click examples

See hakonhagland.github.io/pylint-sort-functions

Links

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

pylint_sort_functions-1.1.0.tar.gz (323.8 kB view details)

Uploaded Source

Built Distribution

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

pylint_sort_functions-1.1.0-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file pylint_sort_functions-1.1.0.tar.gz.

File metadata

  • Download URL: pylint_sort_functions-1.1.0.tar.gz
  • Upload date:
  • Size: 323.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for pylint_sort_functions-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a76688b112527aca06e37d1c973954c673a470ef2c14160cc3559615b81b1e21
MD5 25dad10eddacfad07419595a8e209de6
BLAKE2b-256 0739dcf3942e2bad19056b5d11e287df5c581033333e23036c7518db87f7fa9c

See more details on using hashes here.

File details

Details for the file pylint_sort_functions-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pylint_sort_functions-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec9772cd765fa694bb3fde73c4baa7ad09a6a7d1ec767801b59b206aab24e9e2
MD5 4463a02c8fe892cc3fe76bd93bf19663
BLAKE2b-256 222b6e13664ca86156ff5018fe3a898d44f632823d8bc5db8677d77358d31521

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