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
- PyPI Package: pylint-sort-functions
- GitHub Repository: pylint-sort-functions
- Issue Tracker: GitHub Issues
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pylint_sort_functions-1.0.1.tar.gz.
File metadata
- Download URL: pylint_sort_functions-1.0.1.tar.gz
- Upload date:
- Size: 114.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e33647c29bb92ea8028b11bdf56dff42bbf39d93d1cc07ee8a8b9b3f117a69a7
|
|
| MD5 |
165d9607f083aec952f31050dbf483e1
|
|
| BLAKE2b-256 |
65d5fa3ac617b0ad8508c6014cd1b7d848bdc62cb15f975d9287b634a855e0b9
|
File details
Details for the file pylint_sort_functions-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pylint_sort_functions-1.0.1-py3-none-any.whl
- Upload date:
- Size: 21.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2b6fafe98481aefea67e33cd90cfd9e726d433c7acc0a4963ea896b6c5e8126
|
|
| MD5 |
d3a476b0130de6341ff2c53e95b223ef
|
|
| BLAKE2b-256 |
30551d407770414215685c83f7c2094c17ee7386985f677b05656c0399594331
|