CodeSorter is a LibCST codemod that automatically sorts and organizes Python code.
Warning
Only apply CodeSorter to a code base you own or maintain. Reordering an entire file is a sweeping, opinionated change that conflicts with in-flight work and erases the history of carefully chosen ordering. Opening a pull request that runs CodeSorter across someone else's project is strongly discouraged — it is noisy, unsolicited, and burdensome to review. Adopt it as a pre-commit hook in your own repositories instead, where every contributor benefits from the consistent ordering.
- Smart Sorting: Automatically sorts functions, classes, and methods alphabetically
- Decorator Awareness: Properly handles
@property,@staticmethod,@classmethod, and@pytest.fixturedecorators - Hierarchical Organization: Maintains logical grouping within classes and modules
- Constant Grouping: Orders each scope as constants, then classes, then functions, sorting constants by dependency while preserving enum and dataclass field order
- Keyword Sorting: Alphabetizes keyword arguments in calls, keyword-only parameters,
and dict string keys, while preserving
*/**unpacking semantics and the keyword-argument order of order-sensitive callables such asOrderedDict - Pytest Integration: Special handling for pytest fixtures with proper scope ordering
- CLI Interface: Simple command-line interface for easy integration
- Pre-commit Hook: Ready-to-use pre-commit hook for automated code organization
From PyPI:
# install the codesorter CLI as a standalone tool
uv tool install codesorter
# or add it to a project's lint dependency group
uv add --group lint codesorterFrom Source:
git clone https://github.com/praw-dev/CodeSorter.git
cd CodeSorter
uv tool install .Development Installation:
git clone https://github.com/praw-dev/CodeSorter.git
cd CodeSorter
uv syncThe simplest way to use CodeSorter is through the command-line interface:
# Sort a single file
codesorter my_file.py
# Sort all Python files in a directory
codesorter my_project/
# Sort with additional options
codesorter --helpAdd CodeSorter to your pre-commit configuration to automatically sort code on every commit:
# .pre-commit-config.yaml
repos:
- repo: https://github.com/praw-dev/CodeSorter
rev: v0.2.5
hooks:
- id: codesorterOr use the check-only variant, which fails the hook without modifying files:
repos:
- repo: https://github.com/praw-dev/CodeSorter
rev: v0.2.5
hooks:
- id: codesorter-checkYou can also use CodeSorter programmatically:
import libcst as cst
from codesorter.sort_code import SortCodeCommand
from libcst.codemod import CodemodContext
# Parse your code
code = """
def z_function():
pass
def a_function():
pass
"""
# Create context and command
context = CodemodContext()
command = SortCodeCommand(context)
# Transform the code
result = command.transform_module(cst.parse_module(code))
print(result.code)CodeSorter uses LibCST (Concrete Syntax Tree) to parse and transform Python code. It applies sophisticated sorting rules:
- Functions are sorted alphabetically by name
- Global functions are sorted separately from class methods
- Methods are grouped by kind, in this order:
@abstractmethodmethods- pytest fixtures (
autousefixtures first) @staticmethodmethods@classmethodmethods- cached properties and
@propertymethods (getter, then setter, then deleter) @contextmanagermethods- regular instance methods
- Within each group, methods are sorted alphabetically, with leading-underscore
(
_privateand__dunder__) names ahead of public ones
- Fixtures are sorted by scope (session, package, module, class, function)
- Within each scope, fixtures are sorted alphabetically
autousefixtures are handled specially
Before:
class MyClass:
def z_method(self):
pass
@property
def a_property(self):
pass
@staticmethod
def b_static():
passAfter:
class MyClass:
@staticmethod
def b_static():
pass
@property
def a_property(self):
pass
def z_method(self):
pass# Clone the repository
git clone https://github.com/praw-dev/CodeSorter.git
cd CodeSorter
# Install with development dependencies
uv sync
# Install pre-commit hooks
uv run pre-commit install# Run all tests
uv run pytest
# Run a specific test file
uv run pytest tests/test_sort_code.py
# Run the full tox matrix (tests, type, pre-commit)
uv run toxThe project uses several tools to maintain code quality:
- Ruff: Fast linting and formatting
- Pyright: Type checking
- Pre-commit: Automated quality checks
Run all quality checks:
uv run pre-commit run --all-files- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Run the test suite:
uv run pytest - Run pre-commit hooks:
pre-commit run --all-files - Commit your changes:
git commit -m "Add feature" - Push to your fork:
git push origin feature-name - Create a Pull Request
See the examples/ directory for before and after examples of CodeSorter in action:
examples/before_example.py: Unsorted codeexamples/after_example.py: Same code after sorting
This project is licensed under the MIT License - see the LICENSE.txt file for details.
See the change log for the full list of changes.