astral-sh/ruff · error
Own line docstring must have indentation
Error message
Own line docstring must have indentation
What it means
An internal expectation in the pydocstyle D-z blank-before/after-class fixer: while rebuilding the fix, a docstring that sits on its own line was found with no indentation to work with. The rule only fires on class docstrings that are indented, so this indicates the fixer's assumptions about the located line are broken.
Source
Thrown at crates/ruff_linter/src/rules/pydocstyle/rules/blank_before_after_class.rs:255
// If the class is empty except for comments, we don't need to insert a newline between
// docstring and no content
let all_blank_after = lines.clone().all(|line| {
line.trim_whitespace().is_empty() || line.trim_whitespace_start().starts_with('#')
});
if all_blank_after {
return;
}
let first_line = lines.next();
let mut replacement_start = first_line.as_ref().map(Line::start).unwrap_or_default();
// Edge case: There is trailing end-of-line content after the docstring, either a statement
// separated by a semicolon or a comment.
if let Some(first_line) = &first_line {
let trailing = first_line.as_str().trim_whitespace_start();
if let Some(next_statement) = trailing.strip_prefix(';') {
let indentation = indentation_at_offset(docstring.start(), checker.source())
.expect("Own line docstring must have indentation");
let mut diagnostic =
checker.report_diagnostic(IncorrectBlankLineAfterClass, docstring.range());
let line_ending = checker.stylist().line_ending().as_str();
// We have to trim the whitespace twice, once before the semicolon above and
// once after the semicolon here, or we get invalid indents:
// ```rust
// class Priority:
// """Has priorities""" ; priorities=1
// ```
let next_statement = next_statement.trim_whitespace_start();
diagnostic.set_fix(Fix::safe_edit(Edit::replacement(
line_ending.to_string() + line_ending + indentation + next_statement,
replacement_start,
first_line.end(),
)));
return;
} else if trailing.starts_with('#') {View on GitHub (pinned to 26f38c119c)
Solutions
- Verify the line-location logic used to compute the docstring's indentation
- Skip the autofix when the own-line docstring has no indentation
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at crates/ruff_linter/src/rules/pydocstyle/rules/blank_before_after_class.rs:255 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/dc7eed269807ce1d.
Report an issue: GitHub.