11# -*- coding: utf-8 -*-
22"""
3- File that contains the pyls plugin mypy-ls.
3+ File that contains the pylsp plugin mypy-ls.
44
55Created on Fri Jul 10 09:53:57 2020
66
1212import os .path
1313import logging
1414from mypy import api as mypy_api
15- from pyls import hookimpl
16- from pyls .workspace import Document , Workspace
17- from pyls .config .config import Config
15+ from pylsp import hookimpl
16+ from pylsp .workspace import Document , Workspace
17+ from pylsp .config .config import Config
1818from typing import Optional , Dict , Any , IO , List
1919import atexit
2020
2727tmpFile : Optional [IO [str ]] = None
2828
2929
30- def parse_line (line : str , document : Optional [Document ] = None ) -> Optional [Dict [str , Any ]]:
30+ def parse_line (
31+ line : str , document : Optional [Document ] = None
32+ ) -> Optional [Dict [str , Any ]]:
3133 """
3234 Return a language-server diagnostic from a line of the Mypy error report.
3335
@@ -54,51 +56,53 @@ def parse_line(line: str, document: Optional[Document] = None) -> Optional[Dict[
5456 if file_path != "<string>" : # live mode
5557 # results from other files can be included, but we cannot return
5658 # them.
57- if document and document .path and not document .path .endswith (
58- file_path ):
59- log . warning ( "discarding result for %s against %s" , file_path ,
60- document . path )
59+ if document and document .path and not document .path .endswith (file_path ):
60+ log . warning (
61+ "discarding result for %s against %s" , file_path , document . path
62+ )
6163 return None
6264
6365 lineno = int (linenoStr or 1 ) - 1 # 0-based line number
6466 offset = int (offsetStr or 1 ) - 1 # 0-based offset
6567 errno = 2
66- if severity == ' error' :
68+ if severity == " error" :
6769 errno = 1
6870 diag : Dict [str , Any ] = {
69- ' source' : ' mypy' ,
70- ' range' : {
71- ' start' : {' line' : lineno , ' character' : offset },
71+ " source" : " mypy" ,
72+ " range" : {
73+ " start" : {" line" : lineno , " character" : offset },
7274 # There may be a better solution, but mypy does not provide end
73- ' end' : {' line' : lineno , ' character' : offset + 1 }
75+ " end" : {" line" : lineno , " character" : offset + 1 },
7476 },
75- ' message' : msg ,
76- ' severity' : errno
77+ " message" : msg ,
78+ " severity" : errno ,
7779 }
7880 if document :
7981 # although mypy does not provide the end of the affected range, we
8082 # can make a good guess by highlighting the word that Mypy flagged
81- word = document .word_at_position (diag [' range' ][ ' start' ])
83+ word = document .word_at_position (diag [" range" ][ " start" ])
8284 if word :
83- diag ['range' ]['end' ]['character' ] = (
84- diag ['range' ]['start' ]['character' ] + len (word ))
85+ diag ["range" ]["end" ]["character" ] = diag ["range" ]["start" ][
86+ "character"
87+ ] + len (word )
8588
8689 return diag
8790 return None
8891
8992
9093@hookimpl
91- def pyls_lint (config : Config , workspace : Workspace , document : Document ,
92- is_saved : bool ) -> List [Dict [str , Any ]]:
94+ def pylsp_lint (
95+ config : Config , workspace : Workspace , document : Document , is_saved : bool
96+ ) -> List [Dict [str , Any ]]:
9397 """
9498 Lints.
9599
96100 Parameters
97101 ----------
98102 config : Config
99- The pyls config.
103+ The pylsp config.
100104 workspace : Workspace
101- The pyls workspace.
105+ The pylsp workspace.
102106 document : Document
103107 The document to be linted.
104108 is_saved : bool
@@ -110,27 +114,25 @@ def pyls_lint(config: Config, workspace: Workspace, document: Document,
110114 List of the linting data.
111115
112116 """
113- settings = config .plugin_settings ('mypy-ls' )
114- live_mode = settings .get ('live_mode' , True )
115- args = ['--incremental' ,
116- '--show-column-numbers' ,
117- '--follow-imports' , 'silent' ]
117+ settings = config .plugin_settings ("mypy-ls" )
118+ live_mode = settings .get ("live_mode" , True )
119+ args = ["--incremental" , "--show-column-numbers" , "--follow-imports" , "silent" ]
118120
119121 global tmpFile
120122 if live_mode and not is_saved and tmpFile :
121123 tmpFile = open (tmpFile .name , "w" )
122124 tmpFile .write (document .source )
123125 tmpFile .close ()
124- args .extend ([' --shadow-file' , document .path , tmpFile .name ])
126+ args .extend ([" --shadow-file" , document .path , tmpFile .name ])
125127 elif not is_saved :
126128 return []
127129
128130 if mypyConfigFile :
129- args .append (' --config-file' )
131+ args .append (" --config-file" )
130132 args .append (mypyConfigFile )
131133 args .append (document .path )
132- if settings .get (' strict' , False ):
133- args .append (' --strict' )
134+ if settings .get (" strict" , False ):
135+ args .append (" --strict" )
134136
135137 report , errors , _ = mypy_api .run (args )
136138
@@ -144,14 +146,14 @@ def pyls_lint(config: Config, workspace: Workspace, document: Document,
144146
145147
146148@hookimpl
147- def pyls_settings (config : Config ) -> Dict [str , Dict [str , Dict [str , str ]]]:
149+ def pylsp_settings (config : Config ) -> Dict [str , Dict [str , Dict [str , str ]]]:
148150 """
149151 Read the settings.
150152
151153 Parameters
152154 ----------
153155 config : Config
154- The pyls config.
156+ The pylsp config.
155157
156158 Returns
157159 -------
@@ -187,10 +189,11 @@ def init(workspace: str) -> Dict[str, str]:
187189 configuration = eval (file .read ())
188190 global mypyConfigFile
189191 mypyConfigFile = findConfigFile (workspace , "mypy.ini" )
190- if (("enabled" not in configuration or configuration ["enabled" ])
191- and ("live_mode" not in configuration or configuration ["live_mode" ])):
192+ if ("enabled" not in configuration or configuration ["enabled" ]) and (
193+ "live_mode" not in configuration or configuration ["live_mode" ]
194+ ):
192195 global tmpFile
193- tmpFile = tempfile .NamedTemporaryFile ('w' , delete = False )
196+ tmpFile = tempfile .NamedTemporaryFile ("w" , delete = False )
194197 tmpFile .close ()
195198 return configuration
196199
0 commit comments