Improve pylama linter output handling (#4106)

* Use JSON format for newer pylama version

https://github.com/klen/pylama/blob/develop/Changelog

The --format json option is added in pylama version 8.1.4.

* Fix linting warnings for pylama
This commit is contained in:
Diep Pham 2022-04-24 06:28:33 +07:00 committed by GitHub
parent e2ef4d91ee
commit 6c51bb1573
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 155 additions and 25 deletions

View File

@ -22,6 +22,22 @@ function! ale_linters#python#pylama#GetExecutable(buffer) abort
return ale#python#FindExecutable(a:buffer, 'python_pylama', ['pylama']) return ale#python#FindExecutable(a:buffer, 'python_pylama', ['pylama'])
endfunction endfunction
function! ale_linters#python#pylama#RunWithVersionCheck(buffer) abort
let l:executable = ale_linters#python#pylama#GetExecutable(a:buffer)
let l:exec_args = l:executable =~? 'pipenv\|poetry$'
\ ? ' run pylama'
\ : ''
let l:command = ale#Escape(l:executable) . l:exec_args . ' --version'
return ale#semver#RunWithVersionCheck(
\ a:buffer,
\ l:executable,
\ l:command,
\ function('ale_linters#python#pylama#GetCommand'),
\)
endfunction
function! ale_linters#python#pylama#GetCwd(buffer) abort function! ale_linters#python#pylama#GetCwd(buffer) abort
if ale#Var(a:buffer, 'python_pylama_change_directory') if ale#Var(a:buffer, 'python_pylama_change_directory')
" Pylama loads its configuration from the current directory only, and " Pylama loads its configuration from the current directory only, and
@ -35,27 +51,33 @@ function! ale_linters#python#pylama#GetCwd(buffer) abort
return '' return ''
endfunction endfunction
function! ale_linters#python#pylama#GetCommand(buffer) abort function! ale_linters#python#pylama#GetCommand(buffer, version) abort
let l:executable = ale_linters#python#pylama#GetExecutable(a:buffer) let l:executable = ale_linters#python#pylama#GetExecutable(a:buffer)
let l:exec_args = l:executable =~? 'pipenv\|poetry$' let l:exec_args = l:executable =~? 'pipenv\|poetry$'
\ ? ' run pylama' \ ? ' run pylama'
\ : '' \ : ''
" json format is added in version 8.1.4
" https://github.com/klen/pylama/blob/develop/Changelog
let l:format_json_args = ale#semver#GTE(a:version, [8, 1, 4])
\ ? ' --format json'
\ : ''
" Note: Using %t to lint changes would be preferable, but many pylama " Note: Using %t to lint changes would be preferable, but many pylama
" checks use surrounding paths (e.g. C0103 module name, E0402 relative " checks use surrounding paths (e.g. C0103 module name, E0402 relative
" import beyond top, etc.). Neither is ideal. " import beyond top, etc.). Neither is ideal.
return ale#Escape(l:executable) . l:exec_args return ale#Escape(l:executable) . l:exec_args
\ . ale#Pad(ale#Var(a:buffer, 'python_pylama_options')) \ . ale#Pad(ale#Var(a:buffer, 'python_pylama_options'))
\ . l:format_json_args
\ . ' %s' \ . ' %s'
endfunction endfunction
function! ale_linters#python#pylama#Handle(buffer, lines) abort function! ale_linters#python#pylama#Handle(buffer, version, lines) abort
if empty(a:lines) if empty(a:lines)
return [] return []
endif endif
let l:output = ale#python#HandleTraceback(a:lines, 1) let l:output = ale#python#HandleTraceback(a:lines, 1)
let l:pattern = '\v^.{-}:([0-9]+):([0-9]+): +%(([A-Z][0-9]+):? +)?(.*)$'
" First letter of error code is a pylint-compatible message type " First letter of error code is a pylint-compatible message type
" http://pylint.pycqa.org/en/latest/user_guide/output.html#source-code-analysis-section " http://pylint.pycqa.org/en/latest/user_guide/output.html#source-code-analysis-section
@ -75,16 +97,41 @@ function! ale_linters#python#pylama#Handle(buffer, lines) abort
\ 'D': 'style', \ 'D': 'style',
\} \}
for l:match in ale#util#GetMatches(a:lines, l:pattern) if ale#semver#GTE(a:version, [8, 1, 4])
call add(l:output, { try
\ 'lnum': str2nr(l:match[1]), let l:errors = json_decode(join(a:lines, ''))
\ 'col': str2nr(l:match[2]), catch
\ 'code': l:match[3], return l:output
\ 'type': get(l:pylint_type_to_ale_type, l:match[3][0], 'W'), endtry
\ 'sub_type': get(l:pylint_type_to_ale_sub_type, l:match[3][0], ''),
\ 'text': l:match[4], if empty(l:errors)
\}) return l:output
endfor endif
for l:error in l:errors
call add(l:output, {
\ 'lnum': l:error['lnum'],
\ 'col': l:error['col'],
\ 'code': l:error['number'],
\ 'type': get(l:pylint_type_to_ale_type, l:error['etype'], 'W'),
\ 'sub_type': get(l:pylint_type_to_ale_sub_type, l:error['etype'], ''),
\ 'text': printf('%s [%s]', l:error['message'], l:error['source']),
\})
endfor
else
let l:pattern = '\v^.{-}:([0-9]+):([0-9]+): +%(([A-Z][0-9]+):? +)?(.*)$'
for l:match in ale#util#GetMatches(a:lines, l:pattern)
call add(l:output, {
\ 'lnum': str2nr(l:match[1]),
\ 'col': str2nr(l:match[2]),
\ 'code': l:match[3],
\ 'type': get(l:pylint_type_to_ale_type, l:match[3][0], 'W'),
\ 'sub_type': get(l:pylint_type_to_ale_sub_type, l:match[3][0], ''),
\ 'text': l:match[4],
\})
endfor
endif
return l:output return l:output
endfunction endfunction
@ -93,7 +140,15 @@ call ale#linter#Define('python', {
\ 'name': 'pylama', \ 'name': 'pylama',
\ 'executable': function('ale_linters#python#pylama#GetExecutable'), \ 'executable': function('ale_linters#python#pylama#GetExecutable'),
\ 'cwd': function('ale_linters#python#pylama#GetCwd'), \ 'cwd': function('ale_linters#python#pylama#GetCwd'),
\ 'command': function('ale_linters#python#pylama#GetCommand'), \ 'command': function('ale_linters#python#pylama#RunWithVersionCheck'),
\ 'callback': 'ale_linters#python#pylama#Handle', \ 'callback': {buffer, lines -> ale#semver#RunWithVersionCheck(
\ buffer,
\ ale_linters#python#pylama#GetExecutable(buffer),
\ '%e --version',
\ {buffer, version -> ale_linters#python#pylama#Handle(
\ buffer,
\ l:version,
\ lines)},
\ )},
\ 'lint_file': 1, \ 'lint_file': 1,
\}) \})

View File

@ -12,10 +12,13 @@ After:
silent file something_else.py silent file something_else.py
Execute(The pylama handler should handle no messages): Execute(The pylama handler should handle no messages with version older than 8.1.4):
AssertEqual [], ale_linters#python#pylama#Handle(bufnr(''), []) AssertEqual [], ale_linters#python#pylama#Handle(bufnr(''), [8, 0, 5], [])
Execute(The pylama handler should handle basic warnings and syntax errors): Execute(The pylama handler should handle no messages with version newer or equal than 8.1.4):
AssertEqual [], ale_linters#python#pylama#Handle(bufnr(''), [8, 2, 0], [])
Execute(The pylama handler should handle basic warnings and syntax errors with version older than 8.1.4):
AssertEqual AssertEqual
\ [ \ [
\ { \ {
@ -83,7 +86,7 @@ Execute(The pylama handler should handle basic warnings and syntax errors):
\ 'text': 'Invalid string quote ", should be '' [pylint]', \ 'text': 'Invalid string quote ", should be '' [pylint]',
\ }, \ },
\ ], \ ],
\ ale_linters#python#pylama#Handle(bufnr(''), [ \ ale_linters#python#pylama#Handle(bufnr(''), [8, 0, 5], [
\ 'No config file found, using default configuration', \ 'No config file found, using default configuration',
\ 'index.py:8:1: W0611 ''foo'' imported but unused [pyflakes]', \ 'index.py:8:1: W0611 ''foo'' imported but unused [pyflakes]',
\ 'index.py:8:0: E0401 Unable to import ''foo'' [pylint]', \ 'index.py:8:0: E0401 Unable to import ''foo'' [pylint]',
@ -95,7 +98,79 @@ Execute(The pylama handler should handle basic warnings and syntax errors):
\ 'index.py:20:0: C4001 Invalid string quote ", should be '' [pylint]', \ 'index.py:20:0: C4001 Invalid string quote ", should be '' [pylint]',
\ ]) \ ])
Execute(The pylama handler should handle tracebacks with parsable messages): Execute(The pylama handler should handle basic warnings and syntax errors with version newer than 8.1.4):
AssertEqual
\ [
\ {
\ 'lnum': 8,
\ 'col': 1,
\ 'code': 'W0611',
\ 'type': 'W',
\ 'sub_type': '',
\ 'text': '''foo'' imported but unused [pyflakes]',
\ },
\ {
\ 'lnum': 8,
\ 'col': 0,
\ 'code': 'E0401',
\ 'type': 'E',
\ 'sub_type': '',
\ 'text': 'Unable to import ''foo'' [pylint]',
\ },
\ {
\ 'lnum': 10,
\ 'col': 1,
\ 'code': 'E302',
\ 'type': 'E',
\ 'sub_type': '',
\ 'text': 'expected 2 blank lines, found 1 [pycodestyle]',
\ },
\ {
\ 'lnum': 11,
\ 'col': 1,
\ 'code': 'D401',
\ 'type': 'W',
\ 'sub_type': 'style',
\ 'text': 'First line should be in imperative mood (''Get'', not ''Gets'') [pydocstyle]',
\ },
\ {
\ 'lnum': 15,
\ 'col': 81,
\ 'code': 'E501',
\ 'type': 'E',
\ 'sub_type': '',
\ 'text': 'line too long (96 > 80 characters) [pycodestyle]',
\ },
\ {
\ 'lnum': 16,
\ 'col': 1,
\ 'code': 'D203',
\ 'type': 'W',
\ 'sub_type': 'style',
\ 'text': '1 blank line required before class docstring (found 0) [pydocstyle]',
\ },
\ {
\ 'lnum': 18,
\ 'col': 1,
\ 'code': 'D107',
\ 'type': 'W',
\ 'sub_type': 'style',
\ 'text': 'Missing docstring in __init__ [pydocstyle]',
\ },
\ {
\ 'lnum': 20,
\ 'col': 0,
\ 'code': 'C4001',
\ 'type': 'W',
\ 'sub_type': 'style',
\ 'text': 'Invalid string quote ", should be '' [pylint]',
\ },
\ ],
\ ale_linters#python#pylama#Handle(bufnr(''), [8, 2, 0], [
\ '[{"source":"pyflakes","col":1,"lnum":8,"etype":"W","message":"''foo'' imported but unused","filename":"index.py","number":"W0611"},{"source":"pylint","col":0,"lnum":8,"etype":"E","message":"Unable to import ''foo''","filename":"index.py","number":"E0401"},{"source":"pycodestyle","col":1,"lnum":10,"etype":"E","message":"expected 2 blank lines, found 1","filename":"index.py","number":"E302"},{"source":"pydocstyle","col":1,"lnum":11,"etype":"D","message":"First line should be in imperative mood (''Get'', not ''Gets'')","filename":"index.py","number":"D401"},{"source":"pycodestyle","col":81,"lnum":15,"etype":"E","message":"line too long (96 > 80 characters)","filename":"index.py","number":"E501"},{"source":"pydocstyle","col":1,"lnum":16,"etype":"D","message":"1 blank line required before class docstring (found 0)","filename":"index.py","number":"D203"},{"source":"pydocstyle","col":1,"lnum":18,"etype":"D","message":"Missing docstring in __init__","filename":"index.py","number":"D107"},{"source":"pylint","col":0,"lnum":20,"etype":"C","message":"Invalid string quote \", should be ''","filename":"index.py","number":"C4001"}]',
\ ])
Execute(The pylama handler should handle tracebacks with parsable messages with version older than 8.1.4):
AssertEqual AssertEqual
\ [ \ [
\ { \ {
@ -135,7 +210,7 @@ Execute(The pylama handler should handle tracebacks with parsable messages):
\ 'text': 'line too long (96 > 80 characters) [pycodestyle]', \ 'text': 'line too long (96 > 80 characters) [pycodestyle]',
\ }, \ },
\ ], \ ],
\ ale_linters#python#pylama#Handle(bufnr(''), [ \ ale_linters#python#pylama#Handle(bufnr(''), [8, 0, 5], [
\ 'Traceback (most recent call last):', \ 'Traceback (most recent call last):',
\ ' File "/usr/local/lib/python2.7/site-packages/pylama/core.py", line 66, in run', \ ' File "/usr/local/lib/python2.7/site-packages/pylama/core.py", line 66, in run',
\ ' path, code=code, ignore=ignore, select=select, params=lparams)', \ ' path, code=code, ignore=ignore, select=select, params=lparams)',
@ -158,7 +233,7 @@ Execute(The pylama handler should handle tracebacks with parsable messages):
" Note: This is probably a bug, since all pylama plugins produce codes, but " Note: This is probably a bug, since all pylama plugins produce codes, but
" should be handled for compatibility. " should be handled for compatibility.
" Note: The pylama isort plugin is distributed in the isort package. " Note: The pylama isort plugin is distributed in the isort package.
Execute(The pylama handler should handle messages without codes): Execute(The pylama handler should handle messages without codes with version older than 8.1.4):
AssertEqual AssertEqual
\ [ \ [
\ { \ {
@ -170,13 +245,13 @@ Execute(The pylama handler should handle messages without codes):
\ 'text': 'Incorrectly sorted imports. [isort]' \ 'text': 'Incorrectly sorted imports. [isort]'
\ }, \ },
\ ], \ ],
\ ale_linters#python#pylama#Handle(bufnr(''), [ \ ale_linters#python#pylama#Handle(bufnr(''), [8, 0, 5], [
\ 'index.py:0:0: Incorrectly sorted imports. [isort]', \ 'index.py:0:0: Incorrectly sorted imports. [isort]',
\ ]) \ ])
" Note: This is a pylama bug, but should be handled for compatibility. " Note: This is a pylama bug, but should be handled for compatibility.
" See https://github.com/klen/pylama/pull/146 " See https://github.com/klen/pylama/pull/146
Execute(The pylama handler should handle message codes followed by a colon): Execute(The pylama handler should handle message codes followed by a colon with version older than 8.1.4):
AssertEqual AssertEqual
\ [ \ [
\ { \ {
@ -188,6 +263,6 @@ Execute(The pylama handler should handle message codes followed by a colon):
\ 'text': 'Found commented out code: # needs_sphinx = ''1.0'' [eradicate]', \ 'text': 'Found commented out code: # needs_sphinx = ''1.0'' [eradicate]',
\ }, \ },
\ ], \ ],
\ ale_linters#python#pylama#Handle(bufnr(''), [ \ ale_linters#python#pylama#Handle(bufnr(''), [8, 0, 5], [
\ 'index.py:31:1: E800: Found commented out code: # needs_sphinx = ''1.0'' [eradicate]', \ 'index.py:31:1: E800: Found commented out code: # needs_sphinx = ''1.0'' [eradicate]',
\ ]) \ ])