pyorder
A Python source code ordering linter.
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
# install pyorder as a development dependency
uv add pyorder --dev
# check Python files
uv run pyorder path/to/your/file1.py path/to/your/file2.py
# fix Python files
uv run pyorder --fix path/to/your/file1.py path/to/your/file2.py
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.
What exactly is being ordered
pyorder parses a given file as an abstract syntax tree (AST) and considers the
following types of definitions within a Python module:
- Module-level definitions, specifically instances of
ast.FunctionDef,ast.AsyncFunctionDefandast.ClassDef - Module-level class definitions, specifically instances of
ast.ClassDef,ast.FunctionDef, andast.AsyncFunctionDef
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.
Module-level definitions
- callers-before-callees: a definition that calls another definition appears before that definition (excluded are hard dependencies that are needed at definition time, 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.,
__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 calls another definition should be defined before that definition (excluded are hard dependencies that are needed at definition time, 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.
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 customization can be done by writing a plugin. pyrig
is another tool built on pyrig-runtime and describes how to write a plugin
based on the system of pyrig-runtime subclasses.
Documentation
Please refer to the official documentation for more details and examples.
Metadata
Release files for pyorder 0.13.2
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-0.13.2.tar.gz | 13.7 kB | Without provenance | Details for pyorder-0.13.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyorder-0.13.2-py3-none-any.whl | Python 3 | none | any | Details for pyorder-0.13.2-py3-none-any.whl |
Total release size: 28.7 kB
Release files / pyorder-0.13.2.tar.gz
| Download URL | pyorder-0.13.2.tar.gz |
|---|---|
| Size | 13.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d18373fe79f4d87d6c75b53e29ad249b968086317b0a9aafe4cc4c89dc70bc94
|
|
BLAKE2b-256 checksum How to use checksums |
e7e6408062fec189e1c82ef3f3c42f865db27a1f259a03957c4bc1b730c7def6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","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-0.13.2-py3-none-any.whl
| Download URL | pyorder-0.13.2-py3-none-any.whl |
|---|---|
| Size | 15.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fe5ba7cae5126b08d2862d32ecf0dd6a293a1556bb1d5510f8f01365d987967d
|
|
BLAKE2b-256 checksum How to use checksums |
a324bb9b343e06ad64a759e83130cc152b715b6cb0bc0aff0f933871312d5f0a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","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 0.13.2
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.