Skip to content

Instantly share code, notes, and snippets.

@bh11111
Created December 10, 2019 01:56
Show Gist options
  • Save bh11111/4d09b45b8a4152fe9795096ff8f2b1d1 to your computer and use it in GitHub Desktop.
Save bh11111/4d09b45b8a4152fe9795096ff8f2b1d1 to your computer and use it in GitHub Desktop.
Python Pylint Runner to Pass (Exit 0) or Fail (Exit 1) Based on Pylint Score Threshold
import argparse
import logging
from pylint.lint import Run
logging.getLogger().setLevel(logging.INFO)
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)
logging.info('PyLint Starting | '
'Path: {} | '
'Threshold: {} '.format(path, threshold))
results = Run([path], do_exit=False)
final_score = results.linter.stats['global_note']
if final_score < threshold:
message = ('PyLint Failed | '
'Score: {} | '
'Threshold: {} '.format(final_score, threshold))
logging.error(message)
raise Exception(message)
else:
message = ('PyLint Passed | '
'Score: {} | '
'Threshold: {} '.format(final_score, threshold))
logging.info(message)
exit(0)
@Yamen-Malik
Copy link

Yamen-Malik commented Feb 27, 2023

Thanks for sharing the script.

But please note that for pylint >= 2.12.2 you will get TypeError: 'LinterStats' object is not subscriptable on line 36.
Please use results.linter.stats.global_note instead.

And for anyone looking to add a success threshold, since pylint 2.5.0 you can use a new argument called --fail-under

Example

pylint --fail-under=8 python_code.py

@cglukas
Copy link

cglukas commented Mar 4, 2024

Damn I should have read this comment before forking this gist XD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment