pyorder
A Python linter for ordering source code.
Overview
pyorder is a development tool built on the ast library for ordering Python
source code according to specified rules and conventions. Specifically, it is
designed to order definitions within a Python module. It does not attempt to order
everything in a source file, like attributes or statements outside of definitions.
pyorder focuses on ordering Python code according to best practices regarding
readability of Python code.
Usage
Requires Python 3.12+.
# install pyorder as a development dependency
uv add pyorder --dev
# check Python files
uv run pyorder check path/to/your/file1.py path/to/your/file2.py
# fix Python files
uv run pyorder check --fix path/to/your/file1.py path/to/your/file2.py
pyorder check will only exit with a zero status code if all files were found
to be correctly ordered from the beginning, anything else will cause exit code 1,
even if the files are later fixed with --fix.
The --fix option will modify all files that are not ordered according to the rules.
Although pyorder attempts to respect valid Python syntax, it does NOT guarantee
valid Python syntax or that no logical errors will be introduced after reordering.
It simply reorders the definitions according to the rules. Run pyorder without
the --fix option first to see the diff output of files that would be modified.
Please check all modified files after fixing to ensure that your code is correct.
As can be seen from this design, pyorder can be very nicely integrated as a pre-commit
hook.
What exactly is being ordered
pyorder parses a given file as an abstract syntax tree (AST) and considers instances
of ast.FunctionDef, ast.AsyncFunctionDef, and ast.ClassDef on the module level
and the class level within module-level classes. The philosophy behind this being
that definitions structure a codebase and ordering just those gives a good balance
between a forced order for readability and structure and freedom to write your
code as you please.
Ordering Rules
pyorder enforces the rules on two levels, module-level definitions and
class-level definitions of module-level classes.
The listed rules below are applied as a sorting key in that priority order within
each level of definitions, meaning for any two definitions, the first rule that
ranks them differently decides their order; later rules only break ties.
Module-level definitions
- callers-before-callees: a definition that uses another definition should be defined before it. (Hard dependencies that are needed at definition time are ordered before it, e.g. base classes, arg/kwargs defaults, or decorators).
- functions-before-classes: functions should be defined before classes within the module.
- visibility-ordering: public before private definitions (e.g.,
public,_private,__more_private) - alphabetical-ordering: definitions are ordered alphabetically.
Class-level definitions
- classes-before-methods: nested classes should be defined before methods.
- constructor-first: Constructor methods(e.g.,
__init_subclass__,__new__,__init__) should be defined before other methods. - abstract-first: methods decorated with
@abstractmethodshould be defined before other methods. - overrides-first: methods decorated with
@overrideshould be defined before other methods. - special-methods-first: special methods (e.g.,
__str__,__repr__) should be defined before other methods. - classmethods-first: methods decorated with
@classmethodshould be defined before other methods. - callers-before-callees: a definition that uses another definition should be defined before it. (Hard dependencies that are needed at definition time are ordered before it, e.g. base classes, arg/kwargs defaults, or decorators).
- visibility-ordering: public before private definitions (e.g.,
public,_private,__more_private) within the class. - alphabetical-ordering: definitions are ordered alphabetically within the class.
Example
Before:
def example_class_helper():
pass
class ExampleClass:
def public_method(self):
return self._private_method()
def _private_method(self):
pass
class NestedExample:
pass
def __init__(self):
example_class_helper()
def _private_helper():
return ExampleClass()
def public_caller():
return _private_helper()
After running pyorder check --fix example.py:
def public_caller():
return _private_helper()
def _private_helper():
return ExampleClass()
class ExampleClass:
class NestedExample:
pass
def __init__(self):
example_class_helper()
def public_method(self):
return self._private_method()
def _private_method(self):
pass
def example_class_helper():
pass
Customization
pyorder does not support inline comments like # noqa. The missing support for
inline comments is intentional and is part of this tool's philosophy of enforcing
a consistent ordering of Python source code. However pyorder is built on pyrig-runtime.
Therefore any customizations and adjustments can be achieved by writing a plugin
based on the subclassing system of pyrig-runtime.
Documentation
Please refer to the official documentation for more details and examples.
Metadata
Release files for pyorder 1.0.46
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyorder-1.0.46.tar.gz | 14.8 kB | Without provenance | Details for pyorder-1.0.46.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyorder-1.0.46-py3-none-any.whl | Python 3 | none | any | Details for pyorder-1.0.46-py3-none-any.whl |
Total release size: 30.9 kB
Release files / pyorder-1.0.46.tar.gz
| Download URL | pyorder-1.0.46.tar.gz |
|---|---|
| Size | 14.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f83468bee105fa9ff3c333147a083cec3b6951db7e063e137dd5de1e761f3bb4
|
|
BLAKE2b-256 checksum How to use checksums |
f48adc5ac60338c386cef98702044bd14117edc88524e81dbc6db6cf075b3270
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pyorder-1.0.46-py3-none-any.whl
| Download URL | pyorder-1.0.46-py3-none-any.whl |
|---|---|
| Size | 16.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
77a81ad912ccbba92ba8c09ebca9e97e0c9b06add7ef61816304e78c03bbddf8
|
|
BLAKE2b-256 checksum How to use checksums |
c0db93b5fdb41685a74492cc622a9c0577e21c31aa1043d4dfb24433f636eaaf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for pyorder 1.0.46
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.