28

Pylint looks like a good tool for running analysis of Python code.

However, our main objective is to catch any potential bugs and not coding conventions. Enabling all Pylint checks seems to generate a lot of noise. What is the set of Pylint features you use and is effective?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amit
  • 10,612
  • 11
  • 61
  • 60

5 Answers5

25

You can block any warnings/errors you don't like, via:

pylint --disable=error1,error2

I've blocked the following (description from http://www.logilab.org/card/pylintfeatures):

W0511: Used when a warning note as FIXME or XXX is detected

W0142: Used * or * magic*. Used when a function or method is called using *args or **kwargs to dispatch arguments. This doesn't improve readability and should be used with care.

W0141: Used builtin function %r. Used when a black listed builtin function is used (see the bad-function option). Usual black listed functions are the ones like map, or filter, where Python offers now some cleaner alternative like list comprehension.

R0912: Too many branches (%s/%s). Used when a function or method has too many branches, making it hard to follow.

R0913: Too many arguments (%s/%s). Used when a function or method takes too many arguments.

R0914: Too many local variables (%s/%s). Used when a function or method has too many local variables.

R0903: Too few public methods (%s/%s). Used when class has too few public methods, so be sure it's really worth it.

W0212: Access to a protected member %s of a client class. Used when a protected member (i.e. class member with a name beginning with an underscore) is access outside the class or a descendant of the class where it's defined.

W0312: Found indentation with %ss instead of %ss. Used when there are some mixed tabs and spaces in a module.

C0111: Missing docstring. Used when a module, function, class or method has no docstring. Some special methods like __init__ don't necessarily require a docstring.

C0103: Invalid name "%s" (should match %s). Used when the name doesn't match the regular expression associated to its type (constant, variable, class...).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
muckabout
  • 1,923
  • 1
  • 19
  • 31
  • 3
    The description of disabling particular types of message is correct, but I believe some of the specific choices of suppressed errors are controversial. Stuff like "Too many local variables/branches" is a huge red flag. – Jonathan Hartley Jan 28 '14 at 15:33
  • 1
    The `-d, --disable` option is not a python list with `[]` characters; it is just a comma-delimited list. So not `--disable=[C0111,R0912]`; use `--disable=C0111,R0912`. – DrStrangepork Aug 30 '17 at 18:02
  • The link is (effectively) broken: *"No result matching query"* – Peter Mortensen Jan 16 '22 at 15:08
13

To persistently disable warnings and conventions:

  1. Create a ~/.pylintrc file by running pylint --generate-rcfile > ~/.pylintrc
  2. Edit ~/.pylintrc
  3. Uncomment disable= and change that line to disable=W,C
Jack Kelly
  • 2,214
  • 2
  • 22
  • 32
9

Pyflakes should serve your purpose well.

bbigras
  • 1,311
  • 2
  • 17
  • 32
user225312
  • 126,773
  • 69
  • 172
  • 181
  • What does it have to do with Pylint? Can you elaborate? Please respond by [editing your answer](https://https://stackoverflow.com/posts/4395913/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jan 16 '22 at 15:04
7

-E will only flag what Pylint thinks is an error (i.e., no warnings, no conventions, etc.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gurney alex
  • 13,247
  • 4
  • 43
  • 57
1

Using grep like:

pylint my_file.py | grep -v "^C"

Edit : As mentionned in the question, to remove the conventions advices from pylint output, you remove the lines that start with an uppercase C.

From the doc of pylint, the output consists in lines that fit the format

MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE

and the message type can be:

  • [R]efactor for a “good practice” metric violation
  • [C]onvention for coding standard violation
  • [W]arning for stylistic problems, or minor programming issues
  • [E]rror for important programming issues (i.e. most probably bug)
  • [F]atal for errors which prevented further processing

Only the first letter is displayed, so you can play with grep to select/remove the level of message type you want.

I didn't use Pylint recently, but I would probably use a parameter inside Pylint to do so.

jumpy
  • 128
  • 6
  • What is it supposed to filter out? Can you provide an example? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/38899514/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jan 14 '22 at 20:05
  • Filter out "C" for *"convention"*? The others are *"refactoring"*, *"warnings"*, *"errors (probable bugs)"*, and *"fatal"* (?). – Peter Mortensen Jan 15 '22 at 00:33