2017-01-20 17:30:23 +00:00
|
|
|
" Author: Keith Smiley <k@keith.so>, w0rp <devw0rp@gmail.com>
|
2016-12-31 00:06:49 +00:00
|
|
|
" Description: mypy support for optional python typechecking
|
|
|
|
|
2018-01-11 16:43:10 +00:00
|
|
|
call ale#Set('python_mypy_executable', 'mypy')
|
|
|
|
call ale#Set('python_mypy_ignore_invalid_syntax', 0)
|
2020-01-02 14:19:21 +00:00
|
|
|
call ale#Set('python_mypy_show_notes', 1)
|
2018-01-11 16:43:10 +00:00
|
|
|
call ale#Set('python_mypy_options', '')
|
2018-04-09 18:11:20 +00:00
|
|
|
call ale#Set('python_mypy_use_global', get(g:, 'ale_use_global_executables', 0))
|
Add python_[linter]_auto_pipenv options for python linters (fixes #1656)
When set to true, and the buffer is currently inside a pipenv,
GetExecutable will return "pipenv", which will trigger the existing
functionality to append the correct pipenv arguments to run each linter.
Defaults to false.
I was going to implement ale#python#PipenvPresent by invoking
`pipenv --venv` or `pipenv --where`, but it seemed to be abominably
slow, even to the point where the test suite wasn't even finishing
("Tried to run tests 3 times"). The diff is:
diff --git a/autoload/ale/python.vim b/autoload/ale/python.vim
index 7baae079..8c100d41 100644
--- a/autoload/ale/python.vim
+++ b/autoload/ale/python.vim
@@ -106,5 +106,9 @@ endfunction
" Detects whether a pipenv environment is present.
function! ale#python#PipenvPresent(buffer) abort
- return findfile('Pipfile.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
+ let l:cd_string = ale#path#BufferCdString(a:buffer)
+ let l:output = systemlist(l:cd_string . 'pipenv --where')[0]
+ " `pipenv --where` returns the path to the dir containing the Pipfile
+ " if in a pipenv, or some error text otherwise.
+ return strpart(l:output, 0, 18) !=# "No Pipfile present"
endfunction
Using vim's `findfile` is much faster, behaves correctly in the majority
of situations, and also works reliably when the `pipenv` command doesn't
exist.
2018-07-12 03:02:23 +00:00
|
|
|
call ale#Set('python_mypy_auto_pipenv', 0)
|
2017-05-06 22:19:54 +00:00
|
|
|
|
|
|
|
function! ale_linters#python#mypy#GetExecutable(buffer) abort
|
2018-09-16 01:38:26 +00:00
|
|
|
if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'python_mypy_auto_pipenv'))
|
|
|
|
\ && ale#python#PipenvPresent(a:buffer)
|
Add python_[linter]_auto_pipenv options for python linters (fixes #1656)
When set to true, and the buffer is currently inside a pipenv,
GetExecutable will return "pipenv", which will trigger the existing
functionality to append the correct pipenv arguments to run each linter.
Defaults to false.
I was going to implement ale#python#PipenvPresent by invoking
`pipenv --venv` or `pipenv --where`, but it seemed to be abominably
slow, even to the point where the test suite wasn't even finishing
("Tried to run tests 3 times"). The diff is:
diff --git a/autoload/ale/python.vim b/autoload/ale/python.vim
index 7baae079..8c100d41 100644
--- a/autoload/ale/python.vim
+++ b/autoload/ale/python.vim
@@ -106,5 +106,9 @@ endfunction
" Detects whether a pipenv environment is present.
function! ale#python#PipenvPresent(buffer) abort
- return findfile('Pipfile.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
+ let l:cd_string = ale#path#BufferCdString(a:buffer)
+ let l:output = systemlist(l:cd_string . 'pipenv --where')[0]
+ " `pipenv --where` returns the path to the dir containing the Pipfile
+ " if in a pipenv, or some error text otherwise.
+ return strpart(l:output, 0, 18) !=# "No Pipfile present"
endfunction
Using vim's `findfile` is much faster, behaves correctly in the majority
of situations, and also works reliably when the `pipenv` command doesn't
exist.
2018-07-12 03:02:23 +00:00
|
|
|
return 'pipenv'
|
|
|
|
endif
|
|
|
|
|
2017-07-05 12:07:55 +00:00
|
|
|
return ale#python#FindExecutable(a:buffer, 'python_mypy', ['mypy'])
|
2017-05-06 22:19:54 +00:00
|
|
|
endfunction
|
2016-12-31 00:06:49 +00:00
|
|
|
|
2017-08-20 16:43:42 +00:00
|
|
|
" The directory to change to before running mypy
|
|
|
|
function! s:GetDir(buffer) abort
|
2020-01-02 14:35:21 +00: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
|
|
|
|
|
2017-05-06 22:19:54 +00:00
|
|
|
let l:project_root = ale#python#FindProjectRoot(a:buffer)
|
2017-08-20 16:43:42 +00: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 22:19:54 +00:00
|
|
|
let l:executable = ale_linters#python#mypy#GetExecutable(a:buffer)
|
2017-01-20 17:30:23 +00:00
|
|
|
|
2018-06-05 02:43:02 +00:00
|
|
|
let l:exec_args = l:executable =~? 'pipenv$'
|
|
|
|
\ ? ' run mypy'
|
|
|
|
\ : ''
|
|
|
|
|
2017-08-20 16:43:42 +00: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)
|
2018-06-05 02:43:02 +00:00
|
|
|
\ . ale#Escape(l:executable) . l:exec_args
|
2017-05-06 22:19:54 +00:00
|
|
|
\ . ' --show-column-numbers '
|
2017-04-16 00:24:08 +00:00
|
|
|
\ . ale#Var(a:buffer, 'python_mypy_options')
|
2017-06-27 09:06:03 +00:00
|
|
|
\ . ' --shadow-file %s %t %s'
|
2016-12-31 00:06:49 +00:00
|
|
|
endfunction
|
|
|
|
|
2017-04-16 00:24:08 +00:00
|
|
|
function! ale_linters#python#mypy#Handle(buffer, lines) abort
|
2017-08-20 16:43:42 +00:00
|
|
|
let l:dir = s:GetDir(a:buffer)
|
2017-01-20 17:30:23 +00: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)
|
2020-01-02 14:19:21 +00: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
|
|
|
|
\ . '): (.+)$'
|
2017-01-20 17:30:23 +00:00
|
|
|
let l:output = []
|
|
|
|
|
2017-04-17 23:35:53 +00:00
|
|
|
for l:match in ale#util#GetMatches(a:lines, l:pattern)
|
2018-01-11 16:43:10 +00: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 17:30:23 +00:00
|
|
|
call add(l:output, {
|
2017-08-20 16:43:42 +00:00
|
|
|
\ 'filename': ale#path#GetAbsPath(l:dir, l:match[1]),
|
2017-05-06 22:19:54 +00:00
|
|
|
\ 'lnum': l:match[2] + 0,
|
|
|
|
\ 'col': l:match[3] + 0,
|
2020-01-02 14:19:21 +00:00
|
|
|
\ 'type': l:match[4] is# 'error' ? 'E' : (l:match[4] is# 'note' ? 'I': 'W'),
|
2017-05-06 22:19:54 +00:00
|
|
|
\ 'text': l:match[5],
|
2017-01-20 17:30:23 +00:00
|
|
|
\})
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return l:output
|
|
|
|
endfunction
|
|
|
|
|
2017-04-15 12:35:54 +00:00
|
|
|
call ale#linter#Define('python', {
|
2016-12-31 00:06:49 +00:00
|
|
|
\ 'name': 'mypy',
|
2019-02-22 18:05:04 +00:00
|
|
|
\ 'executable': function('ale_linters#python#mypy#GetExecutable'),
|
|
|
|
\ 'command': function('ale_linters#python#mypy#GetCommand'),
|
2017-01-20 17:30:23 +00:00
|
|
|
\ 'callback': 'ale_linters#python#mypy#Handle',
|
2019-08-15 15:36:11 +00:00
|
|
|
\ 'output_stream': 'both'
|
2016-12-31 00:06:49 +00:00
|
|
|
\})
|