0

I am using Pylint on a project and would like to use it as a step of our continuos integration, I want that every time that missin-final-newline message is happend it will exit(1). My code just walk through all the paths and pylint all over files:

def get_all_py_paths() -> List:
"""
Iterate over all directories and get the full path of .py files.
:return: list of absolute .py paths
"""
logging.info('Run get all path function')
file_paths = []
for root, _, files in os.walk('../'):
    for filename in files:
        if filename.endswith('.py') and '__init__' not in filename:
            filepath = os.path.join(root, filename)
            file_paths.append(filepath)
return file_paths


if __name__ == '__main__':

 files_path = get_all_py_paths()
 results = lint.Run(PYLINT_PARAMS + files_path, do_exit=False)
 print(results.option_groups)
 pylint_score = results.linter.stats['global_note']

 if pylint_score < THRESHOLD:
    exit(1)

i added to the pylintrc file:

fail-on=missing-final-newline

and

enable=c-extension-no-member, missing-final-newline

but also with the fail-on it still just warning and not exit(1). There is a way to add something to pylint conf something to make missing-final-newline to exit(1) if it happend?

Bernana
  • 245
  • 1
  • 12
  • 1
    Take a look at this repository, you might find it useful for your CI: https://github.com/jenstroeger/python-package-template/ In particular, take a look at the [end-of-file-fixer](https://github.com/pre-commit/pre-commit-hooks#end-of-file-fixer) commit hook (which I used in that example package). – Jens Oct 11 '21 at 11:30
  • You should not have to make a script for that pylint can do it itself. – Pierre.Sassoulas Oct 11 '21 at 12:52
  • I wrote a script that validate that every line is end with new line. But i'm checking if pylint can do it. – Bernana Oct 11 '21 at 13:02
  • @Pierre.Sassoulas How Pylint do it? – Bernana Oct 11 '21 at 13:02
  • Say you have a file `a.py` with a missing final line and you do :`pylint a.py` then pylint warn about : `a.py:1:0: C0304: Final newline missing (missing-final-newline)` – Pierre.Sassoulas Oct 11 '21 at 14:39
  • @Pierre.Sassoulas that's i know. but instead of warn i want that if i get this warn the code will exit(1) – Bernana Oct 12 '21 at 07:16

1 Answers1

0

You can use pylint directly without doing a script. Say you have a file a.py with no final new line. Then if you do pylint a.py --fail-on=missing-final-newline;echo $?, pylint a.py --disable=all --enable=missing-final-newline;echo $? or pylint a.py;echo $? (provided missing-final-newline is not disabled by configuration) then the exit code will be 16 not 0.

************* Module a
a.py:1:0: C0304: Final newline missing (missing-final-newline)

------------------------------------------------------------------
Your code has been rated at 0.00/10 (previous run: 0.00/10, +0.00)

16

This is due to the fact that pylint have exit code between 0 and 32, see documentation for 2.11.1:

exit code   meaning
0           no error     
1           fatal message issued     
2           error message issued     
4           warning message issued   
8           refactor message issued      
16          convention message issued
Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48
  • First of all thank you very much. i don't run the command from the terminal because i wan't to use it as a codebuild. so i'm searching for a way to just add something to the code or to the pylint configuration. i added the fail-on=missin-final-newline to the pylintrc but nothing changed with it. – Bernana Oct 14 '21 at 10:28
  • Ho, sorry I did not understand that. I could update the answer but check https://stackoverflow.com/a/2062665/2519059, you should be able to use all the options you would when launching it non programmatically. – Pierre.Sassoulas Oct 15 '21 at 08:12