Improve mypy handling a little bit more

This commit is contained in:
w0rp 2017-01-20 17:30:23 +00:00
parent ea438be5c1
commit 9820899b9e
6 changed files with 100 additions and 4 deletions

View file

@ -1,17 +1,62 @@
" Author: Keith Smiley <k@keith.so> " Author: Keith Smiley <k@keith.so>, w0rp <devw0rp@gmail.com>
" Description: mypy support for optional python typechecking " Description: mypy support for optional python typechecking
let g:ale_python_mypy_options = get(g:, 'ale_python_mypy_options', '') let g:ale_python_mypy_options = get(g:, 'ale_python_mypy_options', '')
function! g:ale_linters#python#mypy#GetCommand(buffer) abort function! g:ale_linters#python#mypy#GetCommand(buffer) abort
return g:ale#util#stdin_wrapper let l:automatic_stubs_dir = ale#util#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 . ' '
\ : ''
return l:automatic_stubs_command
\ . g:ale#util#stdin_wrapper
\ . ' .py mypy --show-column-numbers ' \ . ' .py mypy --show-column-numbers '
\ . g:ale_python_mypy_options \ . g:ale_python_mypy_options
endfunction endfunction
function! g:ale_linters#python#mypy#Handle(buffer, lines) abort
" Look for lines like the following:
"
" file.py:4: error: No library stub file for module 'django.db'
"
" Lines like these should be ignored below:
"
" file.py:4: note: (Stub files are from https://github.com/python/typeshed)
let l:pattern = '^.\+:\(\d\+\):\?\(\d\+\)\?: \([^:]\+\): \(.\+\)$'
let l:output = []
for l:line in a:lines
let l:match = matchlist(l:line, l:pattern)
if len(l:match) == 0
continue
endif
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.
continue
endif
call add(l:output, {
\ 'bufnr': a:buffer,
\ 'lnum': l:match[1] + 0,
\ 'vcol': 0,
\ 'col': l:match[2] + 0,
\ 'text': l:match[4],
\ 'type': l:match[3] =~# 'error' ? 'E' : 'W',
\ 'nr': -1,
\})
endfor
return l:output
endfunction
call g:ale#linter#Define('python', { call g:ale#linter#Define('python', {
\ 'name': 'mypy', \ 'name': 'mypy',
\ 'executable': 'mypy', \ 'executable': 'mypy',
\ 'command_callback': 'ale_linters#python#mypy#GetCommand', \ 'command_callback': 'ale_linters#python#mypy#GetCommand',
\ 'callback': 'ale#handlers#HandleGCCFormat', \ 'callback': 'ale_linters#python#mypy#Handle',
\}) \})

View file

@ -44,6 +44,20 @@ function! ale#util#FindNearestFile(buffer, filename) abort
return '' return ''
endfunction endfunction
" Given a buffer and a directory name, find the nearest directory by searching upwards
" through the paths relative to the given buffer.
function! ale#util#FindNearestDirectory(buffer, directory_name) abort
let l:buffer_filename = fnamemodify(bufname(a:buffer), ':p')
let l:relative_path = finddir(a:directory_name, l:buffer_filename . ';')
if !empty(l:relative_path)
return fnamemodify(l:relative_path, ':p')
endif
return ''
endfunction
" Given a buffer, a string to search for, an a global fallback for when " Given a buffer, a string to search for, an a global fallback for when
" the search fails, look for a file in parent paths, and if that fails, " the search fails, look for a file in parent paths, and if that fails,
" use the global fallback path instead. " use the global fallback path instead.

View file

@ -0,0 +1,15 @@
Execute(Open a file some directory down):
silent! cd /testplugin/test
:e! top/middle/bottom/dummy.txt
Then(We should be able to find the right directory):
AssertEqual
\ expand('%:p:h:h:h:h') . '/top/ale-special-directory-name-dont-use-this-please/',
\ ale#util#FindNearestDirectory(bufnr('%'), 'ale-special-directory-name-dont-use-this-please')
Execute(Do nothing):
Then(We shouldn't find anything for files which don't match):
AssertEqual
\ '',
\ ale#util#FindNearestDirectory(bufnr('%'), 'ale-this-should-never-match-anything')

View file

@ -0,0 +1,22 @@
Execute(The mypy handler should parse lines correctly):
runtime ale_linters/python/mypy.vim
AssertEqual
\ [
\ {
\ 'bufnr': 347,
\ 'lnum': 4,
\ 'vcol': 0,
\ 'col': 0,
\ 'text': "No library stub file for module 'django.db'",
\ 'type': 'E',
\ 'nr': -1,
\ },
\ ],
\ ale_linters#python#mypy#Handle(347, [
\ "file.py:4: error: No library stub file for module 'django.db'",
\ 'file.py:4: note: (Stub files are from https://github.com/python/typeshed)',
\ ])
After:
call ale#linter#Reset()

View file

@ -2,7 +2,7 @@ Execute(Open a file some directory down):
silent! cd /testplugin/test silent! cd /testplugin/test
:e! top/middle/bottom/dummy.txt :e! top/middle/bottom/dummy.txt
Then(We should be able to to find the local version of a file): Then(We should be able to find the local version of a file):
AssertEqual AssertEqual
\ expand('%:p:h:h:h:h') . '/top/example.ini', \ expand('%:p:h:h:h:h') . '/top/example.ini',
\ ale#util#ResolveLocalPath(bufnr('%'), 'example.ini', '/global/config.ini') \ ale#util#ResolveLocalPath(bufnr('%'), 'example.ini', '/global/config.ini')