Skip to content

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

@JelleZijlstra

Description

@JelleZijlstra

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions