All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 " Author: w0rp <dev@w0rp.com>
2 " Description: Functions for integrating with Python linters.
4 call ale#Set('python_auto_pipenv', '0')
5 call ale#Set('python_auto_poetry', '0')
6 call ale#Set('python_auto_uv', '0')
8 let s:sep = has('win32') ? '\' : '/'
9 " bin is used for Unix virtualenv directories, and Scripts is for Windows.
10 let s:bin_dir = has('unix') ? 'bin' : 'Scripts'
11 " The default virtualenv directory names are ordered from the likely most
12 " common names down to the least common. `.env` might be more common, but it's
13 " also likely to conflict with a `.env` file for environment variables, so we
14 " search for it last. (People really shouldn't use that name.)
15 let g:ale_virtualenv_dir_names = get(g:, 'ale_virtualenv_dir_names', [
24 function! ale#python#FindProjectRootIni(buffer) abort
25 for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
26 " If you change this, update ale-python-root documentation.
27 if filereadable(l:path . '/MANIFEST.in')
28 \|| filereadable(l:path . '/setup.cfg')
29 \|| filereadable(l:path . '/pytest.ini')
30 \|| filereadable(l:path . '/tox.ini')
31 \|| filereadable(l:path . '/.pyre_configuration.local')
32 \|| filereadable(l:path . '/mypy.ini')
33 \|| filereadable(l:path . '/.mypy.ini')
34 \|| filereadable(l:path . '/pycodestyle.cfg')
35 \|| filereadable(l:path . '/.flake8')
36 \|| filereadable(l:path . '/.flake8rc')
37 \|| filereadable(l:path . '/pylama.ini')
38 \|| filereadable(l:path . '/pylintrc')
39 \|| filereadable(l:path . '/.pylintrc')
40 \|| filereadable(l:path . '/pyrightconfig.json')
41 \|| filereadable(l:path . '/pyrightconfig.toml')
42 \|| filereadable(l:path . '/Pipfile')
43 \|| filereadable(l:path . '/Pipfile.lock')
44 \|| filereadable(l:path . '/poetry.lock')
45 \|| filereadable(l:path . '/pyproject.toml')
46 \|| filereadable(l:path . '/.tool-versions')
47 \|| filereadable(l:path . '/uv.lock')
55 " Given a buffer number, find the project root directory for Python.
56 " The root directory is defined as the first directory found while searching
57 " upwards through paths, including the current directory, until a path
58 " containing an init file (one from MANIFEST.in, setup.cfg, pytest.ini,
59 " tox.ini) is found. If it is not possible to find the project root directory
60 " via init file, then it will be defined as the first directory found
61 " searching upwards through paths, including the current directory, until no
62 " __init__.py files is found.
63 function! ale#python#FindProjectRoot(buffer) abort
64 let l:ini_root = ale#python#FindProjectRootIni(a:buffer)
70 for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
71 if !filereadable(l:path . '/__init__.py')
79 " Given a buffer number, find a virtualenv path for Python.
80 function! ale#python#FindVirtualenv(buffer) abort
81 for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
82 " Skip empty path components returned in MSYS.
87 for l:dirname in ale#Var(a:buffer, 'virtualenv_dir_names')
88 let l:venv_dir = ale#path#Simplify(
89 \ join([l:path, l:dirname], s:sep)
91 let l:script_filename = ale#path#Simplify(
92 \ join([l:venv_dir, s:bin_dir, 'activate'], s:sep)
95 if filereadable(l:script_filename)
104 " Automatically determine virtualenv environment variables and build
105 " a string of them to prefix linter commands with.
106 function! ale#python#AutoVirtualenvEnvString(buffer) abort
107 let l:venv_dir = ale#python#FindVirtualenv(a:buffer)
109 if !empty(l:venv_dir)
112 let l:pathdir = join([l:venv_dir, s:bin_dir], s:sep)
114 " expand PATH correctly inside of the appropriate shell.
115 " set VIRTUAL_ENV to point to venv
117 call add(l:strs, 'set PATH=' . ale#Escape(l:pathdir) . ';%PATH% && ')
118 call add(l:strs, 'set VIRTUAL_ENV=' . ale#Escape(l:venv_dir) . ' && ')
120 call add(l:strs, 'PATH=' . ale#Escape(l:pathdir) . '":$PATH" ')
121 call add(l:strs, 'VIRTUAL_ENV=' . ale#Escape(l:venv_dir) . ' ')
124 return join(l:strs, '')
130 " Given a buffer number and a command name, find the path to the executable.
131 " First search on a virtualenv for Python, if nothing is found, try the global
132 " command. Returns an empty string if cannot find the executable
133 function! ale#python#FindExecutable(buffer, base_var_name, path_list) abort
134 if ale#Var(a:buffer, a:base_var_name . '_use_global')
135 return ale#Var(a:buffer, a:base_var_name . '_executable')
138 let l:virtualenv = ale#python#FindVirtualenv(a:buffer)
140 if !empty(l:virtualenv)
141 for l:path in a:path_list
142 let l:ve_executable = ale#path#Simplify(
143 \ join([l:virtualenv, s:bin_dir, l:path], s:sep)
146 if executable(l:ve_executable)
147 return l:ve_executable
152 return ale#Var(a:buffer, a:base_var_name . '_executable')
155 " Handle traceback.print_exception() output starting in the first a:limit lines.
156 function! ale#python#HandleTraceback(lines, limit) abort
157 let l:nlines = len(a:lines)
158 let l:limit = a:limit > l:nlines ? l:nlines : a:limit
161 while l:start < l:limit
162 if a:lines[l:start] is# 'Traceback (most recent call last):'
169 if l:start >= l:limit
173 let l:end = l:start + 1
175 " Traceback entries are always prefixed with 2 spaces.
176 " SyntaxError marker (if present) is prefixed with at least 4 spaces.
177 " Final exc line starts with exception class name (never a space).
178 while l:end < l:nlines && a:lines[l:end][0] is# ' '
182 let l:exc_line = l:end < l:nlines
184 \ : 'An exception was thrown.'
188 \ 'text': l:exc_line . ' (See :ALEDetail)',
189 \ 'detail': join(a:lines[(l:start):(l:end)], "\n"),
193 " Detects whether a pipenv environment is present.
194 function! ale#python#PipenvPresent(buffer) abort
195 return findfile('Pipfile.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
198 " Detects whether a poetry environment is present.
199 function! ale#python#PoetryPresent(buffer) abort
200 return findfile('poetry.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''
203 " Detects whether a poetry environment is present.
204 function! ale#python#UvPresent(buffer) abort
205 return findfile('uv.lock', expand('#' . a:buffer . ':p:h') . ';') isnot# ''