-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Remove whitespaces of whitespace-only files #3348
Conversation
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: * Empty files are left as is * Whitespace-only files (no newline) reformat into empty files * Whitespace-only files (1 or more newlines) reformat into a single newline character 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>
4f704bd
to
1a409b2
Compare
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>
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>
Hey! I just wanted to say thank you for submitting a PR. I know us maintainers haven't been very active reviewing PRs lately and that's disheartening. I speak for the rest of the maintainership team that we really appreciate all of your work. I promise I've seen your PRs (this one, #3338 and #3336) and care about them. I've been really busy lately and finding time (and the energy) to do reviews has been tough. I'll try my best to find time this week. Thanks again. |
Hey @ichard26, I understand that maintaining such an important project for many people requires many hours, so no worries. I must say thank you to all the maintainership team for your work. I'll try to keep doing my part by contributing when I have the time (but no more PRs for now, don't worry 😅). Have a great week! |
Could you take a look at the merge conflict? |
CHANGES.md
Outdated
@@ -29,6 +29,8 @@ | |||
- Parsing support has been added for walruses inside generator expression that are | |||
passed as function args (for example, | |||
`any(match := my_re.match(text) for text in texts)`) (#3327). | |||
- Reformat empty and whitespace-only files as either an empty file (if no newline is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to go into the preview style (not just in the changelog, but also in the code).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be added in the future style page too?
This PR is now ready for review: conflicts with main were solved, and the changes were moved into the preview style 👌 |
Any updates on this PR? Seems to be ready and is already approved |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine in general, I don't like how poorly the tests read, but that's more a symptom of the poor test setup and infrastructure (ie. it's not your fault).
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>
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>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much!
Description
Closes #2484
Closes #2382
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 PR introduces the following behaviors:
To implement these changes, we moved the initial check at
format_file_contents
that raisesNothingChanged
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.Checklist - did you ...
CHANGES.md
if necessary?