11

I am new to Pylint, and when I run it against my script, I get this output:

C: 50, 0: Trailing newlines (trailing-newlines)

Here, Pylint is saying that it is bad to have a final newline.

I like to have a new line at the end of my scripts, so I thought I would disable this warning. I did some google web searching and found this: http://pylint-messages.wikidot.com/messages:c0304

C0304 Message

Final newline missing

Description

Used when a Python source file has no line end character(s) on its last line.

This message belongs to the format checker. Explanation

While Python interpreters typically do not require line end character(s) on the last line, other programs processing Python source files may do, and it is simply good practice to have it. This is confirmed in Python docs: Line Structure which states that a physical line is ended by the respective line end character(s) of the platform.

Here, Pylint is saying that it is bad to miss the final newline.

(A) What is the correct view ? (B) How do I disable the check for the final new line ?

{{ EDIT : It turns out that this is not an issue with Pylint ; It is an issue with vim , which adds eol automatically : How to stop vim from adding a newline at end of file? }}

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Prem
  • 460
  • 1
  • 7
  • 15

3 Answers3

14

The pylint warning you are getting is complaining that you have multiple trailing newlines. The C0304 message occurs when there is no trailing newline at all.

These messages are not contradictory they indicate different problems.

The reason you need at least one newline is that historically some tools have problems if the file ends and the last line has text on it but does not have a newline at the end of the file. Badly written tools can miss processing that last partial line, or even worse can read random memory beyond the last line (though that is unlikely to happen with tools written in Python it can happen with tools written in C).

So you must ensure there is a newline terminating the last non-blank line.

But you don't want any completely blank lines at the end of the file either. They won't actually produce bugs but they're untidy. So delete any blank lines and you should be fine.

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • 2
    I have exactly one newline (pressed enter key on the last line of code) and I get "trailing newline" warning. I remove this (using dd in vim) and the warning is gone. I do not have multiple newlines. – Prem Apr 19 '17 at 10:12
  • 8
    While I was sure that I had only one newline, two users ( @mastro35 and Duncan ) referred to the case of multiple newlines, hence I decided to check with **hexdump -C** and there were indeed **two newlines**. How ? It turns out that **vim is the culprit** : http://stackoverflow.com/questions/1050640/vim-disable-automatic-newline-at-end-of-file ; Pylint was correct. Duncan and mastro35, thanks ! – Prem Apr 19 '17 at 10:42
  • 1
    @Prem thanks for tracking down this maddening vim related issue. i was trying to get no `pylint` issues and i only had one newline... but i kept getting `pylint` issues. i ended up disabling the `trailing-newline` `pylint` issue. – Trevor Boyd Smith Oct 05 '18 at 16:29
  • Thanks @Prem for the link suggesting vim is the culprit, but that solution somehow does not solve the pylint warning for me. So I'd have to disable it from pylint. – RayLuo Apr 03 '20 at 22:52
2

C0304 Final newline missing is the error occures when a Python source file has no line end character(s) on its last line.

I am telling you the way to disables the pylint warring.

To disable the warning you can simple add the below line in your .py file, generally good practice to add before the imports.

# disabling:
# C0304: Trailing newlines (trailing-newlines)
# pylint: disable=C0304

OR You can create a configuration file ~/.pylintrc this allows you to ignore warnings you don't care about.

Community
  • 1
  • 1
Surajano
  • 2,688
  • 13
  • 25
  • 1
    While this may be correct, I do not want to ignore the "Final newline missing" issue. I want to ignore the "Final newline existing" issue. I want to have a newline at the end of the script, but Pylint is complaining. – Prem Apr 19 '17 at 10:17
  • A Python program is divided into a number of logical lines, and it is the way of the implementation of Pylint to check the logical lines. The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules. – Surajano Apr 19 '17 at 10:23
  • try `disable=C0305` – WickedJargon Dec 07 '20 at 16:26
0

I got the same error below with Pylint:

Trailing newlinesPylint(C0305:trailing-newlines)

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:

Final newline missingPylint(C0304:missing-final-newline)

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129