Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Newline is added to empty files causing PEP8 warning W391 #141

Open
johaag opened this issue Apr 11, 2024 · 6 comments
Open

Newline is added to empty files causing PEP8 warning W391 #141

johaag opened this issue Apr 11, 2024 · 6 comments

Comments

@johaag
Copy link

johaag commented Apr 11, 2024

Problem

When the yapf extension is used together with format on save, it is impossible to create empty __init__.py files. They will always contain a newline and hence cause Pylint convention message trailing-newlines / C0305 or pycodestyle (PEP8) warning W391 (blank line at end of file).

How to reproduce

  • Install yapf extension to VS Code, activate the extension.
  • Disable trim final newlines: "files.trimFinalNewlines": false
    (this setting can be used as a workaround)
  • Activate format on save: "editor.formatOnSave": false
  • Create an empty file e.g. __init__.py, observe that a removed newline will be added again.

Behavior of yapf itself

While autopep8 would remove the newline from the otherwise empty file, yapf will not. yapf will only remove extra newlines and leave the file with a single newline. However, if the file is empty, yapf will also not add the newline if used manually. For some reason, the last part is not true for the yapf extension. Here a newline will always be added to an otherwise empty file.

Workaround

Activate VS Code setting to trim final newlines: "files.trimFinalNewlines": true

@EeyoreLee
Copy link
Owner

EeyoreLee commented Apr 12, 2024

@johaag - Thanks for the such a detailed report. The W391 of PEP8 says "There should be one, and only one, blank line at the end of each file. This warning will occur when there are zero, two, or more than two blank lines." and yapf follow it to enable both empty and non-empty files to maintain a newline at the end.

@johaag
Copy link
Author

johaag commented Apr 12, 2024

@EeyoreLee - Thank you for looking into this. I am aware of the specification, but the observed behavior is somewhat consistently different:

Create file with a newline, otherwise empty:

$ echo "" > test.py
$ cat test.py

$ cat test.py | wc -l
1

Run checks on this file:

$ pycodestyle test.py 
test.py:1:1: W391 blank line at end of file
$ flake8 test.py
test.py:1:1: W391 blank line at end of file
$ pylint test.py
************* Module test
test.py:1:0: C0305: Trailing newlines (trailing-newlines)

All linters show the same warning.

Try to fix it with yapf:

$ yapf --style=pep8 -i test.py
$ cat test.py | wc -l
1
$ pycodestyle test.py 
test.py:1:1: W391 blank line at end of file
$ flake8 test.py
test.py:1:1: W391 blank line at end of file
$ pylint test.py
************* Module test
test.py:1:0: C0305: Trailing newlines (trailing-newlines)

Fix with yapf is not successful, because yapf does not remove the single newline. It would remove further newlines, but always keep 1 newline, if 1 or more newlines were present in an otherwise empty file.

Try to fix it with black:

$ black test.py 
All done! ✨ 🍰 ✨
1 file left unchanged.
$ cat test.py | wc -l
1

black behaves same as yapf.

Try to fix it with autopep8:

$ autopep8 -i test.py
$ cat test.py | wc -l
0
$ pycodestyle test.py 
$ flake8 test.py
$ pylint test.py

File is now empty (without a newline) and "fixed" (no warning anymore).

Check behavior of yapf and black on an empty file:

$ yapf --style=pep8 -i test.py
$ cat test.py | wc -l
0
$ black test.py
All done! ✨ 🍰 ✨
1 file left unchanged.
$ cat test.py | wc -l
0
$

Both yapf and black do not add a newline to an empty file.

Now the questions is: If yapf itself is not adding the newline to an empty file, why is the VS Code extension doing so?

Edit: Just now, I might have found the answer: If your extension passes the code via STDIN to yapf, then it is added by yapf as google/yapf#195 suggests.

@EeyoreLee
Copy link
Owner

EeyoreLee commented Apr 15, 2024

@johaag - true, this extension formats code through <stdin> in line google/yapf#L114, so there's always added a \n. But I'm confused why a file with a single newline throw a warning W391. This should be in line with the description of W391.

@EeyoreLee
Copy link
Owner

Same situation occur for me after updating vscode. Vscode changed something. I think it's a bug and I'll fix it later.

@EeyoreLee EeyoreLee added the bug Something isn't working label Jul 17, 2024
@EeyoreLee
Copy link
Owner

Whether an empty file should contain a newline character is worth further discussion. Once it's determined, I will provide the corresponding support. pycodestyle#1273

@EeyoreLee EeyoreLee removed the bug Something isn't working label Dec 27, 2024
@EeyoreLee
Copy link
Owner

Same situation occur for me after updating vscode. Vscode changed something. I think it's a bug and I'll fix it later.

It's just vscode view. A file has only "\n" shows as two line.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants