summaryrefslogtreecommitdiff
path: root/ale_linters/python/mypy.vim
blob: 1509d9e6d2c68778d39eb815ddd2f9b9f0bc03db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
" Author: Keith Smiley <k@keith.so>, w0rp <devw0rp@gmail.com>
" Description: mypy support for optional python typechecking

let g:ale_python_mypy_options = get(g:, 'ale_python_mypy_options', '')

function! g:ale_linters#python#mypy#GetCommand(buffer) abort
    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 '
    \   . g:ale_python_mypy_options
endfunction

let s:path_pattern = '[a-zA-Z]\?\\\?:\?[[:alnum:]/\.\-_]\+'

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 = '^' . s:path_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', {
\   'name': 'mypy',
\   'executable': 'mypy',
\   'command_callback': 'ale_linters#python#mypy#GetCommand',
\   'callback': 'ale_linters#python#mypy#Handle',
\})