Reordering examples¶
Each pair shows the source before ordering and the result of
pyorder check --fix example.py. Examples isolate a rule where possible; in
real files, the first rule that differs takes precedence. The rule sections
within each scope are listed in priority order.
Rule priority¶
For any two definitions, the first rule that ranks them differently decides their order. Later rules only break ties. For example, callers-before-callees comes before functions-before-classes, so a class that calls a module-level function goes before that function:
Before:
def class_utility():
return 1
def another_utility():
return 2
class Example:
def run(self):
return class_utility()
After:
def another_utility():
return 2
class Example:
def run(self):
return class_utility()
def class_utility():
return 1
Module-level definitions¶
Callers before callees¶
A function that calls another same-module function is placed before it:
Before:
After:
Definition-time dependencies go the other way: a base class must be defined
before the class that inherits from it. The source before ordering is valid
Python syntax, but raises NameError when executed because Parent is not
yet defined.
Before:
After:
Functions before classes¶
When references and dependencies do not decide the order, module-level functions precede classes:
Before:
After:
Public before private¶
Names with fewer leading underscores come first:
Before:
After:
Alphabetical order¶
Names at the same earlier priority levels are ordered alphabetically:
Before:
After:
Definitions in classes¶
Classes before methods¶
Direct nested classes precede methods:
Before:
After:
Constructors first¶
The constructor hooks come first in the order they become relevant in Python's class and instance creation process.
Before:
class Example:
def run(self):
pass
def __init__(self):
pass
def __new__(cls):
return super().__new__(cls)
def __init_subclass__(cls):
pass
After:
class Example:
def __init_subclass__(cls):
pass
def __new__(cls):
return super().__new__(cls)
def __init__(self):
pass
def run(self):
pass
Abstract methods first¶
Methods decorated with @abstractmethod precede ordinary methods:
Before:
from abc import ABC, abstractmethod
class Example(ABC):
def run(self):
pass
@abstractmethod
def required(self):
pass
After:
from abc import ABC, abstractmethod
class Example(ABC):
@abstractmethod
def required(self):
pass
def run(self):
pass
Overrides first¶
Methods decorated with @override precede methods that are not marked as
overrides:
Before:
from typing import override
class Base:
def inherited_method(self):
pass
class Example(Base):
def run(self):
pass
@override
def inherited_method(self):
pass
After:
from typing import override
class Base:
def inherited_method(self):
pass
class Example(Base):
@override
def inherited_method(self):
pass
def run(self):
pass
Special methods first¶
Dunder-named methods precede ordinary methods:
Before:
After:
Class methods first¶
Methods decorated with @classmethod precede other methods:
Before:
After:
Callers before callees¶
For methods, the tool uses attribute reads through the first parameter (usually
self or cls) to find references:
Before:
After:
Public before private¶
This visibility rule also applies to methods and nested classes:
Before:
After:
Alphabetical order¶
Methods at the same earlier priority levels are ordered alphabetically:
Before:
After:
Edge cases¶
Annotations are not runtime dependencies¶
Annotations are not considered definition-time dependencies because quoting them
or using from __future__ import annotations prevents them from being evaluated
at definition time. Unquoted annotations still count as references.
Before:
After:
Quoting the annotation as "alpha" removes this reference from the AST, so
alphabetical ordering would place alpha first instead.
Non-definition statements stay in place¶
Calls are not reordered, even when definitions move around them. In this
example, ordering moves zebra below the module-level call to it. The result
is valid syntax but raises NameError when executed:
Before:
After:
Review fixed files and run your normal tests; --fix does not verify runtime
behavior. It merely switches definitions around to order them according to the rules.