ruff
ruff copied to clipboard
Fix for `D413` introduces whitespace
Minimal test.py
:
def test_func():
"""Some test function.
Returns
-------
None
"""
pass
Fixing with ruff --isolated --select="D413" --fix test.py
adds unnecessary whitespace (W293
)
--- a/test.py
+++ b/test.py
@@ -4,5 +4,6 @@ def test_func():
Returns
-------
None
+
"""
pass
I took a quick look at this and the culprit seems to be here: https://github.com/astral-sh/ruff/blob/175c266de30c159b69308346cb84e51134ac529e/crates/ruff_linter/src/rules/pydocstyle/rules/sections.rs#L1667-L1683
Instead of the fix "introducing" whitespace it is more so leaving behind the original indentation space which obviously violates (W293). In this scenario I would imagine the original indentation will need to be deleted before inserting the new line and new indentation. Below is an initial attempt at addressing this problem. Let me know if this is a good solution and I would be glad to submit a pull request.
let fix = match num_blank_lines {
0 => Fix::safe_edit(Edit::insertion(
format!("{}{}", line_end.repeat(2), docstring.indentation),
context.end(),
)),
1 => {
let del_start = context.end() - TextSize::from(docstring.indentation.len() as u32);
let rest = [Edit::insertion(
format!("{}{}", line_end, docstring.indentation),
context.end(),
)];
Fix::safe_edits(Edit::deletion(del_start, context.end()), rest)
}
_ => unreachable!(),
};
diagnostic.set_fix(fix);
@jusexton this looks about right, although it would be nice if we don't have to branch on the existing empty lines. I also think that this won't work if the line has more whitespace than just the indent?
def test_func():
"""Some test function.
Returns
-------
None
"""
pass
Could we maybe use the range information in context.following_lines()
to determine the offset?
@MichaReiser Thanks for the help. A PR has been created making use of context.following_lines()
to determine the deletion length instead of docstring.indentation
. I also attempted removing all branching but a check still seems to be required around whether the last line is whitespace or not. Let me know if there is a better way!