Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Pylint care about blank lines

I am not a stickler for most things pep-8, but certain things I personally prefer when writing code (as opposed to for work, where I would adhere to the style or lack thereof of the existing code base).

One thing I personally tend to follow is pep-8's suggestion about blank lines:

Surround top-level function and class definitions with two blank lines.

Method definitions inside a class are surrounded by a single blank line.

However, I have not been able to get Pylint to warn me when I violate this. I don't see anything that seems relevant disabled in my .pylintrc, and I have not been able to figure out if this is possible in Pylint, and if so, how to enable it.

Based on this answer, it looks like there are certain aspects of pep-8 that Pylint does not (or did not at the time) cover, but I have not been able to ascertain whether this is the case for blank lines.

Is it possible to have Pylint warn about blank lines (too many/not enough) without writing custom extensions?

like image 926
elethan Avatar asked Nov 10 '16 03:11

elethan


People also ask

Do blank lines matter in Python programs?

The empty lines are there for better visibility when reading or writing code. This does not add to or take from the program as empty lines are ignored.

Are Blank lines forbidden in Python?

Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations). Use blank lines in functions, sparingly, to indicate logical sections. Python accepts the control-L (i.e.

How many blank lines does the pep8 Standard recommend to use between top-level function definitions within a Python module?

Two Blank Lines Import Statements While many online sources state that there should be two blank lines after the import statements before the code starts, this is not generally correct. PEP 8 only states that top-level function or class definitions should be surrounded by two blank lines!

How do you use blank lines in your code?

Blank lines improve readability by setting off sections of code that are logically related. Two blank lines should always be used in the following circumstances: Between sections of a source file. Between class and interface definitions.


1 Answers

As mentioned in this other answer, E301 and E303 doesn't seem to be a thing in pylint (yet?).

One alternative would be to use the pycodestyle (previously: pep8) tool directly, which would allow you to check for blank lines.

Hopefully you'll like it as much as pylint, despite maybe being a little bit less configurable.

like image 160
tbobm Avatar answered Oct 04 '22 21:10

tbobm