How do I enable print statement catching in pylint?

Question:

I have a file p.py which has a single line:

print('hello')

I run pylint (2.6.0) with:

pylint p.py --enable=E

all I get is:

************* Module p
p.py:1:0: C0304: Final newline missing (missing-final-newline)
p.py:1:0: C0114: Missing module docstring (missing-module-docstring)

----------------------------------------------------------------------

Your code has been rated at -10.00/10 (previous run: -10.00/10, +0.00)

How do I get pylint to throw E1601?

Asked By: user947659

||

Answers:

To trigger this error you’d need to be running Python 2.x and use a print statement instead of the print function

Print statements were the only option in early releases of Python 2.x. It was only with the decision to replace them with print functions in Python 3, that print functions were added to Python 2.6.

# Print statement, only works in Python 2.x
print 'hello'

# Print function, Python >= 2.6
print('hello')

Pylint won’t emit E1601 when running Python >= 3.0. In Python 3 using a print statement isn’t a stylistic error it’s a syntax error. My best guess why E1601 exists is to encourage Python 2 developers to use Python 3 features when possible so that their code is more portable.

Answered By: sphennings

I know this doesn’t use pylint as the question asks, but I have used a simple bash one-liner in CI to catch calls to print():

prints=$(grep -rn 'print('); [[ -z $prints ]] && echo 'no prints' || printf "found prints:n$printsn"

In the || path, you could exit 1 to fail a CI run or whatever you need.

Answered By: tykom

The simplest approach for python3 is to use the combination of the builtins plugin and the bad-functions definition.

under [MASTER] section:

load-plugins=pylint.extensions.bad_builtin

and then under [BASIC]:

bad-functions=print,exec # also good to check for exec`

Answered By: David Merritt
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.