1212import shlex
1313
1414from pylsp import hookimpl , lsp
15+ from pylsp .config .config import Config
16+ from pylsp .workspace import Document , Workspace
17+ from pylsp .lsp import Diagnostic , Range
1518
1619try :
1720 import ujson as json
4548
4649
4750class PylintLinter :
48- last_diags = collections .defaultdict (list )
51+ last_diags : dict [ str , list [ Diagnostic ]] = collections .defaultdict (list )
4952
5053 @classmethod
5154 def lint (
52- cls , document , is_saved , flags = ""
53- ): # pylint: disable=too-many-locals,too-many-branches
55+ cls , document : Document , is_saved : bool , flags = ""
56+ ) -> list [ Diagnostic ] : # pylint: disable=too-many-locals,too-many-branches
5457 """Plugin interface to pylsp linter.
5558
5659 Args:
@@ -137,12 +140,12 @@ def lint(
137140 # * fatal
138141 # * refactor
139142 # * warning
140- diagnostics = []
143+ diagnostics : list [ Diagnostic ] = []
141144 for diag in json .loads (json_out ):
142145 # pylint lines index from 1, pylsp lines index from 0
143146 line = diag ["line" ] - 1
144147
145- err_range = {
148+ err_range : Range = {
146149 "start" : {
147150 "line" : line ,
148151 # Index columns start from 0
@@ -169,14 +172,15 @@ def lint(
169172 elif diag ["type" ] == "warning" :
170173 severity = lsp .DiagnosticSeverity .Warning
171174
172- code = diag ["message-id" ]
175+ code : str = diag ["message-id" ]
173176
174- diagnostic = {
177+ diagnostic : Diagnostic = {
175178 "source" : "pylint" ,
176179 "range" : err_range ,
177180 "message" : "[{}] {}" .format (diag ["symbol" ], diag ["message" ]),
178181 "severity" : severity ,
179182 "code" : code ,
183+ "tags" : [],
180184 }
181185
182186 if code in UNNECESSITY_CODES :
@@ -189,7 +193,7 @@ def lint(
189193 return diagnostics
190194
191195
192- def _build_pylint_flags (settings ) :
196+ def _build_pylint_flags (settings : dict ) -> str :
193197 """Build arguments for calling pylint."""
194198 pylint_args = settings .get ("args" )
195199 if pylint_args is None :
@@ -198,7 +202,7 @@ def _build_pylint_flags(settings):
198202
199203
200204@hookimpl
201- def pylsp_settings ():
205+ def pylsp_settings () -> dict :
202206 # Default pylint to disabled because it requires a config
203207 # file to be useful.
204208 return {
@@ -214,22 +218,24 @@ def pylsp_settings():
214218
215219
216220@hookimpl
217- def pylsp_lint (config , workspace , document , is_saved ):
221+ def pylsp_lint (
222+ config : Config , workspace : Workspace , document : Document , is_saved : bool
223+ ) -> list [Diagnostic ]:
218224 """Run pylint linter."""
219225 with workspace .report_progress ("lint: pylint" ):
220226 settings = config .plugin_settings ("pylint" )
221227 log .debug ("Got pylint settings: %s" , settings )
222228 # pylint >= 2.5.0 is required for working through stdin and only
223229 # available with python3
224230 if settings .get ("executable" ) and sys .version_info [0 ] >= 3 :
225- flags = build_args_stdio (settings )
231+ flag_list = build_args_stdio (settings )
226232 pylint_executable = settings .get ("executable" , "pylint" )
227- return pylint_lint_stdin (pylint_executable , document , flags )
233+ return pylint_lint_stdin (pylint_executable , document , flag_list )
228234 flags = _build_pylint_flags (settings )
229235 return PylintLinter .lint (document , is_saved , flags = flags )
230236
231237
232- def build_args_stdio (settings ) :
238+ def build_args_stdio (settings : dict ) -> list [ str ] :
233239 """Build arguments for calling pylint.
234240
235241 :param settings: client settings
@@ -244,7 +250,11 @@ def build_args_stdio(settings):
244250 return pylint_args
245251
246252
247- def pylint_lint_stdin (pylint_executable , document , flags ):
253+ def pylint_lint_stdin (
254+ pylint_executable : str ,
255+ document : Document ,
256+ flags : list [str ],
257+ ) -> list [Diagnostic ]:
248258 """Run pylint linter from stdin.
249259
250260 This runs pylint in a subprocess with popen.
@@ -265,7 +275,9 @@ def pylint_lint_stdin(pylint_executable, document, flags):
265275 return _parse_pylint_stdio_result (document , pylint_result )
266276
267277
268- def _run_pylint_stdio (pylint_executable , document , flags ):
278+ def _run_pylint_stdio (
279+ pylint_executable : str , document : Document , flags : list [str ]
280+ ) -> str :
269281 """Run pylint in popen.
270282
271283 :param pylint_executable: path to pylint executable
@@ -298,7 +310,7 @@ def _run_pylint_stdio(pylint_executable, document, flags):
298310 return stdout .decode ()
299311
300312
301- def _parse_pylint_stdio_result (document , stdout ) :
313+ def _parse_pylint_stdio_result (document : Document , stdout : str ) -> list [ Diagnostic ] :
302314 """Parse pylint results.
303315
304316 :param document: document to run pylint on
@@ -309,20 +321,20 @@ def _parse_pylint_stdio_result(document, stdout):
309321 :return: linting diagnostics
310322 :rtype: list
311323 """
312- diagnostics = []
324+ diagnostics : list [ Diagnostic ] = []
313325 lines = stdout .splitlines ()
314326 for raw_line in lines :
315327 parsed_line = re .match (r"(.*):(\d*):(\d*): (\w*): (.*)" , raw_line )
316328 if not parsed_line :
317329 log .debug ("Pylint output parser can't parse line '%s'" , raw_line )
318330 continue
319331
320- parsed_line = parsed_line .groups ()
321- if len (parsed_line ) != 5 :
332+ parsed_line_groups = parsed_line .groups ()
333+ if len (parsed_line_groups ) != 5 :
322334 log .debug ("Pylint output parser can't parse line '%s'" , raw_line )
323335 continue
324336
325- _ , line , character , code , msg = parsed_line
337+ _ , line , character , code , msg = parsed_line_groups
326338 line = int (line ) - 1
327339 character = int (character )
328340 severity_map = {
@@ -334,7 +346,7 @@ def _parse_pylint_stdio_result(document, stdout):
334346 "W" : lsp .DiagnosticSeverity .Warning ,
335347 }
336348 severity = severity_map [code [0 ]]
337- diagnostic = {
349+ diagnostic : Diagnostic = {
338350 "source" : "pylint" ,
339351 "code" : code ,
340352 "range" : {
@@ -347,6 +359,7 @@ def _parse_pylint_stdio_result(document, stdout):
347359 },
348360 "message" : msg ,
349361 "severity" : severity ,
362+ "tags" : [],
350363 }
351364 if code in UNNECESSITY_CODES :
352365 diagnostic ["tags" ] = [lsp .DiagnosticTag .Unnecessary ]
0 commit comments