Pylint is an incredibly useful tool for static code analysis. It provides a simple score out of 10, a detailed output on what to fix, and the ability to ignore things you do not believe in.
This is part one of three of a series I am doing on how I use pylint in my projects.
- Part 1: Executing with Command Line vs Programmatically
- Part 2: Pylint Runner Script with Changeable Threshold
- Part 3: A Github Action for Failing Builds under a Pylint Threshold
Command Line vs Programmatically
In both of these instances, you will have to install Pylint.
$ pip install pylint
This should give you confirmation it is installed.
.
.
.
Installing collected packages: pylint
Successfully installed pylint-2.4.4
I will assume your code is in a folder called src/
. Now to run on your project with the command line you can run the following.
$ pylint src/
Which should give you some output from the static code analysis followed by a score!
************* Module src.src_hi
src/src_hi.py:6:0: C0103: Constant name "logger" doesn't conform to UPPER_CASE naming style (invalid-name)
************* Module src.src_try
src/src_src.py:11:2: W0511: TODO: test (fixme)------------------------------------------------------------------
Your code has been rated at 9.55/10 (previous run: 9.55/10, +0.00)
But what if you want to do the same with python code? First make a python file in your project directory at the same level you ran the above script, I will call it simple.py
.
Then run the python file and you should see the same output as you got before!
$ python simple.py************* Module src.src_hi
src/src_hi.py:6:0: C0103: Constant name "logger" doesn't conform to UPPER_CASE naming style (invalid-name)
************* Module src.src_try
src/src_src.py:11:2: W0511: TODO: test (fixme)------------------------------------------------------------------
Your code has been rated at 9.55/10 (previous run: 9.55/10, +0.00)
Now that you know how to run with the command line and from a python script, we can go into a more in depth into utility.
This code above and from the next to parts of the series can be found on Github!
Thanks!
Personal Note
Real projects should expect much higher scores. My production projects usually are set to have 9 or 9.5 as the score threshold limit. However, I do use additional exclusions that differ from the base Pylint setup.
C0114, # missing-module-docstring
C0115, # missing-class-docstring
C0116, # missing-function-docstring
R0903 # too-few-public-methods
This snippet can be copy and pasted into the disable
section of a .pylintrc
file and it will ignore those errors and they will not lower your score.