开发者

Pylint best practices

开发者 https://www.devze.com 2023-01-29 09:17 出处:网络
Pylint looks like a goo开发者_StackOverflow社区d tool for running analysis of Python code. However, our main objective is to catch any potential bugs and not coding conventions. Enabling all Pylint ch

Pylint looks like a goo开发者_StackOverflow社区d 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?


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...).


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


Pyflakes should serve your purpose well.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号