Python flake8 py错误地报告W391(文件末尾没有换行符)

问题描述 投票:0回答:1

W391说,文件末尾应该有一个(只有一个)空白行。但是,如果文件末尾至少有一个换行符,则flake8将报告错误:

$ 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

您可以在上面看到,实际上在文件末尾只有一个空白行(0a\n)。但是,当我运行flake8时,出现W391错误:

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

为什么?

python pep8 flake8 pyflakes
1个回答
0
投票

显然,vim会自动向每个文件添加换行符,这让我以为最后一个空白行不存在。随着时间的流逝,这个隐式换行符使我困惑,以为最后两个换行符创建了一个空行。

因此,警告是正确的。文件末尾应该只有一个\n

© www.soinside.com 2019 - 2024. All rights reserved.