All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 " Author: Kevin Locke <kevin@kevinlocke.name>
2 " Description: pylama for python files
4 call ale#Set('python_pylama_executable', 'pylama')
5 call ale#Set('python_pylama_options', '')
6 call ale#Set('python_pylama_use_global', get(g:, 'ale_use_global_executables', 0))
7 call ale#Set('python_pylama_auto_pipenv', 0)
8 call ale#Set('python_pylama_auto_poetry', 0)
9 call ale#Set('python_pylama_auto_uv', 0)
10 call ale#Set('python_pylama_change_directory', 1)
12 function! ale_linters#python#pylama#GetExecutable(buffer) abort
13 if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_pylama_auto_pipenv'))
14 \ && ale#python#PipenvPresent(a:buffer)
18 if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_pylama_auto_poetry'))
19 \ && ale#python#PoetryPresent(a:buffer)
23 if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'python_pylama_auto_uv'))
24 \ && ale#python#UvPresent(a:buffer)
28 return ale#python#FindExecutable(a:buffer, 'python_pylama', ['pylama'])
31 function! ale_linters#python#pylama#RunWithVersionCheck(buffer) abort
32 let l:executable = ale_linters#python#pylama#GetExecutable(a:buffer)
33 let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
37 let l:command = ale#Escape(l:executable) . l:exec_args . ' --version'
39 return ale#semver#RunWithVersionCheck(
43 \ function('ale_linters#python#pylama#GetCommand'),
47 function! ale_linters#python#pylama#GetCwd(buffer) abort
48 if ale#Var(a:buffer, 'python_pylama_change_directory')
49 " Pylama loads its configuration from the current directory only, and
50 " applies file masks using paths relative to the current directory.
51 " Run from project root, if found, otherwise buffer dir.
52 let l:project_root = ale#python#FindProjectRoot(a:buffer)
54 return !empty(l:project_root) ? l:project_root : '%s:h'
60 function! ale_linters#python#pylama#GetCommand(buffer, version) abort
61 let l:executable = ale_linters#python#pylama#GetExecutable(a:buffer)
62 let l:exec_args = l:executable =~? '\(pipenv\|poetry\|uv\)$'
66 " json format is added in version 8.1.4
67 " https://github.com/klen/pylama/blob/develop/Changelog
68 let l:format_json_args = ale#semver#GTE(a:version, [8, 1, 4])
72 " Note: Using %t to lint changes would be preferable, but many pylama
73 " checks use surrounding paths (e.g. C0103 module name, E0402 relative
74 " import beyond top, etc.). Neither is ideal.
75 return ale#Escape(l:executable) . l:exec_args
76 \ . ale#Pad(ale#Var(a:buffer, 'python_pylama_options'))
77 \ . l:format_json_args
81 function! ale_linters#python#pylama#Handle(buffer, version, lines) abort
86 let l:output = ale#python#HandleTraceback(a:lines, 1)
88 " First letter of error code is a pylint-compatible message type
89 " http://pylint.pycqa.org/en/latest/user_guide/output.html#source-code-analysis-section
90 " D is for Documentation (pydocstyle)
91 let l:pylint_type_to_ale_type = {
100 let l:pylint_type_to_ale_sub_type = {
106 if ale#semver#GTE(a:version, [8, 1, 4])
108 let l:errors = json_decode(join(a:lines, ''))
117 for l:error in l:errors
119 \ 'lnum': l:error['lnum'],
120 \ 'col': l:error['col'],
121 \ 'code': l:error['number'],
122 \ 'type': get(l:pylint_type_to_ale_type, l:error['etype'], 'W'),
123 \ 'sub_type': get(l:pylint_type_to_ale_sub_type, l:error['etype'], ''),
124 \ 'text': printf('%s [%s]', l:error['message'], l:error['source']),
128 let l:pattern = '\v^.{-}:([0-9]+):([0-9]+): +%(([A-Z][0-9]+):? +)?(.*)$'
130 for l:match in ale#util#GetMatches(a:lines, l:pattern)
132 \ 'lnum': str2nr(l:match[1]),
133 \ 'col': str2nr(l:match[2]),
134 \ 'code': l:match[3],
135 \ 'type': get(l:pylint_type_to_ale_type, l:match[3][0], 'W'),
136 \ 'sub_type': get(l:pylint_type_to_ale_sub_type, l:match[3][0], ''),
137 \ 'text': l:match[4],
145 call ale#linter#Define('python', {
147 \ 'executable': function('ale_linters#python#pylama#GetExecutable'),
148 \ 'cwd': function('ale_linters#python#pylama#GetCwd'),
149 \ 'command': function('ale_linters#python#pylama#RunWithVersionCheck'),
150 \ 'callback': {buffer, lines -> ale#semver#RunWithVersionCheck(
152 \ ale_linters#python#pylama#GetExecutable(buffer),
154 \ {buffer, version -> ale_linters#python#pylama#Handle(