astral-sh/ruff · error
We shouldn't be attempting an autofix if a sequence has < 2
Error message
We shouldn't be attempting an autofix if a sequence has < 2 elements; a sequence with 1 or 0 elements cannot be unsorted.
What it means
A deliberate panic in into_sorted_source_code: the sorter was asked to sort a literal sequence with fewer than two items. Sequences of length 0 or 1 cannot be unsorted, so the caller violated the method's documented precondition — a logic error in the rule that detected the sequence as unsorted.
Source
Thrown at crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs:448
})
}
/// Sort a multiline sequence of literal strings
/// that is known to be unsorted.
///
/// This function panics if it is called and `self.items`
/// has length < 2. It's redundant to call this method in this case,
/// since lists with < 2 items cannot be unsorted,
/// so this is a logic error.
pub(super) fn into_sorted_source_code(
mut self,
sorting_style: SortingStyle,
locator: &Locator,
stylist: &Stylist,
) -> String {
let (first_item_start, last_item_end) = match self.items.as_slice() {
[first_item, .., last_item] => (first_item.start(), last_item.end()),
_ => panic!(
"We shouldn't be attempting an autofix if a sequence has < 2 elements;
a sequence with 1 or 0 elements cannot be unsorted."
),
};
// As well as the "items" in a multiline string sequence,
// there is also a "prelude" and a "postlude":
// - Prelude == the region of source code from the opening parenthesis,
// up to the start of the first item in `__all__`/`__slots__`/etc.
// - Postlude == the region of source code from the end of the last
// item in `__all__`/`__slots__`/etc. up to and including the closing
// parenthesis.
//
// For example:
//
// ```python
// __all__ = [ # comment0
// # comment1View on GitHub (pinned to 26f38c119c)
Solutions
- Fix the caller to only request a fix when the sequence has at least two items
- Return an empty/unchanged result instead of panicking for short sequences
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs:448 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05).
Data as JSON: /api/errors/a15442c44fcae4ee.
Report an issue: GitHub.