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:279:19: error: "Request" has no attribute "id"'
22- TEST_LINE_WITHOUT_END = 'test_plugin.py:279:8: error: "Request" has no attribute "id"'
23- TEST_LINE_WITHOUT_COL = "test_plugin.py:279: " 'error: "Request" has no attribute "id"'
24- 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]'
2523
2624windows_flag : Dict [str , int ] = (
2725 {"creationflags" : subprocess .CREATE_NO_WINDOW } if os .name == "nt" else {} # type: ignore
@@ -66,48 +64,22 @@ def test_plugin(workspace, last_diagnostics_monkeypatch):
6664 diag = diags [0 ]
6765 assert diag ["message" ] == TYPE_ERR_MSG
6866 assert diag ["range" ]["start" ] == {"line" : 0 , "character" : 0 }
69- assert diag ["range" ]["end" ] == {"line" : 0 , "character" : 9 }
67+ # Running mypy in 3.7 produces wrong error ends this can be removed when 3.7 reaches EOL
68+ if sys .version_info < (3 , 8 ):
69+ assert diag ["range" ]["end" ] == {"line" : 0 , "character" : 1 }
70+ else :
71+ assert diag ["range" ]["end" ] == {"line" : 0 , "character" : 9 }
72+ assert diag ["severity" ] == 1
73+ assert diag ["code" ] == "attr-defined"
7074
7175
7276def test_parse_full_line (workspace ):
7377 diag = plugin .parse_line (TEST_LINE ) # TODO parse a document here
7478 assert diag ["message" ] == '"Request" has no attribute "id"'
7579 assert diag ["range" ]["start" ] == {"line" : 278 , "character" : 7 }
76- assert diag ["range" ]["end" ] == {"line" : 278 , "character" : 19 }
77-
78-
79- def test_parse_line_without_end (workspace ):
80- doc = Document (DOC_URI , workspace )
81- diag = plugin .parse_line (TEST_LINE_WITHOUT_END , doc )
82- assert diag ["message" ] == '"Request" has no attribute "id"'
83- assert diag ["range" ]["start" ] == {"line" : 278 , "character" : 7 }
84- assert diag ["range" ]["end" ] == {"line" : 278 , "character" : 13 }
85-
86-
87- def test_parse_line_without_col (workspace ):
88- doc = Document (DOC_URI , workspace )
89- diag = plugin .parse_line (TEST_LINE_WITHOUT_COL , doc )
90- assert diag ["message" ] == '"Request" has no attribute "id"'
91- assert diag ["range" ]["start" ] == {"line" : 278 , "character" : 0 }
92- assert diag ["range" ]["end" ] == {"line" : 278 , "character" : 1 }
93-
94-
95- def test_parse_line_without_line (workspace ):
96- doc = Document (DOC_URI , workspace )
97- diag = plugin .parse_line (TEST_LINE_WITHOUT_LINE , doc )
98- assert diag ["message" ] == '"Request" has no attribute "id"'
99- assert diag ["range" ]["start" ] == {"line" : 0 , "character" : 0 }
100- assert diag ["range" ]["end" ] == {"line" : 0 , "character" : 6 }
101-
102-
103- @pytest .mark .parametrize ("word,bounds" , [("" , (7 , 8 )), ("my_var" , (7 , 13 ))])
104- def test_parse_line_with_context (monkeypatch , word , bounds , workspace ):
105- doc = Document (DOC_URI , workspace )
106- monkeypatch .setattr (Document , "word_at_position" , lambda * args : word )
107- diag = plugin .parse_line (TEST_LINE_WITHOUT_END , doc )
108- assert diag ["message" ] == '"Request" has no attribute "id"'
109- assert diag ["range" ]["start" ] == {"line" : 278 , "character" : bounds [0 ]}
110- assert diag ["range" ]["end" ] == {"line" : 278 , "character" : bounds [1 ]}
80+ assert diag ["range" ]["end" ] == {"line" : 278 , "character" : 16 }
81+ assert diag ["severity" ] == 1
82+ assert diag ["code" ] == "attr-defined"
11183
11284
11385def test_multiple_workspaces (tmpdir , last_diagnostics_monkeypatch ):
@@ -116,7 +88,7 @@ def foo():
11688 return
11789 unreachable = 1
11890"""
119- DOC_ERR_MSG = "Statement is unreachable [unreachable] "
91+ DOC_ERR_MSG = "Statement is unreachable"
12092
12193 # Initialize two workspace folders.
12294 folder1 = tmpdir .mkdir ("folder1" )
@@ -141,6 +113,7 @@ def foo():
141113 assert len (diags ) == 1
142114 diag = diags [0 ]
143115 assert diag ["message" ] == DOC_ERR_MSG
116+ assert diag ["code" ] == "unreachable"
144117
145118 # Test document in workspace 2 (without mypy.ini configuration)
146119 doc2 = Document (DOC_URI , ws2 , DOC_SOURCE )
@@ -236,6 +209,7 @@ def test_option_overrides_dmypy(last_diagnostics_monkeypatch, workspace):
236209 "--python-executable" ,
237210 "/tmp/fake" ,
238211 "--show-error-end" ,
212+ "--no-error-summary" ,
239213 document .path ,
240214 ]
241215 m .assert_called_with (expected , capture_output = True , ** windows_flag , encoding = "utf-8" )
@@ -279,7 +253,7 @@ def foo():
279253 return
280254 unreachable = 1
281255"""
282- DOC_ERR_MSG = "Statement is unreachable [unreachable] "
256+ DOC_ERR_MSG = "Statement is unreachable"
283257
284258 config_sub_paths = [".config" ]
285259
@@ -305,6 +279,7 @@ def foo():
305279 assert len (diags ) == 1
306280 diag = diags [0 ]
307281 assert diag ["message" ] == DOC_ERR_MSG
282+ assert diag ["code" ] == "unreachable"
308283
309284
310285def test_config_sub_paths_config_changed (tmpdir , last_diagnostics_monkeypatch ):
@@ -313,7 +288,7 @@ def foo():
313288 return
314289 unreachable = 1
315290"""
316- DOC_ERR_MSG = "Statement is unreachable [unreachable] "
291+ DOC_ERR_MSG = "Statement is unreachable"
317292
318293 # Create configuration file for workspace.
319294 config_dir = tmpdir .mkdir (".config" )
@@ -336,3 +311,4 @@ def foo():
336311 assert len (diags ) == 1
337312 diag = diags [0 ]
338313 assert diag ["message" ] == DOC_ERR_MSG
314+ assert diag ["code" ] == "unreachable"
0 commit comments