Python flake8 py reporting W391 (no newline at end of file) incorrectly
Asked Answered
S

2

8

W391 says that there should be one (and only one) blank line at the end of file. However, flake8 reports the error when there is at least one newline at the end of the file:

$ cat /tmp/test.py
def hello():
    print('hello')


hello()


$ hexdump -C /tmp/test.py
00000000  64 65 66 20 68 65 6c 6c  6f 28 29 3a 0a 20 20 20  |def hello():.   |
00000010  20 70 72 69 6e 74 28 27  68 65 6c 6c 6f 27 29 0a  | print('hello').|
00000020  0a 0a 68 65 6c 6c 6f 28  29 0a 0a                 |..hello()..|
0000002b

You can see above there is in fact one and only one blank line at the end of the file (0a is \n). However, when I run flake8, I get the W391 error:

$ flake8 /tmp/test.py
/tmp/test.py:6:1: W391 blank line at end of file

Why is that?

Syblesybley answered 10/11, 2019 at 2:59 Comment(12)
There are two line breaks.Subadar
W391 implies two line break characters, assuming there's at least one other line. The last two line break characters will create one blank line at the end of the file.Syblesybley
And that is exactly what the message says.Subadar
But it's reporting it as an error. Read the spec -- The file has no errors according to spec and shouldn't fail linting.Syblesybley
The message is misleading. It should be two blank lines at the end of file or similar. Even in the specs is does not fit. You have two and it should be one.Subadar
No, there is ONE blank line at the end of the file. There is one newline character at the end of line 4, and a second newline character immediately following, creating ONE blank line on line 5.Syblesybley
Two line break characters at the end create two blank lines. One after both of them.Subadar
I think you're confused. Two newline characters creates ONE blank line.Syblesybley
No, you have not understood how line breaks work. Take a fresh editor, type some characters, hit Return twice and see how many empty lines there are below what you typed.Subadar
Well, this is embarrassing, but it appears I am indeed wrong. After trying another editor, I've discovered that my use of vim for over 10 years has brainwashed me into thinking two newlines meant one blank line. Apparently vim automatically adds a newline to every file, which fools me into thinking that last blank line isn't there. Apologies for my confusion and stubborn persistence!Syblesybley
What is this purpose of w391?Mcneill
@Mcneill it's specific to the flake8 style rules: flake8rules.com/rules/W391.htmlSyblesybley
S
8

Apparently vim automatically adds a newline to every file, which fools me into thinking that last blank line isn't there. Over time this implicit newline confused me into thinking two newline characters at the end created one blank line.

So, the warning is correct. There should be one and only one \n at the end of the file.

Syblesybley answered 10/11, 2019 at 19:12 Comment(0)
D
0

I got the same error below with Flake8:

blank line at end of fileFlake8(W391)

Because I added more than one blank lines after the last code print(math.pi) as shown below:

1 import math
2
3 print(math.pi)
4
5

So, I added only one blank line after the last code print(math.pi) as shown below, then the error was solved:

1 import math
2
3 print(math.pi)
4

In addition, if you don't add one blank line after the last code print(math.pi) as shown below:

1 import math
2
3 print(math.pi)

Then, you get the error below:

no newline at end of fileFlake8(W292)

Distillery answered 10/6, 2023 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.