Ordering¶
funcsort.ordering ¶
Load-time dependency constraints over a block's statement order.
This is a pure module: it knows nothing about libcst, TOML or groups. It takes the order the grouping engine wants and returns an order that is also safe to execute.
The model exploits the shape the sorter already has. Statements that cannot be sorted --
imports, if/try blocks, nested classes -- are anchors pinned to their original
index, and the sortable candidates are poured into the remaining slots. So the
question is never "what is the best permutation of the body"; it is only "which candidate
goes in which slot", subject to:
- precedence -- a candidate that defines a name another candidate reads at load time must take an earlier slot;
- release times -- a candidate reading a name an anchor defines must land after it;
- deadlines -- a candidate defining a name an anchor reads must land before it.
OrderingOutcome ¶
Bases: StrEnum
How the desired order fared against the block's dependency constraints.
UNCONSTRAINED
class-attribute
instance-attribute
¶
UNCONSTRAINED = 'unconstrained'
The desired order was already safe and is emitted verbatim.
REPAIRED
class-attribute
instance-attribute
¶
REPAIRED = 'repaired'
The desired order was unsafe; a different, safe order was found.
INFEASIBLE
class-attribute
instance-attribute
¶
INFEASIBLE = 'infeasible'
No safe order was found; the caller must keep the block as it was.
Statement
dataclass
¶
Statement(index, provides=frozenset(), requires=frozenset())
A statement's position within its block body, and its load-time name flow.
requires
class-attribute
instance-attribute
¶
requires = frozenset()
Names read while the statement executes -- a decorator expression or a parameter default, never a function body, which runs long after the module is loaded.
OrderingProblem
dataclass
¶
OrderingProblem(anchors, candidates, slots, desired)
OrderingResult
dataclass
¶
OrderingResult(order, outcome)
_Constraints
dataclass
¶
_Constraints(predecessors, release, deadline, pinned, slot_of=dict())
Derived scheduling constraints, all invariant under candidate permutation.
solve_order ¶
solve_order(problem)
Fit the desired order to the block's load-time dependencies.
On :attr:OrderingOutcome.INFEASIBLE the returned order is the block's original one,
which is always safe because the input file already runs.
_derive ¶
_derive(problem)
Build the precedence edges, release times, deadlines and pins for a block.
_providers ¶
_providers(candidates)
Map every name a candidate binds to the candidates that bind it, in body order.
_pinned ¶
_pinned(providers, anchor_provides)
Return the candidates whose bindings are too ambiguous to express as an edge.
A name bound more than once makes "the provider comes first" ambiguous: which binding a reader sees depends on order, so no single edge expresses it. Every candidate involved is pinned to its own slot instead. Rare, and conservative.
_predecessors ¶
_predecessors(candidate, providers)
Return the candidates that must take an earlier slot than candidate.
Only unambiguously bound names produce an edge; the rest are handled by :func:_pinned.
_satisfies ¶
_satisfies(order, slots, constraints)
Return whether order places every candidate in a legal slot.
_repair ¶
_repair(problem, constraints)
Schedule candidates into slots greedily, or return None if the greedy stalls.
Slots are filled in ascending order. Among the candidates that are legal now, the
one with the earliest deadline wins, falling back to the desired order. When no
deadlines exist -- the ordinary case -- every key is (inf, rank), so this
degenerates to pure desired order and output quality is untouched.
_successors ¶
_successors(predecessors)
Invert the precedence edges, so releasing a candidate is a single lookup.
_is_eligible ¶
_is_eligible(index, constraints, unmet, *, position, body_index)
Return whether a candidate may legally take the slot at position.