Remove whitespaces of whitespace-only files by aaossa · Pull Request #3348 · psf/black (original) (raw)

@aaossa

Currently, empty and whitespace-only (with or without newlines) are not modified. In some discussions (issues and pull requests) consesus was to reformat whitespace-only files to empty or single-character files, preserving line endings when possible. With that said, this commit introduces the following behaviors:

To implement these changes, we moved the initial check at format_file_contents that raises NothingChanged if the source (with no whitespaces) is an empty string. In the case of *.ipynb files, format_ipynb_string checks a similar condition and removed whitespaces. In the case of Python files, format_str_once includes a check on the output that returns the correct newline character if possible or an empty string otherwise.

Signed-off-by: Antonio Ossa Guerra aaossa@uc.cl

@aaossa aaossa marked this pull request as draft

October 21, 2022 20:02

aaossa

@aaossa

This commit introduces two tests and modifies other two tests. The introduced tests verify the expected behaviour on files containing a single newline character via 1) format_str and 2) format_file_in_place. In the other hand, the first modified test (test_format_file_contents) verify that NothingChanged is raised on files containing only a single newline, and that whitespace-only files are properly formatted to a single newline character. The second modified test (test_reformat_one_with_stdin_empty) validates the expected behavior when the input is passed via stdin.

Before the fix introduced in the previous commit, these tests (and a couple of others) failed on cases covering whitespace-only files. Now, these tests are passed for all cases.

Signed-off-by: Antonio Ossa Guerra aaossa@uc.cl

@aaossa

Add entry about new behavior on whitespace-only files: removing whitespace characters and return a single newline (if present) or an empty file

Signed-off-by: Antonio Ossa Guerra aaossa@uc.cl

@aaossa aaossa marked this pull request as ready for review

October 24, 2022 20:06

JelleZijlstra

@aaossa

@aaossa aaossa marked this pull request as draft

October 26, 2022 17:23

@aaossa aaossa marked this pull request as ready for review

October 26, 2022 19:31

JelleZijlstra

ichard26

@aaossa

The new criteria to reformat empty and whitespace-only files should go into the preview style

Signed-off-by: Antonio Ossa Guerra aaossa@uc.cl

@aaossa

Adding comments on a non-intuitive behavior ar _format_str_once, and improving the test_one_empty_line test readability by using a pre-defined function that implements the same assertions

Signed-off-by: Antonio Ossa Guerra aaossa@uc.cl

@aaossa

ichard26

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})