-
Notifications
You must be signed in to change notification settings - Fork 5
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
Comments
@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. |
@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 --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 Try to fix it with $ black test.py
All done! ✨ 🍰 ✨
1 file left unchanged.
$ cat test.py | wc -l
1
Try to fix it with $ 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 --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 Now the questions is: If Edit: Just now, I might have found the answer: If your extension passes the code via STDIN to |
@johaag - true, this extension formats code through |
Same situation occur for me after updating vscode. Vscode changed something. I think it's a bug and I'll fix it later. |
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 |
It's just vscode view. A file has only "\n" shows as two line. |
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
"files.trimFinalNewlines": false
(this setting can be used as a workaround)
"editor.formatOnSave": false
__init__.py
, observe that a removed newline will be added again.Behavior of
yapf
itselfWhile
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 theyapf
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
The text was updated successfully, but these errors were encountered: