1
0
Fork 0
mirror of synced 2024-11-15 21:48:57 -05:00
ultimate-vim/sources_non_forked/ale/ale_linters/python/mypy.vim

104 lines
3.5 KiB
VimL
Raw Normal View History

" Author: Keith Smiley <k@keith.so>, w0rp <devw0rp@gmail.com>
" Description: mypy support for optional python typechecking
call ale#Set('python_mypy_executable', 'mypy')
call ale#Set('python_mypy_ignore_invalid_syntax', 0)
2020-01-07 07:45:07 -05:00
call ale#Set('python_mypy_show_notes', 1)
call ale#Set('python_mypy_options', '')
2018-06-14 06:31:12 -04:00
call ale#Set('python_mypy_use_global', get(g:, 'ale_use_global_executables', 0))
2018-09-24 20:40:17 -04:00
call ale#Set('python_mypy_auto_pipenv', 0)
2021-07-30 16:52:54 -04:00
call ale#Set('python_mypy_auto_poetry', 0)
function! ale_linters#python#mypy#GetExecutable(buffer) abort
2018-09-24 20:40:17 -04:00
if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_mypy_auto_pipenv'))
\ && ale#python#PipenvPresent(a:buffer)
return 'pipenv'
endif
2021-07-30 16:52:54 -04:00
if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'python_mypy_auto_poetry'))
\ && ale#python#PoetryPresent(a:buffer)
return 'poetry'
endif
return ale#python#FindExecutable(a:buffer, 'python_mypy', ['mypy'])
endfunction
" The directory to change to before running mypy
2021-05-05 04:25:00 -04:00
function! ale_linters#python#mypy#GetCwd(buffer) abort
2020-01-07 07:45:07 -05:00
" If we find a directory with "mypy.ini" in it use that,
" else try and find the "python project" root, or failing
" that, run from the same folder as the current file
for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
if filereadable(l:path . '/mypy.ini')
return l:path
endif
endfor
let l:project_root = ale#python#FindProjectRoot(a:buffer)
return !empty(l:project_root)
\ ? l:project_root
\ : expand('#' . a:buffer . ':p:h')
endfunction
function! ale_linters#python#mypy#GetCommand(buffer) abort
let l:executable = ale_linters#python#mypy#GetExecutable(a:buffer)
2021-07-30 16:52:54 -04:00
let l:exec_args = l:executable =~? 'pipenv\|poetry$'
2018-06-14 06:31:12 -04:00
\ ? ' run mypy'
\ : ''
2021-05-05 04:25:00 -04:00
return '%e' . l:exec_args
\ . ale#Pad(ale#Var(a:buffer, 'python_mypy_options'))
\ . ' --show-column-numbers'
\ . ' --shadow-file %s %t %s'
endfunction
function! ale_linters#python#mypy#Handle(buffer, lines) abort
2021-05-05 04:25:00 -04:00
let l:dir = ale_linters#python#mypy#GetCwd(a:buffer)
" 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)
2020-01-07 07:45:07 -05:00
let l:types = 'error|warning'
if ale#Var(a:buffer, 'python_mypy_show_notes')
let l:types = 'error|warning|note'
endif
let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):?(\d+)?: ('
\ . l:types
\ . '): (.+)$'
let l:output = []
for l:match in ale#util#GetMatches(a:lines, l:pattern)
" 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
call add(l:output, {
\ 'filename': ale#path#GetAbsPath(l:dir, l:match[1]),
\ 'lnum': l:match[2] + 0,
\ 'col': l:match[3] + 0,
2020-01-07 07:45:07 -05:00
\ 'type': l:match[4] is# 'error' ? 'E' : (l:match[4] is# 'note' ? 'I': 'W'),
\ 'text': l:match[5],
\})
endfor
return l:output
endfunction
call ale#linter#Define('python', {
\ 'name': 'mypy',
2019-03-08 06:04:56 -05:00
\ 'executable': function('ale_linters#python#mypy#GetExecutable'),
2021-05-05 04:25:00 -04:00
\ 'cwd': function('ale_linters#python#mypy#GetCwd'),
2019-03-08 06:04:56 -05:00
\ 'command': function('ale_linters#python#mypy#GetCommand'),
\ 'callback': 'ale_linters#python#mypy#Handle',
2019-08-22 11:36:17 -04:00
\ 'output_stream': 'both'
\})