1
1
# -*- coding: utf-8 -*-
2
2
"""
3
- File that contains the pyls plugin mypy-ls.
3
+ File that contains the pylsp plugin mypy-ls.
4
4
5
5
Created on Fri Jul 10 09:53:57 2020
6
6
12
12
import os .path
13
13
import logging
14
14
from 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
18
18
from typing import Optional , Dict , Any , IO , List
19
19
import atexit
20
20
27
27
tmpFile : Optional [IO [str ]] = None
28
28
29
29
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 ]]:
31
33
"""
32
34
Return a language-server diagnostic from a line of the Mypy error report.
33
35
@@ -54,51 +56,53 @@ def parse_line(line: str, document: Optional[Document] = None) -> Optional[Dict[
54
56
if file_path != "<string>" : # live mode
55
57
# results from other files can be included, but we cannot return
56
58
# 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
+ )
61
63
return None
62
64
63
65
lineno = int (linenoStr or 1 ) - 1 # 0-based line number
64
66
offset = int (offsetStr or 1 ) - 1 # 0-based offset
65
67
errno = 2
66
- if severity == ' error' :
68
+ if severity == " error" :
67
69
errno = 1
68
70
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 },
72
74
# 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 },
74
76
},
75
- ' message' : msg ,
76
- ' severity' : errno
77
+ " message" : msg ,
78
+ " severity" : errno ,
77
79
}
78
80
if document :
79
81
# although mypy does not provide the end of the affected range, we
80
82
# 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" ])
82
84
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 )
85
88
86
89
return diag
87
90
return None
88
91
89
92
90
93
@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 ]]:
93
97
"""
94
98
Lints.
95
99
96
100
Parameters
97
101
----------
98
102
config : Config
99
- The pyls config.
103
+ The pylsp config.
100
104
workspace : Workspace
101
- The pyls workspace.
105
+ The pylsp workspace.
102
106
document : Document
103
107
The document to be linted.
104
108
is_saved : bool
@@ -110,27 +114,25 @@ def pyls_lint(config: Config, workspace: Workspace, document: Document,
110
114
List of the linting data.
111
115
112
116
"""
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" ]
118
120
119
121
global tmpFile
120
122
if live_mode and not is_saved and tmpFile :
121
123
tmpFile = open (tmpFile .name , "w" )
122
124
tmpFile .write (document .source )
123
125
tmpFile .close ()
124
- args .extend ([' --shadow-file' , document .path , tmpFile .name ])
126
+ args .extend ([" --shadow-file" , document .path , tmpFile .name ])
125
127
elif not is_saved :
126
128
return []
127
129
128
130
if mypyConfigFile :
129
- args .append (' --config-file' )
131
+ args .append (" --config-file" )
130
132
args .append (mypyConfigFile )
131
133
args .append (document .path )
132
- if settings .get (' strict' , False ):
133
- args .append (' --strict' )
134
+ if settings .get (" strict" , False ):
135
+ args .append (" --strict" )
134
136
135
137
report , errors , _ = mypy_api .run (args )
136
138
@@ -144,14 +146,14 @@ def pyls_lint(config: Config, workspace: Workspace, document: Document,
144
146
145
147
146
148
@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 ]]]:
148
150
"""
149
151
Read the settings.
150
152
151
153
Parameters
152
154
----------
153
155
config : Config
154
- The pyls config.
156
+ The pylsp config.
155
157
156
158
Returns
157
159
-------
@@ -187,10 +189,11 @@ def init(workspace: str) -> Dict[str, str]:
187
189
configuration = eval (file .read ())
188
190
global mypyConfigFile
189
191
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
+ ):
192
195
global tmpFile
193
- tmpFile = tempfile .NamedTemporaryFile ('w' , delete = False )
196
+ tmpFile = tempfile .NamedTemporaryFile ("w" , delete = False )
194
197
tmpFile .close ()
195
198
return configuration
196
199
0 commit comments