I'd like to extract my pylint rating and set a threshold. Let me explain, for example, if the score is under 5, I want exit 1; And if my code is rated higher than 5, I want exit 0 and continue my Jenkins procedure.
Asked
Active
Viewed 5,705 times
11
-
Please, check @Gooseman answer. I think it is the best answer now. – Alexandre Paes Aug 30 '20 at 23:37
3 Answers
11
Since pylint 2.5.0 there is a new argument called --fail-under
that resolves this question without needing external tools or scripts.
In this example, pylint will exit with error when score is under 8:
pylint --fail-under=8 python_code.py
Gooseman
- 1,639
- 14
- 14
8
Here's a way to access the pylint API it in Python. The following code should be saved to a file and executed with first argument to the script to be module/file to lint:
import sys
from pylint import lint
THRESHOLD = 5
if len(sys.argv) < 2:
raise ArgumentError("Module to evaluate needs to be the first argument")
run = lint.Run([sys.argv[1]], do_exit=False)
score = run.linter.stats['global_note']
if score < THRESHOLD:
sys.exit(1)
Bryce Guinta
- 2,556
- 32
- 29
5
Install
> pip install pylint-fail-under
And you can check the threshold value as below
pylint-fail-under --fail_under=6.0 test_pylint_code.py (or path)
If the score is below 6.0 it returns a message
ERROR: score 5.3999999999999995 is less than fail-under value 6.0
Else it returns exit code 0.
Link to official documentation is https://pypi.org/project/pylint-fail-under/