From cc02eb8a5a0e40e23ae5e3cc2a4e92f0353d30b5 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 20 Aug 2017 17:43:42 +0100 Subject: [PATCH] #653 Show errors from other files for mypy --- ale_linters/python/mypy.vim | 27 +++++++++++++++---------- test/handler/test_mypy_handler.vader | 30 +++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/ale_linters/python/mypy.vim b/ale_linters/python/mypy.vim index 1d4efe89..6884a9ac 100644 --- a/ale_linters/python/mypy.vim +++ b/ale_linters/python/mypy.vim @@ -10,14 +10,22 @@ function! ale_linters#python#mypy#GetExecutable(buffer) abort return ale#python#FindExecutable(a:buffer, 'python_mypy', ['mypy']) endfunction -function! ale_linters#python#mypy#GetCommand(buffer) abort +" The directory to change to before running mypy +function! s:GetDir(buffer) abort let l:project_root = ale#python#FindProjectRoot(a:buffer) - let l:cd_command = !empty(l:project_root) - \ ? ale#path#CdString(l:project_root) - \ : '' + + 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) let l:executable = ale_linters#python#mypy#GetExecutable(a:buffer) - return l:cd_command + " 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) \ . ale#Escape(l:executable) \ . ' --show-column-numbers ' \ . ale#Var(a:buffer, 'python_mypy_options') @@ -25,6 +33,7 @@ function! ale_linters#python#mypy#GetCommand(buffer) abort endfunction function! ale_linters#python#mypy#Handle(buffer, lines) abort + let l:dir = s:GetDir(a:buffer) " Look for lines like the following: " " file.py:4: error: No library stub file for module 'django.db' @@ -34,17 +43,13 @@ function! ale_linters#python#mypy#Handle(buffer, lines) abort " file.py:4: note: (Stub files are from https://github.com/python/typeshed) let l:pattern = '\v^([a-zA-Z]?:?[^:]+):(\d+):?(\d+)?: (error|warning): (.+)$' let l:output = [] - let l:buffer_filename = expand('#' . a:buffer . ':p') for l:match in ale#util#GetMatches(a:lines, l:pattern) - if l:buffer_filename[-len(l:match[1]):] isnot# l:match[1] - 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, - \ 'type': l:match[4] =~# 'error' ? 'E' : 'W', + \ 'type': l:match[4] is# 'error' ? 'E' : 'W', \ 'text': l:match[5], \}) endfor diff --git a/test/handler/test_mypy_handler.vader b/test/handler/test_mypy_handler.vader index c949b1aa..d0cf91e2 100644 --- a/test/handler/test_mypy_handler.vader +++ b/test/handler/test_mypy_handler.vader @@ -11,16 +11,39 @@ Execute(The mypy handler should parse lines correctly): AssertEqual \ [ \ { + \ 'lnum': 768, + \ 'col': 38, + \ 'filename': 'foo/bar/foo/bar/baz.py', + \ 'type': 'E', + \ 'text': 'Cannot determine type of ''SOME_SYMBOL''', + \ }, + \ { + \ 'lnum': 821, + \ 'col': 38, + \ 'filename': 'foo/bar/foo/bar/baz.py', + \ 'type': 'E', + \ 'text': 'Cannot determine type of ''SOME_SYMBOL''', + \ }, + \ { + \ 'lnum': 38, + \ 'col': 44, + \ 'filename': 'foo/bar/foo/bar/other.py', + \ 'type': 'E', + \ 'text': 'Cannot determine type of ''ANOTHER_SYMBOL''', + \ }, + \ { \ 'lnum': 15, \ 'col': 3, - \ 'text': 'Argument 1 to "somefunc" has incompatible type "int"; expected "str"', + \ 'filename': 'foo/bar/foo/bar/__init__.py', \ 'type': 'E', + \ 'text': 'Argument 1 to "somefunc" has incompatible type "int"; expected "str"' \ }, \ { \ 'lnum': 72, \ 'col': 1, - \ 'text': 'Some warning', + \ 'filename': 'foo/bar/foo/bar/__init__.py', \ 'type': 'W', + \ 'text': 'Some warning', \ }, \ ], \ ale_linters#python#mypy#Handle(bufnr(''), [ @@ -47,8 +70,9 @@ Execute(The mypy handler should handle Windows names with spaces): \ { \ 'lnum': 4, \ 'col': 0, - \ 'text': "No library stub file for module 'django.db'", + \ 'filename': 'C:\something\with spaces.py', \ 'type': 'E', + \ 'text': 'No library stub file for module ''django.db''', \ }, \ ], \ ale_linters#python#mypy#Handle(bufnr(''), [