1
1
import collections
2
2
import os
3
3
import subprocess
4
+ import sys
4
5
from pathlib import Path
5
6
from typing import Dict
6
7
from unittest .mock import Mock
16
17
DOC_URI = f"file:/{ Path (__file__ )} "
17
18
DOC_TYPE_ERR = """{}.append(3)
18
19
"""
19
- TYPE_ERR_MSG = '"Dict[<nothing>, <nothing>]" has no attribute "append" [attr-defined] '
20
+ TYPE_ERR_MSG = '"Dict[<nothing>, <nothing>]" has no attribute "append"'
20
21
21
- TEST_LINE = 'test_plugin.py:279:8: error: "Request" has no attribute "id"'
22
- TEST_LINE_WITHOUT_COL = "test_plugin.py:279: " 'error: "Request" has no attribute "id"'
23
- TEST_LINE_WITHOUT_LINE = "test_plugin.py: " 'error: "Request" has no attribute "id"'
22
+ TEST_LINE = 'test_plugin.py:279:8:279:16: error: "Request" has no attribute "id" [attr-defined]'
23
+ TEST_LINE_NOTE = (
24
+ 'test_plugin.py:124:1:129:77: note: Use "-> None" if function does not return a value'
25
+ )
24
26
25
27
windows_flag : Dict [str , int ] = (
26
28
{"creationflags" : subprocess .CREATE_NO_WINDOW } if os .name == "nt" else {} # type: ignore
@@ -65,40 +67,31 @@ def test_plugin(workspace, last_diagnostics_monkeypatch):
65
67
diag = diags [0 ]
66
68
assert diag ["message" ] == TYPE_ERR_MSG
67
69
assert diag ["range" ]["start" ] == {"line" : 0 , "character" : 0 }
68
- assert diag ["range" ]["end" ] == {"line" : 0 , "character" : 1 }
70
+ # Running mypy in 3.7 produces wrong error ends this can be removed when 3.7 reaches EOL
71
+ if sys .version_info < (3 , 8 ):
72
+ assert diag ["range" ]["end" ] == {"line" : 0 , "character" : 1 }
73
+ else :
74
+ assert diag ["range" ]["end" ] == {"line" : 0 , "character" : 9 }
75
+ assert diag ["severity" ] == 1
76
+ assert diag ["code" ] == "attr-defined"
69
77
70
78
71
79
def test_parse_full_line (workspace ):
72
80
diag = plugin .parse_line (TEST_LINE ) # TODO parse a document here
73
81
assert diag ["message" ] == '"Request" has no attribute "id"'
74
82
assert diag ["range" ]["start" ] == {"line" : 278 , "character" : 7 }
75
- assert diag ["range" ]["end" ] == {"line" : 278 , "character" : 8 }
76
-
83
+ assert diag ["range" ]["end" ] == {"line" : 278 , "character" : 16 }
84
+ assert diag ["severity" ] == 1
85
+ assert diag ["code" ] == "attr-defined"
77
86
78
- def test_parse_line_without_col (workspace ):
79
- doc = Document (DOC_URI , workspace )
80
- diag = plugin .parse_line (TEST_LINE_WITHOUT_COL , doc )
81
- assert diag ["message" ] == '"Request" has no attribute "id"'
82
- assert diag ["range" ]["start" ] == {"line" : 278 , "character" : 0 }
83
- assert diag ["range" ]["end" ] == {"line" : 278 , "character" : 1 }
84
87
85
-
86
- def test_parse_line_without_line (workspace ):
87
- doc = Document (DOC_URI , workspace )
88
- diag = plugin .parse_line (TEST_LINE_WITHOUT_LINE , doc )
89
- assert diag ["message" ] == '"Request" has no attribute "id"'
90
- assert diag ["range" ]["start" ] == {"line" : 0 , "character" : 0 }
91
- assert diag ["range" ]["end" ] == {"line" : 0 , "character" : 6 }
92
-
93
-
94
- @pytest .mark .parametrize ("word,bounds" , [("" , (7 , 8 )), ("my_var" , (7 , 13 ))])
95
- def test_parse_line_with_context (monkeypatch , word , bounds , workspace ):
96
- doc = Document (DOC_URI , workspace )
97
- monkeypatch .setattr (Document , "word_at_position" , lambda * args : word )
98
- diag = plugin .parse_line (TEST_LINE , doc )
99
- assert diag ["message" ] == '"Request" has no attribute "id"'
100
- assert diag ["range" ]["start" ] == {"line" : 278 , "character" : bounds [0 ]}
101
- assert diag ["range" ]["end" ] == {"line" : 278 , "character" : bounds [1 ]}
88
+ def test_parse_note_line (workspace ):
89
+ diag = plugin .parse_line (TEST_LINE_NOTE )
90
+ assert diag ["message" ] == 'Use "-> None" if function does not return a value'
91
+ assert diag ["range" ]["start" ] == {"line" : 123 , "character" : 0 }
92
+ assert diag ["range" ]["end" ] == {"line" : 128 , "character" : 77 }
93
+ assert diag ["severity" ] == 3
94
+ assert diag ["code" ] is None
102
95
103
96
104
97
def test_multiple_workspaces (tmpdir , last_diagnostics_monkeypatch ):
@@ -107,7 +100,7 @@ def foo():
107
100
return
108
101
unreachable = 1
109
102
"""
110
- DOC_ERR_MSG = "Statement is unreachable [unreachable] "
103
+ DOC_ERR_MSG = "Statement is unreachable"
111
104
112
105
# Initialize two workspace folders.
113
106
folder1 = tmpdir .mkdir ("folder1" )
@@ -132,6 +125,7 @@ def foo():
132
125
assert len (diags ) == 1
133
126
diag = diags [0 ]
134
127
assert diag ["message" ] == DOC_ERR_MSG
128
+ assert diag ["code" ] == "unreachable"
135
129
136
130
# Test document in workspace 2 (without mypy.ini configuration)
137
131
doc2 = Document (DOC_URI , ws2 , DOC_SOURCE )
@@ -226,7 +220,8 @@ def test_option_overrides_dmypy(last_diagnostics_monkeypatch, workspace):
226
220
"--" ,
227
221
"--python-executable" ,
228
222
"/tmp/fake" ,
229
- "--show-column-numbers" ,
223
+ "--show-error-end" ,
224
+ "--no-error-summary" ,
230
225
document .path ,
231
226
]
232
227
m .assert_called_with (expected , capture_output = True , ** windows_flag , encoding = "utf-8" )
@@ -270,7 +265,7 @@ def foo():
270
265
return
271
266
unreachable = 1
272
267
"""
273
- DOC_ERR_MSG = "Statement is unreachable [unreachable] "
268
+ DOC_ERR_MSG = "Statement is unreachable"
274
269
275
270
config_sub_paths = [".config" ]
276
271
@@ -296,6 +291,7 @@ def foo():
296
291
assert len (diags ) == 1
297
292
diag = diags [0 ]
298
293
assert diag ["message" ] == DOC_ERR_MSG
294
+ assert diag ["code" ] == "unreachable"
299
295
300
296
301
297
def test_config_sub_paths_config_changed (tmpdir , last_diagnostics_monkeypatch ):
@@ -304,7 +300,7 @@ def foo():
304
300
return
305
301
unreachable = 1
306
302
"""
307
- DOC_ERR_MSG = "Statement is unreachable [unreachable] "
303
+ DOC_ERR_MSG = "Statement is unreachable"
308
304
309
305
# Create configuration file for workspace.
310
306
config_dir = tmpdir .mkdir (".config" )
@@ -327,3 +323,4 @@ def foo():
327
323
assert len (diags ) == 1
328
324
diag = diags [0 ]
329
325
assert diag ["message" ] == DOC_ERR_MSG
326
+ assert diag ["code" ] == "unreachable"
0 commit comments