Skip to content

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.

_NO_RELEASE module-attribute

_NO_RELEASE = -1

_NO_DEADLINE module-attribute

_NO_DEADLINE = float('inf')

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.

index instance-attribute

index

provides class-attribute instance-attribute

provides = frozenset()

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)

A block's candidates, its fixed anchors, and the order the groups want.

anchors instance-attribute

anchors

Statements pinned to their own index; they never move.

candidates instance-attribute

candidates

slots instance-attribute

slots

Ascending body indices the candidates may occupy -- the anchors' complement.

desired instance-attribute

desired

with_desired

with_desired(desired)

Return a copy of this problem with a different preferred order.

OrderingResult dataclass

OrderingResult(order, outcome)

The order to emit, and how it was arrived at.

order instance-attribute

order

Candidate indices in the order they should fill :attr:OrderingProblem.slots.

outcome instance-attribute

outcome

is_safe property

is_safe

Return whether :attr:order satisfies the block's dependencies.

_Constraints dataclass

_Constraints(predecessors, release, deadline, pinned, slot_of=dict())

Derived scheduling constraints, all invariant under candidate permutation.

predecessors instance-attribute

predecessors

release instance-attribute

release

deadline instance-attribute

deadline

pinned instance-attribute

pinned

slot_of class-attribute instance-attribute

slot_of = field(default_factory=dict)

is_trivial property

is_trivial

Return whether nothing constrains the order (the common case).

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.

_release

_release(candidate, anchors)

Return the body index below which candidate may not sink.

_deadline

_deadline(candidate, anchors)

Return the body index above which candidate may not rise.

_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.

_union

_union(sets)

Return the union of an iterable of frozensets.