Closed tugrulates closed 1 month ago
Thanks for the great write-up.
Unfortunately, fixing this will be difficult because the pycodestyle linter doesn't support parsing code blocks. This also requires introducing a new setting to be compatible with the formatter (or we pick up the setting from the formatter?)
pycodestyle.max-doc-code-line-length
that defaults to dynamic
(same as the format.docstring-code-length
setting) if left unspecifiedpycodestyle
linter and apply the dynamic or "fixed" length depending on the max-doc-code-line-length
setting. Ignore my previous comment. I'm wrong. The dynamic
setting actually ensures that this isn't a problem. Let me double check why the list isn't handled correctly
I added a few debug statements to the code to understand the values calculated in
[crates/ruff_python_formatter/src/string/docstring.rs:500:41] self.f.options().line_width().value() = 88
[crates/ruff_python_formatter/src/string/docstring.rs:501:36] self.f.options().indent_width() = IndentWidth(
4,
)
[crates/ruff_python_formatter/src/string/docstring.rs:502:36] self.f.context().indent_level() = IndentLevel {
level: 2,
}
[crates/ruff_python_formatter/src/string/docstring.rs:505:37] kind.extra_indent_ascii_spaces() = 4
[crates/ruff_python_formatter/src/string/docstring.rs:524:30] line_width = LineWidth(
80,
)
indent_level.to_ascii_spaces(indent_width)
reduces the indent level by 1. That makes me think that the problem here is that the formatter doesn't account for the indentation compared to Example
.
The new logic has to account for that the formatter normalizes the indentation when some lines are incorrectly indented. I think that's done in https://github.com/astral-sh/ruff/blob/138e70bd5c01d61061247c43b1bbb33d0290a18f/crates/ruff_python_formatter/src/string/docstring.rs#L380-L471
@tugrulates do you want to take a stab at this?
Certainly, I can work on this.
I think I have a fix for this
Thank you for the great tool. I am encountering an issue with the docstring code formatter in
dynamic
mode.When the doctest code has an indent that is more than the docstring indent, the dynamic calculation is off, and as a result, the formatter and the linter are in a disagreement.
I have searched the issue using "doctest", "docstring", "dynamic" keywords. #9126 is for the bug where this calculation if off for the prompt. The current issue is for when the prompt is extra indented.
I am happy to take a stab at this.