@@ -51,6 +51,64 @@ def debug(message: object) -> None:
5151 print (f"\n [{ __title__ } ] DEBUG: { message } " )
5252
5353
54+ def parse_comments (
55+ mypy_output : str ,
56+ default_file : str ,
57+ default_line : int ,
58+ ) -> dict [str , list [utils .Comment ]]:
59+ """Parse mypy output, return mapping of filenames to lists of comments."""
60+ error_type = re .compile (r" \[[a-z\-]+\]\s*$" )
61+
62+ files : dict [str , list [utils .Comment ]] = {}
63+ for output_line in mypy_output .splitlines ():
64+ if not output_line .strip ():
65+ continue
66+ filename = default_file
67+ line = default_line
68+ line_end = default_line
69+ col = 0
70+ col_end = 0
71+ msg_type = "unrecognized"
72+
73+ if output_line .count (": " ) < 2 :
74+ text = output_line
75+ else :
76+ where , msg_type , text = output_line .split (": " , 2 )
77+
78+ position = where .split (":" )
79+
80+ filename = position [0 ]
81+ if len (position ) > 1 :
82+ line = int (position [1 ])
83+ line_end = line
84+ if len (position ) > 2 :
85+ col = int (position [2 ])
86+ col_end = col
87+ if len (position ) > 4 :
88+ line_end = int (position [3 ])
89+ if line_end == line :
90+ col_end = int (position [4 ])
91+ else :
92+ line_end = line
93+ comment_type = error_type .search (text )
94+ if comment_type is not None :
95+ text = text [: comment_type .start ()]
96+ msg_type = f"{ comment_type .group (0 )[3 :- 1 ]} { msg_type } "
97+
98+ comment = utils .Comment (
99+ file = filename ,
100+ line = line ,
101+ contents = f"{ msg_type } : { text } " ,
102+ column = col ,
103+ line_end = line_end ,
104+ column_end = col_end ,
105+ )
106+
107+ files .setdefault (filename , [])
108+ files [filename ].append (comment )
109+ return files
110+
111+
54112# Important weird: If event handler function returns 'break',
55113# then it prevents other bindings of same event type from running.
56114# If returns None, normal and others are also run.
@@ -211,64 +269,6 @@ def typecomment_only_current_file(self) -> bool:
211269 """Should only add type comments for currently open file?."""
212270 return True
213271
214- @staticmethod
215- def parse_comments (
216- mypy_output : str ,
217- default_file : str ,
218- default_line : int ,
219- ) -> dict [str , list [utils .Comment ]]:
220- """Parse mypy output, return mapping of filenames to lists of comments."""
221- error_type = re .compile (r" \[[a-z\-]+\]\s*$" )
222-
223- files : dict [str , list [utils .Comment ]] = {}
224- for output_line in mypy_output .splitlines ():
225- if not output_line .strip ():
226- continue
227- filename = default_file
228- line = default_line
229- line_end = default_line
230- col = 0
231- col_end = 0
232- msg_type = "unrecognized"
233-
234- if output_line .count (": " ) < 2 :
235- text = output_line
236- else :
237- where , msg_type , text = output_line .split (": " , 2 )
238-
239- position = where .split (":" )
240-
241- filename = position [0 ]
242- if len (position ) > 1 :
243- line = int (position [1 ])
244- line_end = line
245- if len (position ) > 2 :
246- col = int (position [2 ])
247- col_end = col
248- if len (position ) > 4 :
249- line_end = int (position [3 ])
250- if line_end == line :
251- col_end = int (position [4 ])
252- else :
253- line_end = line
254- comment_type = error_type .search (text )
255- if comment_type is not None :
256- text = text [: comment_type .start ()]
257- msg_type = f"{ comment_type .group (0 )[3 :- 1 ]} { msg_type } "
258-
259- comment = utils .Comment (
260- file = filename ,
261- line = line ,
262- contents = f"{ msg_type } : { text } " ,
263- column = col ,
264- line_end = line_end ,
265- column_end = col_end ,
266- )
267-
268- files .setdefault (filename , [])
269- files [filename ].append (comment )
270- return files
271-
272272 def add_type_comments_for_file (
273273 self ,
274274 target_filename : str ,
@@ -308,7 +308,7 @@ def add_mypy_messages(
308308 """
309309 assert self .files .filename is not None
310310
311- files = self . parse_comments (
311+ files = parse_comments (
312312 mypy_output ,
313313 os .path .abspath (self .files .filename ),
314314 start_line ,
@@ -368,7 +368,7 @@ def add_extra_data(
368368 Tuple of:
369369 - Number of lines attempted to add
370370 - List of line numbers added that were not already there
371- otherwise None because no content.
371+ otherwise empty because no content.
372372
373373 """
374374 if not data :
0 commit comments