A Python codemod that sorts and organizes code in your files.
Project description
CodeSorter is a LibCST codemod that automatically sorts and organizes Python code.
Features
Smart Sorting: Automatically sorts functions, classes, and methods alphabetically
Decorator Awareness: Properly handles @property, @staticmethod, @classmethod, and @pytest.fixture decorators
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
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
Installation
From PyPI:
# using uv and add to the lint dependency group
uv add --group lint codesorter
# or using pip
pip install codesorter
From Source:
git clone https://github.com/praw-dev/CodeSorter.git
cd CodeSorter
uv pip install -e .
Development Installation:
git clone https://github.com/praw-dev/CodeSorter.git
cd CodeSorter
uv sync
Usage
Command Line Interface
The 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 --help
Pre-commit Hook
Add 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.0.3
hooks:
- id: codesorter
Or use the check-only variant, which fails the hook without modifying files:
repos:
- repo: https://github.com/praw-dev/CodeSorter
rev: v0.0.3
hooks:
- id: codesorter-check
Programmatic Usage
You 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)
How It Works
CodeSorter uses LibCST (Concrete Syntax Tree) to parse and transform Python code. It applies sophisticated sorting rules:
Function Sorting
Functions are sorted alphabetically by name
Global functions are sorted separately from class methods
Class Method Sorting
Methods are sorted with special consideration for decorators: - @property methods (getters, setters, deleters) - @staticmethod methods - @classmethod methods - Regular instance methods
Methods within classes maintain their logical grouping
Pytest Fixture Sorting
Fixtures are sorted by scope (session, package, module, class, function)
Within each scope, fixtures are sorted alphabetically
autouse fixtures are handled specially
Example Transformation
Before:
class MyClass:
def z_method(self):
pass
@property
def a_property(self):
pass
@staticmethod
def b_static():
pass
After:
class MyClass:
@property
def a_property(self):
pass
@staticmethod
def b_static():
pass
def z_method(self):
pass
Development
Setting Up Development Environment
# 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
Running Tests
# 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 tox
Code Quality
The 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
Contributing
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
Examples
See the examples/ directory for before and after examples of CodeSorter in action:
examples/before_example.py: Unsorted code
examples/after_example.py: Same code after sorting
License
This project is licensed under the MIT License - see the LICENSE.txt file for details.
Changelog
See the change log for the full list of changes.
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 codesorter-0.2.1.tar.gz.
File metadata
- Download URL: codesorter-0.2.1.tar.gz
- Upload date:
- Size: 98.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c9c2a0a124570292d02e0970d282e61db08d30ca8fa75cf88a69b09bbe631bf
|
|
| MD5 |
aaeaf2ac693136b9aff44e3fea74c66b
|
|
| BLAKE2b-256 |
b57abae600e1d64732b610747367b483132ccd36e69339f22442c0460e65d959
|
Provenance
The following attestation bundles were made for codesorter-0.2.1.tar.gz:
Publisher:
pypi.yml on praw-dev/CodeSorter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codesorter-0.2.1.tar.gz -
Subject digest:
8c9c2a0a124570292d02e0970d282e61db08d30ca8fa75cf88a69b09bbe631bf - Sigstore transparency entry: 1818924601
- Sigstore integration time:
-
Permalink:
praw-dev/CodeSorter@15352cfcc5eee670a442275efaaa1467b51cb86b -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/praw-dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@15352cfcc5eee670a442275efaaa1467b51cb86b -
Trigger Event:
release
-
Statement type:
File details
Details for the file codesorter-0.2.1-py3-none-any.whl.
File metadata
- Download URL: codesorter-0.2.1-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77dd2d0aeb12ce34af4df75fa370bf4a51494182caa15763810c905fe4719d3f
|
|
| MD5 |
05e64d21ba606720c730c42172340f9b
|
|
| BLAKE2b-256 |
503bc617b60d95005ce08150ab8cddb000ba9c5b64a02921357a7c96bf3562c5
|
Provenance
The following attestation bundles were made for codesorter-0.2.1-py3-none-any.whl:
Publisher:
pypi.yml on praw-dev/CodeSorter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codesorter-0.2.1-py3-none-any.whl -
Subject digest:
77dd2d0aeb12ce34af4df75fa370bf4a51494182caa15763810c905fe4719d3f - Sigstore transparency entry: 1818924641
- Sigstore integration time:
-
Permalink:
praw-dev/CodeSorter@15352cfcc5eee670a442275efaaa1467b51cb86b -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/praw-dev
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@15352cfcc5eee670a442275efaaa1467b51cb86b -
Trigger Event:
release
-
Statement type: