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 two 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
Python Script with Adjustable Threshold
In the previous article from part 1 above I did a very basic introduction to how to run Pylint programmatically with python. Here I use that same code and more to do the following.
- Parametrize code path input
- Parametrize pylint score threshold
- Run pylint on that input directory
- Fail and exit 1 if the code fails to meet the threshold
- Pass and exit 0 if the code meets the given threshold
I have created this file in my project root as lint.py
.
First, we read in our two arguements with argarse…
import argparse
parser = argparse.ArgumentParser(prog="LINT")
parser.add_argument('-p',
'--path',
help='path to directory you want to run pylint | '
'Default: %(default)s | '
'Type: %(type)s ',
default='./src',
type=str)
parser.add_argument('-t',
'--threshold',
help='score threshold to fail pylint runner | '
'Default: %(default)s | '
'Type: %(type)s ',
default=7,
type=float)
args = parser.parse_args()
path = str(args.path)
threshold = float(args.threshold)
Next, we run Pylint…
from pylint.lint import Run
results = Run([path], do_exit=False)final_score = results.linter.stats['global_note']
Finally, we evaluate the score…
if final_score < threshold: raise Exception()else: exit(0)
In the full Gist code snippet above I use proper logging, outputs, and exception messages to make sure the runner is clear in what it is doing and in the final output!
Here are a few sample runs.
First we run the lint.py and it uses the default values and assume the source path as ./src
and the score threshold as 7
.
$ python lint.py
************* Module src.two
src/two.py:12:0: C0304: Final newline missing------------------------------------------------------------------
Your code has been rated at 1.88/10 (previous run: 1.88/10, +0.00)Exception: PyLint Failed | Score: 1.875 | Threshold: 7.0
You can also specify your threshold by using the parametrized arguments! In this example the code fails again and exits(1) because the code did not meet the threshold of 5.
$ python lint.py --path ./src --threshold 5
************* Module src.two
src/two.py:12:0: C0304: Final newline missing------------------------------------------------------------------
Your code has been rated at 1.88/10 (previous run: 1.88/10, +0.00)Exception: PyLint Failed | Score: 1.875 | Threshold: 5.0
If the code were to pass, like in the next example, it will exit(0)
and all is well! Notice how the threshold
argument can take a float like 1.5
in this case.
$ python lint.py --path ./src --threshold 1.5
************* Module src.two
src/two.py:12:0: C0304: Final newline missing------------------------------------------------------------------
Your code has been rated at 1.88/10 (previous run: 1.88/10, +0.00)PyLint Passed | Score: 1.875 | Threshold: 1.5
Bonus
Run with less verbose args.
$ python lint.py -p ./src -t 1.5
Get help if you forget how to run it!
$ python lint.py -hoptional arguments:
-h, --help show this help message and exit -p PATH, --path PATH path to directory you want to run pylint | Default: ./src | Type: str -t THRESHOLD, --threshold THRESHOLD
score threshold to fail pylint runner | Default: 7 | Type: float
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.
The End
The code above and from this series can be found on Github!
The next article talks about utilizing the python lint.py
script and using it as a Github Action to automatically pass/fail builds based on code quality.
The previous article in the series talks about running Pylint on the command line vs programatically.
Thanks!