-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ruff format removes trailing whitespace in doctests #10275
Comments
That makes sense to me and we do have the infrastructure to support when using |
I took a couple minutes to look, but it's not obvious to me where the issue is. One thing that I'd wonder about is, if we're reformatting docstring code snippets and reformatting itself will strip whitespace, then I'm not sure we can preserve trailing whitespace while Otherwise, when |
Oh yeah, that could be it, depending on how we print the trailing space. The Printer doesn't print any whitespace. It defers it until you print the next (non whitespace) character. Although that only applies for Edit: Nope, not it. You can see how the trailing whitespace is missing in the written IR after the |
@BurntSushi I believe it's coming from this ruff/crates/ruff_python_formatter/src/string/docstring.rs Lines 382 to 392 in fe79798
We would probably need a way for the |
I investigated this a little more. We would need to extend our doctest formatting also to handle the expected output and print that as verbatim instead of using Links:
|
Testing under Linux, using
test_case.py
based on a real world example:test_case.py.txt
Using the current version of black, the module docstring is unchanged (good), but the function doctest is broken (bad):
This seems to be a long standing bug in black psf/black#1654 although the difference in module level versus function level docstrings might be new.
However with ruff, both the module docstring and the function docstring are broken (two doctests fail):
I agree that it is better to avoid meaningful trailing white space, but it can result in some ugly workarounds (assuming the code cannot be changed due to backward compatibility).
Indeed the GitHub editor remove the trailing spaces too when pasting in the code! I have restored them by hand.
I would like both ruff format and black not to touch trailing whitespace in the output section of a doctest.
Separately flake8 or ruff lint can warn about W291, and the real examples this is based on use
# noqa: W291
on the docstring.The text was updated successfully, but these errors were encountered: