Pylint is an excellent tool to have good quality code.
This post is not about how pylint works. Lets see most common pylint errors:
pylint C0111:Missing module docstring
When you are defining methods in python, its necessary to give them some text help what the function is all about.
Running pylint on this method will give you this error.
To solve this:
This function prints hello
pylint: Method could be a function (no-self-use)
This error happens in class methods. You have a class and some methods, but in this method you are not using ‘self’ object which is usually
a reference to the current instance of the class.
In above method, ‘self’ is unused. And, you will get this error: pylint: Method could be a function (no-self-use)
There are two solutions to this:
- Either use ‘self’ in this method. May be a logging object or something.
- Remove ‘self’. But, then you have to make some more changes to method to something like below:
Unable to import for custom module
You have some custom modules and trying to run pylint. Pylint complains about unknown modules. What to do?
To solve this, you need to edit your .pylintrc file,
Open .pylintrc file
Look for [MASTER] section
In init-hook, append your module paths as:
init-hook='import sys; sys.path.append('./modules/hardening_base');sys.path.append('./modules/script_collector');
Where, modules/hardening_base is path of one of my module from root folder of project.
Now run pylint, and error is gone.
pylint: Constant name doesn’t conform to UPPER_CASE naming style
In your main file, you are probably initializing an object without a function.
Define a method.
if __name__ == "__main__":
Do not use len(args) to determine if a sequence is empty
args is an array and I want to put an if condition to check if length is greater than 0.
if len(args) > 0:
# do something
In python 3, this can be re-written as:
Line too long (147/100) (line-too-long)
Your source code line is this much character long. And, recommended configuration says you should have max of 100 character long line.
There are two solution:
- Break your line to multiple lines
Break string to multiple lines
a = 'abc abc '\
Break function call into multiple lines
- Change your .pylintrc (Not recommended way)
Edit your .pylintrc file:
look for [FORMAT] section
change it to some greater value.
But, this is not recommended way. You should break your lines into multiple lines.
Disable some pylint check (Not Recommended)
There is one setting which can disable certain checks that pylint runs. You can disable few checks as per your need.
Edit your .pylintrc file:
Look for [MESSAGES CONTROL]
See disable setting
You can include the pylint error here, which mentions name of check, and run pylint again.