pythonpylintthreshold

How to set a pylint score threshold?


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.


Solution

  • 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)