From 72d2c55479c29e550c27f20e903c882dcc68639b Mon Sep 17 00:00:00 2001 From: Harry Percival Date: Thu, 2 Jan 2020 14:35:21 +0000 Subject: [PATCH] Mypy: try to find folder containing mypy.ini to use as cwd. (#2385) * When deciding which directory to run mypy from, prefer a folder with mypy.ini in it * Add a test for mypy.ini-finding behaviour --- ale_linters/python/mypy.vim | 9 +++++++++ .../python_paths/with_mypy_ini_and_pytest_ini/mypy.ini | 0 .../with_mypy_ini_and_pytest_ini/tests/pytest.ini | 0 .../tests/testsubfolder/my_tests.py | 0 test/command_callback/test_mypy_command_callback.vader | 9 +++++++++ 5 files changed, 18 insertions(+) create mode 100644 test/command_callback/python_paths/with_mypy_ini_and_pytest_ini/mypy.ini create mode 100644 test/command_callback/python_paths/with_mypy_ini_and_pytest_ini/tests/pytest.ini create mode 100644 test/command_callback/python_paths/with_mypy_ini_and_pytest_ini/tests/testsubfolder/my_tests.py diff --git a/ale_linters/python/mypy.vim b/ale_linters/python/mypy.vim index d4778e40..94dfae7d 100644 --- a/ale_linters/python/mypy.vim +++ b/ale_linters/python/mypy.vim @@ -19,6 +19,15 @@ endfunction " The directory to change to before running mypy function! s:GetDir(buffer) abort + " 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) diff --git a/test/command_callback/python_paths/with_mypy_ini_and_pytest_ini/mypy.ini b/test/command_callback/python_paths/with_mypy_ini_and_pytest_ini/mypy.ini new file mode 100644 index 00000000..e69de29b diff --git a/test/command_callback/python_paths/with_mypy_ini_and_pytest_ini/tests/pytest.ini b/test/command_callback/python_paths/with_mypy_ini_and_pytest_ini/tests/pytest.ini new file mode 100644 index 00000000..e69de29b diff --git a/test/command_callback/python_paths/with_mypy_ini_and_pytest_ini/tests/testsubfolder/my_tests.py b/test/command_callback/python_paths/with_mypy_ini_and_pytest_ini/tests/testsubfolder/my_tests.py new file mode 100644 index 00000000..e69de29b diff --git a/test/command_callback/test_mypy_command_callback.vader b/test/command_callback/test_mypy_command_callback.vader index 8ca35207..afa9f9af 100644 --- a/test/command_callback/test_mypy_command_callback.vader +++ b/test/command_callback/test_mypy_command_callback.vader @@ -52,6 +52,15 @@ Execute(The mypy callbacks should detect virtualenv directories and switch to th \ . ' --show-column-numbers ' \ . '--shadow-file %s %t %s' +Execute(The mypy callbacks should cd to directory containing mypy.ini if found): + silent execute 'file ' . fnameescape(g:dir . '/python_paths/with_mypy_ini_and_pytest_ini/tests/testsubfolder/my_tests.py') + + AssertLinter 'mypy', + \ ale#path#CdString(ale#path#Simplify(g:dir . '/python_paths/with_mypy_ini_and_pytest_ini')) + \ . ale#Escape('mypy') + \ . ' --show-column-numbers ' + \ . '--shadow-file %s %t %s' + Execute(You should able able to use the global mypy instead): silent execute 'file ' . fnameescape(g:dir . '/python_paths/with_virtualenv/subdir/foo/bar.py') let g:ale_python_mypy_use_global = 1