From daee4a4722ab2ddd9490a50de2c4c1f590325aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Co=C3=AAlho?= Date: Fri, 1 Dec 2017 14:04:30 -0300 Subject: [PATCH] Add prospector for checking Python code (#1183) --- README.md | 2 +- ale_linters/python/prospector.vim | 82 +++++++++++ doc/ale-python.txt | 40 ++++++ doc/ale.txt | 3 +- test/handler/test_prospector_handler.vader | 158 +++++++++++++++++++++ 5 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 ale_linters/python/prospector.vim create mode 100644 test/handler/test_prospector_handler.vader diff --git a/README.md b/README.md index c5f63f34..0cc5206a 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ formatting. | proto | [protoc-gen-lint](https://github.com/ckaznocha/protoc-gen-lint) | | Pug | [pug-lint](https://github.com/pugjs/pug-lint) | | Puppet | [puppet](https://puppet.com), [puppet-lint](https://puppet-lint.com) | -| Python | [autopep8](https://github.com/hhatto/autopep8), [flake8](http://flake8.pycqa.org/en/latest/), [isort](https://github.com/timothycrosley/isort), [mypy](http://mypy-lang.org/), [pycodestyle](https://github.com/PyCQA/pycodestyle), [pyls](https://github.com/palantir/python-language-server), [pylint](https://www.pylint.org/) !!, [yapf](https://github.com/google/yapf) | +| Python | [autopep8](https://github.com/hhatto/autopep8), [flake8](http://flake8.pycqa.org/en/latest/), [isort](https://github.com/timothycrosley/isort), [mypy](http://mypy-lang.org/), [prospector](http://github.com/landscapeio/prospector), [pycodestyle](https://github.com/PyCQA/pycodestyle), [pyls](https://github.com/palantir/python-language-server), [pylint](https://www.pylint.org/) !!, [yapf](https://github.com/google/yapf) | | R | [lintr](https://github.com/jimhester/lintr) | | ReasonML | [merlin](https://github.com/the-lambda-church/merlin) see `:help ale-integration-reason-merlin` for configuration instructions, [ols](https://github.com/freebroccolo/ocaml-language-server), [refmt](https://github.com/reasonml/reason-cli) | | reStructuredText | [proselint](http://proselint.com/), [rstcheck](https://github.com/myint/rstcheck), [write-good](https://github.com/btford/write-good), [redpen](http://redpen.cc/) | diff --git a/ale_linters/python/prospector.vim b/ale_linters/python/prospector.vim new file mode 100644 index 00000000..66af598a --- /dev/null +++ b/ale_linters/python/prospector.vim @@ -0,0 +1,82 @@ +" Author: chocoelho +" Description: prospector linter python files + +let g:ale_python_prospector_executable = +\ get(g:, 'ale_python_prospector_executable', 'prospector') + +let g:ale_python_prospector_options = +\ get(g:, 'ale_python_prospector_options', '') + +let g:ale_python_prospector_use_global = get(g:, 'ale_python_prospector_use_global', 0) + +function! ale_linters#python#prospector#GetExecutable(buffer) abort + return ale#python#FindExecutable(a:buffer, 'python_prospector', ['prospector']) +endfunction + +function! ale_linters#python#prospector#GetCommand(buffer) abort + return ale#Escape(ale_linters#python#prospector#GetExecutable(a:buffer)) + \ . ' ' . ale#Var(a:buffer, 'python_prospector_options') + \ . ' --messages-only --absolute-paths --zero-exit --output-format json' + \ . ' %s' +endfunction + +function! ale_linters#python#prospector#Handle(buffer, lines) abort + let l:output = [] + + let l:prospector_error = json_decode(join(a:lines, '')) + + for l:error in l:prospector_error.messages + if (l:error.code is# 'W291' || l:error.code is# 'W293' || l:error.code is# 'trailing-whitespace') + \ && !ale#Var(a:buffer, 'warn_about_trailing_whitespace') + " Skip warnings for trailing whitespace if the option is off. + continue + endif + + if l:error.code is# 'W391' + \&& !ale#Var(a:buffer, 'warn_about_trailing_blank_lines') + " Skip warnings for trailing blank lines if the option is off + continue + endif + + if l:error.source =~# '\v\[%(dodgy|mccabe|pep8|pep257|pyroma)\]$' + let l:sub_type = 'style' + else + let l:sub_type = '' + endif + + if l:error.source =~# '\v\[pylint\]$' + let l:type = l:error.code =~? '\m^[CRW]' ? 'W' : 'E' + elseif l:error.source =~# '\v\[%(frosted|pep8)\]$' + let l:type = l:error.code =~? '\m^W' ? 'W' : 'E' + elseif l:error.source =~# '\v\[%(dodgy|pyroma|vulture)\]$' + let l:type = 'W' + else + let l:type = 'E' + endif + + let l:item = { + \ 'lnum': l:error.location.line, + \ 'col': l:error.location.character + 1, + \ 'text': l:error.message, + \ 'code': printf('(%s) %s', l:error.source, l:error.code), + \ 'type': l:type, + \ 'sub_type': l:sub_type, + \} + + if l:sub_type is# '' + unlet l:item.sub_type + endif + + call add(l:output, l:item) + endfor + + return l:output +endfunction + +call ale#linter#Define('python', { +\ 'name': 'prospector', +\ 'executable_callback': 'ale_linters#python#prospector#GetExecutable', +\ 'command_callback': 'ale_linters#python#prospector#GetCommand', +\ 'callback': 'ale_linters#python#prospector#Handle', +\ 'lint_file': 1, +\}) diff --git a/doc/ale-python.txt b/doc/ale-python.txt index a78cb5ac..742a8544 100644 --- a/doc/ale-python.txt +++ b/doc/ale-python.txt @@ -122,6 +122,46 @@ g:ale_python_mypy_use_global *g:ale_python_mypy_use_global* See |ale-integrations-local-executables| +=============================================================================== +prospector *ale-python-prospector* + +g:ale_python_prospector_executable *g:ale_python_prospector_executable* + *b:ale_python_prospector_executable* + Type: |String| + Default: `'prospector'` + + See |ale-integrations-local-executables| + + +g:ale_python_prospector_options *g:ale_python_prospector_options* + *b:ale_python_prospector_options* + Type: |String| + Default: `''` + + This variable can be changed to add command-line arguments to the prospector + invocation. + + For example, to dynamically switch between programs targeting Python 2 and + Python 3, you may want to set > + + let g:ale_python_prospector_executable = 'python3' + " or 'python' for Python 2 + let g:ale_python_prospector_options = '--rcfile /path/to/.prospector.yaml' + " The virtualenv detection needs to be disabled. + let g:ale_python_prospector_use_global = 0 + + after making sure it's installed for the appropriate Python versions (e.g. + `python3 -m pip install --user prospector`). + + +g:ale_python_prospector_use_global *g:ale_python_prospector_use_global* + *b:ale_python_prospector_use_global* + Type: |Number| + Default: `0` + + See |ale-integrations-local-executables| + + =============================================================================== pycodestyle *ale-python-pycodestyle* diff --git a/doc/ale.txt b/doc/ale.txt index a4ec7272..d39a5c65 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -153,6 +153,7 @@ CONTENTS *ale-contents* flake8..............................|ale-python-flake8| isort...............................|ale-python-isort| mypy................................|ale-python-mypy| + prospector..........................|ale-python-prospector| pycodestyle.........................|ale-python-pycodestyle| pylint..............................|ale-python-pylint| pyls................................|ale-python-pyls| @@ -326,7 +327,7 @@ Notes: * proto: `protoc-gen-lint` * Pug: `pug-lint` * Puppet: `puppet`, `puppet-lint` -* Python: `autopep8`, `flake8`, `isort`, `mypy`, `pycodestyle`, `pyls`, `pylint`!!, `yapf` +* Python: `autopep8`, `flake8`, `isort`, `mypy`, `prospector`, `pycodestyle`, `pyls`, `pylint`!!, `yapf` * R: `lintr` * ReasonML: `merlin`, `ols`, `refmt` * reStructuredText: `proselint`, `rstcheck`, `write-good`, `redpen` diff --git a/test/handler/test_prospector_handler.vader b/test/handler/test_prospector_handler.vader new file mode 100644 index 00000000..7962f6cd --- /dev/null +++ b/test/handler/test_prospector_handler.vader @@ -0,0 +1,158 @@ +Before: + Save g:ale_warn_about_trailing_whitespace + + let g:ale_warn_about_trailing_whitespace = 1 + + runtime ale_linters/python/prospector.vim + +After: + Restore + + call ale#linter#Reset() + + silent file something_else.py + +Execute(Basic prospector errors should be handle): + AssertEqual + \ [ + \ { + \ 'lnum': 20, + \ 'col': 1, + \ 'text': 'Trailing whitespace', + \ 'code': '(pylint) trailing-whitespace', + \ 'type': 'E', + \ }, + \ { + \ 'lnum': 1, + \ 'col': 1, + \ 'text': 'Missing module docstring', + \ 'code': '(pylint) missing-docstring', + \ 'type': 'E', + \ }, + \ { + \ 'lnum': 2, + \ 'col': 1, + \ 'text': 'Missing function docstring', + \ 'code': '(pylint) missing-docstring', + \ 'type': 'E', + \ }, + \ { + \ 'lnum': 3, + \ 'col': 5, + \ 'text': '''break'' not properly in loop', + \ 'code': '(pylint) not-in-loop', + \ 'type': 'E', + \ }, + \ { + \ 'lnum': 4, + \ 'col': 5, + \ 'text': 'Unreachable code', + \ 'code': '(pylint) unreachable', + \ 'type': 'E', + \ }, + \ { + \ 'lnum': 7, + \ 'col': 33, + \ 'text': 'No exception type(s) specified', + \ 'code': '(pylint) bare-except', + \ 'type': 'E', + \ }, + \ ], + \ ale_linters#python#prospector#Handle(bufnr(''), [ + \ '{', + \ ' "messages": [', + \ ' {', + \ ' "source": "pylint",', + \ ' "code": "trailing-whitespace",', + \ ' "message": "Trailing whitespace",', + \ ' "location": {', + \ ' "character": 0,', + \ ' "line": 20', + \ ' }', + \ ' },', + \ ' {', + \ ' "source": "pylint",', + \ ' "code": "missing-docstring",', + \ ' "message": "Missing module docstring",', + \ ' "location": {', + \ ' "character": 0,', + \ ' "line": 1', + \ ' }', + \ ' },', + \ ' {', + \ ' "source": "pylint",', + \ ' "code": "missing-docstring",', + \ ' "message": "Missing function docstring",', + \ ' "location": {', + \ ' "character": 0,', + \ ' "line": 2', + \ ' }', + \ ' },', + \ ' {', + \ ' "source": "pylint",', + \ ' "code": "not-in-loop",', + \ ' "message": "''break'' not properly in loop",', + \ ' "location": {', + \ ' "character": 4,', + \ ' "line": 3', + \ ' }', + \ ' },', + \ ' {', + \ ' "source": "pylint",', + \ ' "code": "unreachable",', + \ ' "message": "Unreachable code",', + \ ' "location": {', + \ ' "character": 4,', + \ ' "line": 4', + \ ' }', + \ ' },', + \ ' {', + \ ' "source": "pylint",', + \ ' "code": "bare-except",', + \ ' "message": "No exception type(s) specified",', + \ ' "location": {', + \ ' "character": 32,', + \ ' "line": 7', + \ ' }', + \ ' }', + \ ' ]', + \ '}', + \ ]) + +Execute(Ignoring trailing whitespace messages should work): + let g:ale_warn_about_trailing_whitespace = 0 + + AssertEqual + \ [ + \ { + \ 'lnum': 1, + \ 'col': 1, + \ 'text': 'Missing module docstring', + \ 'code': '(pylint) missing-docstring', + \ 'type': 'E', + \ }, + \ ], + \ ale_linters#python#prospector#Handle(bufnr(''), [ + \ '{', + \ ' "messages": [', + \ ' {', + \ ' "source": "pylint",', + \ ' "code": "trailing-whitespace",', + \ ' "message": "Trailing whitespace",', + \ ' "location": {', + \ ' "character": 0,', + \ ' "line": 4', + \ ' }', + \ ' },', + \ ' {', + \ ' "source": "pylint",', + \ ' "code": "missing-docstring",', + \ ' "message": "Missing module docstring",', + \ ' "location": {', + \ ' "character": 0,', + \ ' "line": 1', + \ ' }', + \ ' }', + \ ' ]', + \ '}', + \ ])