Skip to content

Home

pyorder

CI CD ProjectTester SecurityPracticesChecker Best Practices Baseline

ByteOrderMarkerFormatter CICDLinter CICDSecurityChecker CaseConflictChecker CodeAnalyzer DeadCodeChecker DependencyChecker EndOfFileFormatter EndOfLineFormatter FuzzTester JSONFormatter JSONLinter LargeFileChecker MarkdownLinter MergeConflictChecker ModuleTestNamingChecker PythonLinter PythonOrderLinter SecretsChecker SecurityChecker ShellFormatter ShellLinter SpellChecker TOMLLinter TrailingWhitespaceFormatter TypeChecker XMLLinter YAMLLinter

PackageManager Pyrigger RemoteVersionController VersionControlHookManager VersionController

DocsBuilder PackageIndex ProgrammingLanguage License


A Python linter for ordering source code.


What pyorder does

pyorder is a Python source-code ordering linter and fixer. It parses Python files and reorders function, asynchronous function, and class definitions to make related code easier to scan. It requires Python 3.12 or newer.

Ordering is limited to direct definitions at the module level and direct definitions inside module-level classes. Nested functions, definitions inside control-flow blocks, and definitions inside nested classes are not independently ordered. Imports, assignments, calls, and other statements are not sorted.

Install and run

Install pyorder as a development dependency:

uv add pyorder --dev

Check files and print a diff for any files that need ordering:

uv run pyorder check src/package/module.py

Apply the proposed ordering:

uv run pyorder check --fix src/package/module.py

The check exits with status 1 when it finds any unordered file, including when --fix has rewritten it. Run the check again after fixing to confirm that all files pass. Review changes made by --fix: moving definitions can change runtime behavior, and the tool does not verify that the reordered program works.

pyorder check can also run as a check-only pre-commit hook. It reports files that need reordering; you can use --fix to automatically reorder them.

Ordering rules

The examples page shows before-and-after code for every rule.

How ordering works and its limits

The tool uses Python's abstract syntax tree (AST) to find definition names and references. It does not execute the file or attempt full name resolution. At module level, it looks for loaded names in each definition's syntax tree. In class bodies, method references are limited to attributes accessed through the method's first positional parameter (typically self or cls).

Definition-time dependencies are treated separately from ordinary references. Bases, decorators, default values, and statements in a class body can require a name to exist when a definition is created, so those dependencies are ordered before their users.

Only definitions move; calls and other statements keep their original positions. Reordering definitions around a module-level call or assignment can change whether a name exists when that statement executes. Likewise, the tool does not reorder imports or assignments to make them agree with the new definition order. --fix is a mechanical transformation, not a guarantee of valid runtime behavior.

Always review the changes made by --fix to ensure that the reordered program behaves as expected.

Customization

pyorder has no inline suppression comments such as # noqa. To customize its ordering rules, create a subclass of its Module ordering class in an installed package that depends on pyorder. The runtime discovers compatible subclasses automatically. See customizing with a plugin and the pyrig-runtime subclassing documentation.

Reference