1
1
# -*- coding: utf-8 -*-
2
2
"""
3
- File that contains the pylsp plugin mypy-ls.
3
+ File that contains the python-lsp-server plugin mypy-ls.
4
4
5
5
Created on Fri Jul 10 09:53:57 2020
6
6
27
27
tmpFile : Optional [IO [str ]] = None
28
28
29
29
30
- def parse_line (
31
- line : str , document : Optional [Document ] = None
32
- ) -> Optional [Dict [str , Any ]]:
30
+ def parse_line (line : str , document : Optional [Document ] = None ) -> Optional [Dict [str , Any ]]:
33
31
"""
34
32
Return a language-server diagnostic from a line of the Mypy error report.
35
33
@@ -56,44 +54,42 @@ def parse_line(
56
54
if file_path != "<string>" : # live mode
57
55
# results from other files can be included, but we cannot return
58
56
# them.
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
- )
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 )
63
61
return None
64
62
65
63
lineno = int (linenoStr or 1 ) - 1 # 0-based line number
66
64
offset = int (offsetStr or 1 ) - 1 # 0-based offset
67
65
errno = 2
68
- if severity == " error" :
66
+ if severity == ' error' :
69
67
errno = 1
70
68
diag : Dict [str , Any ] = {
71
- " source" : " mypy" ,
72
- " range" : {
73
- " start" : {" line" : lineno , " character" : offset },
69
+ ' source' : ' mypy' ,
70
+ ' range' : {
71
+ ' start' : {' line' : lineno , ' character' : offset },
74
72
# There may be a better solution, but mypy does not provide end
75
- " end" : {" line" : lineno , " character" : offset + 1 },
73
+ ' end' : {' line' : lineno , ' character' : offset + 1 }
76
74
},
77
- " message" : msg ,
78
- " severity" : errno ,
75
+ ' message' : msg ,
76
+ ' severity' : errno
79
77
}
80
78
if document :
81
79
# although mypy does not provide the end of the affected range, we
82
80
# can make a good guess by highlighting the word that Mypy flagged
83
- word = document .word_at_position (diag [" range" ][ " start" ])
81
+ word = document .word_at_position (diag [' range' ][ ' start' ])
84
82
if word :
85
- diag ["range" ]["end" ]["character" ] = diag ["range" ]["start" ][
86
- "character"
87
- ] + len (word )
83
+ diag ['range' ]['end' ]['character' ] = (
84
+ diag ['range' ]['start' ]['character' ] + len (word ))
88
85
89
86
return diag
90
87
return None
91
88
92
89
93
90
@hookimpl
94
- def pylsp_lint (
95
- config : Config , workspace : Workspace , document : Document , is_saved : bool
96
- ) -> List [Dict [str , Any ]]:
91
+ def pylsp_lint (config : Config , workspace : Workspace , document : Document ,
92
+ is_saved : bool ) -> List [Dict [str , Any ]]:
97
93
"""
98
94
Lints.
99
95
@@ -114,25 +110,27 @@ def pylsp_lint(
114
110
List of the linting data.
115
111
116
112
"""
117
- settings = config .plugin_settings ("mypy-ls" )
118
- live_mode = settings .get ("live_mode" , True )
119
- args = ["--incremental" , "--show-column-numbers" , "--follow-imports" , "silent" ]
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' ]
120
118
121
119
global tmpFile
122
120
if live_mode and not is_saved and tmpFile :
123
121
tmpFile = open (tmpFile .name , "w" )
124
122
tmpFile .write (document .source )
125
123
tmpFile .close ()
126
- args .extend ([" --shadow-file" , document .path , tmpFile .name ])
124
+ args .extend ([' --shadow-file' , document .path , tmpFile .name ])
127
125
elif not is_saved :
128
126
return []
129
127
130
128
if mypyConfigFile :
131
- args .append (" --config-file" )
129
+ args .append (' --config-file' )
132
130
args .append (mypyConfigFile )
133
131
args .append (document .path )
134
- if settings .get (" strict" , False ):
135
- args .append (" --strict" )
132
+ if settings .get (' strict' , False ):
133
+ args .append (' --strict' )
136
134
137
135
report , errors , _ = mypy_api .run (args )
138
136
@@ -189,11 +187,10 @@ def init(workspace: str) -> Dict[str, str]:
189
187
configuration = eval (file .read ())
190
188
global mypyConfigFile
191
189
mypyConfigFile = findConfigFile (workspace , "mypy.ini" )
192
- if ("enabled" not in configuration or configuration ["enabled" ]) and (
193
- "live_mode" not in configuration or configuration ["live_mode" ]
194
- ):
190
+ if (("enabled" not in configuration or configuration ["enabled" ])
191
+ and ("live_mode" not in configuration or configuration ["live_mode" ])):
195
192
global tmpFile
196
- tmpFile = tempfile .NamedTemporaryFile ("w" , delete = False )
193
+ tmpFile = tempfile .NamedTemporaryFile ('w' , delete = False )
197
194
tmpFile .close ()
198
195
return configuration
199
196
0 commit comments