@@ -54,6 +54,7 @@ class PylintConfig(TypedDict):
54
54
enabled : Optional [bool ]
55
55
args : Optional [list [str ]]
56
56
executable : Optional [str ]
57
+ extra_paths : Optional [list [str ]]
57
58
58
59
59
60
class PylintLinter :
@@ -218,6 +219,7 @@ def pylsp_settings() -> dict:
218
219
pylint_config : PylintConfig = {
219
220
"enabled" : False ,
220
221
"args" : [],
222
+ "extra_paths" : None ,
221
223
# disabled by default as it can slow down the workflow
222
224
"executable" : None ,
223
225
}
@@ -234,10 +236,13 @@ def pylsp_lint(
234
236
log .debug ("Got pylint settings: %s" , settings )
235
237
# pylint >= 2.5.0 is required for working through stdin and only
236
238
# available with python3
239
+ extra_paths = settings ["extra_paths" ] or []
237
240
if settings .get ("executable" ) and sys .version_info [0 ] >= 3 :
238
241
flag_list = build_args_stdio (settings )
239
242
pylint_executable = settings ["executable" ] or "pylint"
240
- return pylint_lint_stdin (pylint_executable , document , flag_list )
243
+ return pylint_lint_stdin (
244
+ pylint_executable , document , flag_list , extra_paths
245
+ )
241
246
flags = _build_pylint_flags (settings )
242
247
return PylintLinter .lint (document , is_saved , flags = flags )
243
248
@@ -261,6 +266,7 @@ def pylint_lint_stdin(
261
266
pylint_executable : str ,
262
267
document : Document ,
263
268
flags : list [str ],
269
+ extra_paths : list [str ],
264
270
) -> list [Diagnostic ]:
265
271
"""Run pylint linter from stdin.
266
272
@@ -278,12 +284,12 @@ def pylint_lint_stdin(
278
284
:return: linting diagnostics
279
285
:rtype: list
280
286
"""
281
- pylint_result = _run_pylint_stdio (pylint_executable , document , flags )
287
+ pylint_result = _run_pylint_stdio (pylint_executable , document , flags , extra_paths )
282
288
return _parse_pylint_stdio_result (document , pylint_result )
283
289
284
290
285
291
def _run_pylint_stdio (
286
- pylint_executable : str , document : Document , flags : list [str ]
292
+ pylint_executable : str , document : Document , flags : list [str ], extra_paths : list [ str ]
287
293
) -> str :
288
294
"""Run pylint in popen.
289
295
@@ -302,14 +308,28 @@ def _run_pylint_stdio(
302
308
cmd = [pylint_executable ]
303
309
cmd .extend (flags )
304
310
cmd .extend (["--from-stdin" , document .path ])
305
- p = Popen (cmd , stdin = PIPE , stdout = PIPE , stderr = PIPE )
311
+ workspace_root = document ._workspace .root_path
312
+ python_paths = [workspace_root ] + [
313
+ os .path .join (workspace_root , path ) for path in extra_paths
314
+ ]
315
+ p = Popen (
316
+ cmd ,
317
+ stdin = PIPE ,
318
+ stdout = PIPE ,
319
+ stderr = PIPE ,
320
+ env = {"PYTHONPATH" : ":" .join (python_paths )},
321
+ ) # pylint: disable=consider-using-with
306
322
except IOError :
307
323
log .debug ("Can't execute %s. Trying with 'python -m pylint'" , pylint_executable )
308
324
cmd = [sys .executable , "-m" , "pylint" ]
309
325
cmd .extend (flags )
310
326
cmd .extend (["--from-stdin" , document .path ])
311
327
p = Popen ( # pylint: disable=consider-using-with
312
- cmd , stdin = PIPE , stdout = PIPE , stderr = PIPE
328
+ cmd ,
329
+ stdin = PIPE ,
330
+ stdout = PIPE ,
331
+ stderr = PIPE ,
332
+ env = {"PYTHONPATH" : ":" .join (python_paths )},
313
333
)
314
334
(stdout , stderr ) = p .communicate (document .source .encode ())
315
335
if stderr :
0 commit comments