2017-01-20 12:30:23 -05:00
|
|
|
" Author: Keith Smiley <k@keith.so>, w0rp <devw0rp@gmail.com>
|
2016-12-30 19:06:49 -05:00
|
|
|
" Description: mypy support for optional python typechecking
|
|
|
|
|
2018-01-11 11:43:10 -05:00
|
|
|
call ale#Set('python_mypy_executable', 'mypy')
|
|
|
|
call ale#Set('python_mypy_ignore_invalid_syntax', 0)
|
|
|
|
call ale#Set('python_mypy_options', '')
|
2018-04-09 14:11:20 -04:00
|
|
|
call ale#Set('python_mypy_use_global', get(g:, 'ale_use_global_executables', 0))
|
2017-05-06 18:19:54 -04:00
|
|
|
|
|
|
|
function! ale_linters#python#mypy#GetExecutable(buffer) abort
|
2017-07-05 08:07:55 -04:00
|
|
|
return ale#python#FindExecutable(a:buffer, 'python_mypy', ['mypy'])
|
2017-05-06 18:19:54 -04:00
|
|
|
endfunction
|
2016-12-30 19:06:49 -05:00
|
|
|
|
2017-08-20 12:43:42 -04:00
|
|
|
" The directory to change to before running mypy
|
|
|
|
function! s:GetDir(buffer) abort
|
2017-05-06 18:19:54 -04:00
|
|
|
let l:project_root = ale#python#FindProjectRoot(a:buffer)
|
2017-08-20 12:43:42 -04:00
|
|
|
|
|
|
|
return !empty(l:project_root)
|
|
|
|
\ ? l:project_root
|
|
|
|
\ : expand('#' . a:buffer . ':p:h')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale_linters#python#mypy#GetCommand(buffer) abort
|
|
|
|
let l:dir = s:GetDir(a:buffer)
|
2017-05-06 18:19:54 -04:00
|
|
|
let l:executable = ale_linters#python#mypy#GetExecutable(a:buffer)
|
2017-01-20 12:30:23 -05:00
|
|
|
|
2017-08-20 12:43:42 -04:00
|
|
|
" We have to always switch to an explicit directory for a command so
|
|
|
|
" we can know with certainty the base path for the 'filename' keys below.
|
|
|
|
return ale#path#CdString(l:dir)
|
2017-05-12 04:20:16 -04:00
|
|
|
\ . ale#Escape(l:executable)
|
2017-05-06 18:19:54 -04:00
|
|
|
\ . ' --show-column-numbers '
|
2017-04-15 20:24:08 -04:00
|
|
|
\ . ale#Var(a:buffer, 'python_mypy_options')
|
2017-06-27 05:06:03 -04:00
|
|
|
\ . ' --shadow-file %s %t %s'
|
2016-12-30 19:06:49 -05:00
|
|
|
endfunction
|
|
|
|
|
2017-04-15 20:24:08 -04:00
|
|
|
function! ale_linters#python#mypy#Handle(buffer, lines) abort
|
2017-08-20 12:43:42 -04:00
|
|
|
let l:dir = s:GetDir(a:buffer)
|
2017-01-20 12:30:23 -05:00
|
|
|
" 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)
|
2017-05-06 18:19:54 -04:00
|
|
|
let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):?(\d+)?: (error|warning): (.+)$'
|
2017-01-20 12:30:23 -05:00
|
|
|
let l:output = []
|
|
|
|
|
2017-04-17 19:35:53 -04:00
|
|
|
for l:match in ale#util#GetMatches(a:lines, l:pattern)
|
2018-01-11 11:43:10 -05:00
|
|
|
" Skip invalid syntax errors if the option is on.
|
|
|
|
if l:match[5] is# 'invalid syntax'
|
|
|
|
\&& ale#Var(a:buffer, 'python_mypy_ignore_invalid_syntax')
|
|
|
|
continue
|
|
|
|
endif
|
|
|
|
|
2017-01-20 12:30:23 -05:00
|
|
|
call add(l:output, {
|
2017-08-20 12:43:42 -04:00
|
|
|
\ 'filename': ale#path#GetAbsPath(l:dir, l:match[1]),
|
2017-05-06 18:19:54 -04:00
|
|
|
\ 'lnum': l:match[2] + 0,
|
|
|
|
\ 'col': l:match[3] + 0,
|
2017-08-20 12:43:42 -04:00
|
|
|
\ 'type': l:match[4] is# 'error' ? 'E' : 'W',
|
2017-05-06 18:19:54 -04:00
|
|
|
\ 'text': l:match[5],
|
2017-01-20 12:30:23 -05:00
|
|
|
\})
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return l:output
|
|
|
|
endfunction
|
|
|
|
|
2017-04-15 08:35:54 -04:00
|
|
|
call ale#linter#Define('python', {
|
2016-12-30 19:06:49 -05:00
|
|
|
\ 'name': 'mypy',
|
2017-05-06 18:19:54 -04:00
|
|
|
\ 'executable_callback': 'ale_linters#python#mypy#GetExecutable',
|
2016-12-30 19:06:49 -05:00
|
|
|
\ 'command_callback': 'ale_linters#python#mypy#GetCommand',
|
2017-01-20 12:30:23 -05:00
|
|
|
\ 'callback': 'ale_linters#python#mypy#Handle',
|
2016-12-30 19:06:49 -05:00
|
|
|
\})
|