diff options
Diffstat (limited to 'ale_linters/python/mypy.vim')
-rw-r--r-- | ale_linters/python/mypy.vim | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/ale_linters/python/mypy.vim b/ale_linters/python/mypy.vim index fff306ae..7e4270a2 100644 --- a/ale_linters/python/mypy.vim +++ b/ale_linters/python/mypy.vim @@ -1,18 +1,41 @@ " Author: Keith Smiley <k@keith.so>, w0rp <devw0rp@gmail.com> " Description: mypy support for optional python typechecking +let g:ale_python_mypy_executable = +\ get(g:, 'ale_python_mypy_executable', 'mypy') let g:ale_python_mypy_options = get(g:, 'ale_python_mypy_options', '') +let g:ale_python_mypy_use_global = get(g:, 'ale_python_mypy_use_global', 0) + +" (cd /home/w0rp/git/wazoku/wazoku-spotlight && /home/w0rp/git/wazoku/wazoku-spotlight/ve-py3/bin/mypy --show-column-numbers /home/w0rp/git/wazoku/wazoku-spotlight/central/models/__init__.py) | grep ^central/models/__init__.p + +function! ale_linters#python#mypy#GetExecutable(buffer) abort + if !ale#Var(a:buffer, 'python_mypy_use_global') + let l:virtualenv = ale#python#FindVirtualenv(a:buffer) + + if !empty(l:virtualenv) + let l:ve_mypy = l:virtualenv . '/bin/mypy' + + if executable(l:ve_mypy) + return l:ve_mypy + endif + endif + endif + + return ale#Var(a:buffer, 'python_mypy_executable') +endfunction function! ale_linters#python#mypy#GetCommand(buffer) abort - let l:automatic_stubs_dir = ale#path#FindNearestDirectory(a:buffer, 'stubs') - " TODO: Add Windows support - let l:automatic_stubs_command = (has('unix') && !empty(l:automatic_stubs_dir)) - \ ? 'MYPYPATH=' . l:automatic_stubs_dir . ' ' + let l:project_root = ale#python#FindProjectRoot(a:buffer) + let l:cd_command = !empty(l:project_root) + \ ? ale#path#CdString(l:project_root) \ : '' + let l:executable = ale_linters#python#mypy#GetExecutable(a:buffer) - return 'mypy --show-column-numbers ' + return l:cd_command + \ . fnameescape(l:executable) + \ . ' --show-column-numbers ' \ . ale#Var(a:buffer, 'python_mypy_options') - \ . ' %t' + \ . ' %s' endfunction function! ale_linters#python#mypy#Handle(buffer, lines) abort @@ -23,21 +46,20 @@ function! ale_linters#python#mypy#Handle(buffer, lines) abort " Lines like these should be ignored below: " " file.py:4: note: (Stub files are from https://github.com/python/typeshed) - let l:pattern = '\v^[a-zA-Z]?:?[^:]+:(\d+):?(\d+)?: ([^:]+): (.+)$' + let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):?(\d+)?: (error|warning): (.+)$' let l:output = [] + let l:buffer_filename = expand('#' . a:buffer . ':p') for l:match in ale#util#GetMatches(a:lines, l:pattern) - if l:match[4] =~# 'Stub files are from' - " The lines telling us where to get stub files from make it so - " we can't read the actual errors, so exclude them. + if l:buffer_filename[-len(l:match[1]):] !=# l:match[1] continue endif call add(l:output, { - \ 'lnum': l:match[1] + 0, - \ 'col': l:match[2] + 0, - \ 'text': l:match[4], - \ 'type': l:match[3] =~# 'error' ? 'E' : 'W', + \ 'lnum': l:match[2] + 0, + \ 'col': l:match[3] + 0, + \ 'type': l:match[4] =~# 'error' ? 'E' : 'W', + \ 'text': l:match[5], \}) endfor @@ -46,7 +68,8 @@ endfunction call ale#linter#Define('python', { \ 'name': 'mypy', -\ 'executable': 'mypy', +\ 'executable_callback': 'ale_linters#python#mypy#GetExecutable', \ 'command_callback': 'ale_linters#python#mypy#GetCommand', \ 'callback': 'ale_linters#python#mypy#Handle', +\ 'lint_file': 1, \}) |