Skip to content
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

All-whitespace files do not get their newlines removed #2382

Closed
JelleZijlstra opened this issue Jul 19, 2021 · 3 comments · Fixed by #3348
Closed

All-whitespace files do not get their newlines removed #2382

JelleZijlstra opened this issue Jul 19, 2021 · 3 comments · Fixed by #3348
Labels
T: bug Something isn't working

Comments

@JelleZijlstra
Copy link
Collaborator

From @akaihola in #779:

In akaihola/darker#164 (comment) we noticed that format_str() gives different results for some files than running black from the command line or black.main(..., standalone_mode=False). This should probably be fixed before making format_str() an officially public Python API.

Consider this test script compare_black.py:

import io
import sys
from contextlib import redirect_stdout
from subprocess import PIPE, check_output

from black import FileMode, diff, format_str, main

path = sys.argv[1]

print("============================= format_str =============================")
with open(path) as f:
    content = f.read()
format_str_result = format_str(content, mode=FileMode())
print(diff(content, format_str_result, path, path))

print("================================ main ================================")
with redirect_stdout(io.TextIOWrapper(io.BytesIO())) as main_stdout:
    main(["--quiet", "--diff", path], standalone_mode=False)
main_stdout.seek(0)
print(main_stdout.read())

print("============================= subprocess =============================")
subprocess_result = check_output(["black", "--quiet", "--diff", path], encoding="UTF-8")
print(subprocess_result)

If you create a valid Python source code file with just a few newlines:

echo "



" >newlines.py

and reformat it using the above script:

python compare_black.py newlines.py                   
============================= format_str =============================
--- /tmp/myfile.py
+++ /tmp/myfile.py
@@ -1,5 +0,0 @@
-
-
-
-
-

================================ main ================================

============================= subprocess =============================

You see that format_str() removes those blank lines while main() and black leave the file untouched.

I haven't tried to found other cases where these outputs don't match. I wonder if it only happens to all-whitespace files. Edit: Yes, only with all-whitespace files.

Originally posted by @akaihola in #779 (comment)

@JelleZijlstra JelleZijlstra added the T: bug Something isn't working label Jul 19, 2021
@akaihola
Copy link

All-whitespace files do not get their newlines removed

So @JelleZijlstra the expected behavior is for all-whitespace files to be truncated instead of being left unmodified? Makes sense.

So it's actually format_str() which does the right thing, while the black command line, black.main(..., standalone_mode=False) and black.format_file_contents() are wrong since they don't reformat all-whitespace files.

@JelleZijlstra
Copy link
Collaborator Author

I'm open to persuasion otherwise, but it makes sense to me to truncate such files to either empty or a single newline. I suppose it doesn't make a huge difference in practice.

@septatrix
Copy link

I'm open to persuasion otherwise, but it makes sense to me to truncate such files to either empty or a single newline. I suppose it doesn't make a huge difference in practice.

I think a single newline is the right choice here given that under unix a trailing newline indicates text files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants