11import collections
22import os
33import subprocess
4+ import sys
45from pathlib import Path
56from typing import Dict
67from unittest .mock import Mock
1617DOC_URI = f"file:/{ Path (__file__ )} "
1718DOC_TYPE_ERR = """{}.append(3)
1819"""
19- TYPE_ERR_MSG = '"Dict[<nothing>, <nothing>]" has no attribute "append" [attr-defined] '
20+ TYPE_ERR_MSG = '"Dict[<nothing>, <nothing>]" has no attribute "append"'
2021
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+ )
2426
2527windows_flag : Dict [str , int ] = (
2628 {"creationflags" : subprocess .CREATE_NO_WINDOW } if os .name == "nt" else {} # type: ignore
@@ -65,40 +67,31 @@ def test_plugin(workspace, last_diagnostics_monkeypatch):
6567 diag = diags [0 ]
6668 assert diag ["message" ] == TYPE_ERR_MSG
6769 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"
6977
7078
7179def test_parse_full_line (workspace ):
7280 diag = plugin .parse_line (TEST_LINE ) # TODO parse a document here
7381 assert diag ["message" ] == '"Request" has no attribute "id"'
7482 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"
7786
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 }
8487
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
10295
10396
10497def test_multiple_workspaces (tmpdir , last_diagnostics_monkeypatch ):
@@ -107,7 +100,7 @@ def foo():
107100 return
108101 unreachable = 1
109102"""
110- DOC_ERR_MSG = "Statement is unreachable [unreachable] "
103+ DOC_ERR_MSG = "Statement is unreachable"
111104
112105 # Initialize two workspace folders.
113106 folder1 = tmpdir .mkdir ("folder1" )
@@ -132,6 +125,7 @@ def foo():
132125 assert len (diags ) == 1
133126 diag = diags [0 ]
134127 assert diag ["message" ] == DOC_ERR_MSG
128+ assert diag ["code" ] == "unreachable"
135129
136130 # Test document in workspace 2 (without mypy.ini configuration)
137131 doc2 = Document (DOC_URI , ws2 , DOC_SOURCE )
@@ -226,7 +220,8 @@ def test_option_overrides_dmypy(last_diagnostics_monkeypatch, workspace):
226220 "--" ,
227221 "--python-executable" ,
228222 "/tmp/fake" ,
229- "--show-column-numbers" ,
223+ "--show-error-end" ,
224+ "--no-error-summary" ,
230225 document .path ,
231226 ]
232227 m .assert_called_with (expected , capture_output = True , ** windows_flag , encoding = "utf-8" )
@@ -270,7 +265,7 @@ def foo():
270265 return
271266 unreachable = 1
272267"""
273- DOC_ERR_MSG = "Statement is unreachable [unreachable] "
268+ DOC_ERR_MSG = "Statement is unreachable"
274269
275270 config_sub_paths = [".config" ]
276271
@@ -296,6 +291,7 @@ def foo():
296291 assert len (diags ) == 1
297292 diag = diags [0 ]
298293 assert diag ["message" ] == DOC_ERR_MSG
294+ assert diag ["code" ] == "unreachable"
299295
300296
301297def test_config_sub_paths_config_changed (tmpdir , last_diagnostics_monkeypatch ):
@@ -304,7 +300,7 @@ def foo():
304300 return
305301 unreachable = 1
306302"""
307- DOC_ERR_MSG = "Statement is unreachable [unreachable] "
303+ DOC_ERR_MSG = "Statement is unreachable"
308304
309305 # Create configuration file for workspace.
310306 config_dir = tmpdir .mkdir (".config" )
@@ -327,3 +323,4 @@ def foo():
327323 assert len (diags ) == 1
328324 diag = diags [0 ]
329325 assert diag ["message" ] == DOC_ERR_MSG
326+ assert diag ["code" ] == "unreachable"
0 commit comments