5050EXPLICIT_OPT_OUT_COMMENT = '#coverage:ignore'
5151
5252
53- def diff_to_new_interesting_lines (unified_diff_lines : List [str ]
54- ) -> Dict [int , str ]:
53+ def diff_to_new_interesting_lines (unified_diff_lines : List [str ]) -> Dict [int , str ]:
5554 """
5655 Extracts a set of 'interesting' lines out of a GNU unified diff format.
5756
@@ -95,7 +94,7 @@ def diff_to_new_interesting_lines(unified_diff_lines: List[str]
9594 # Parse the 'new file' range parts of the unified diff.
9695 if not diff_line .startswith ('@@ ' ):
9796 continue
98- change = diff_line [3 : diff_line .index (' @@' , 3 )]
97+ change = diff_line [3 : diff_line .index (' @@' , 3 )]
9998 new = change .split (' ' )[1 ]
10099 start = int (new .split (',' )[0 ])
101100 count = 1 if ',' not in new else int (new .split (',' )[1 ])
@@ -117,9 +116,9 @@ def fix_line_from_coverage_file(line):
117116 return line
118117
119118
120- def get_incremental_uncovered_lines (abs_path : str , base_commit : str ,
121- actual_commit : Optional [str ]
122- ) -> List [Tuple [int , str , str ]]:
119+ def get_incremental_uncovered_lines (
120+ abs_path : str , base_commit : str , actual_commit : Optional [str ]
121+ ) -> List [Tuple [int , str , str ]]:
123122 """
124123 Uses git diff and the annotation files created by
125124 `pytest --cov-report annotate` to find touched but uncovered lines in the
@@ -139,12 +138,10 @@ def get_incremental_uncovered_lines(abs_path: str, base_commit: str,
139138 if not os .path .isfile (abs_path ):
140139 return []
141140
142- unified_diff_lines_str = shell_tools .output_of ('git' , 'diff' , '--unified=0' ,
143- base_commit , actual_commit ,
144- '--' , abs_path )
145- unified_diff_lines = [
146- e for e in unified_diff_lines_str .split ('\n ' ) if e .strip ()
147- ]
141+ unified_diff_lines_str = shell_tools .output_of (
142+ 'git' , 'diff' , '--unified=0' , base_commit , actual_commit , '--' , abs_path
143+ )
144+ unified_diff_lines = [e for e in unified_diff_lines_str .split ('\n ' ) if e .strip ()]
148145
149146 touched_lines = diff_to_new_interesting_lines (unified_diff_lines )
150147
@@ -155,10 +152,12 @@ def get_incremental_uncovered_lines(abs_path: str, base_commit: str,
155152 has_cover_file = os .path .isfile (cover_path )
156153 content_file = cover_path if has_cover_file else abs_path
157154 with open (content_file , 'r' ) as annotated_coverage_file :
158- return [(i , fix_line_from_coverage_file (line ), touched_lines [i ])
159- for i , line in enumerate (annotated_coverage_file , start = 1 )
160- if i in touched_lines and i not in ignored_lines
161- if line_counts_as_uncovered (line , has_cover_file )]
155+ return [
156+ (i , fix_line_from_coverage_file (line ), touched_lines [i ])
157+ for i , line in enumerate (annotated_coverage_file , start = 1 )
158+ if i in touched_lines and i not in ignored_lines
159+ if line_counts_as_uncovered (line , has_cover_file )
160+ ]
162161
163162
164163def line_content_counts_as_uncovered_manual (content : str ) -> bool :
@@ -211,15 +210,13 @@ def determine_ignored_lines(content: str) -> Set[int]:
211210def naive_find_end_of_scope (lines : List [str ], i : int ) -> int :
212211 # TODO: deal with line continuations, which may be less indented.
213212 line = lines [i ]
214- indent = line [:len (line ) - len (line .lstrip ())]
215- while i < len (lines ) and (not lines [i ].strip () or
216- lines [i ].startswith (indent )):
213+ indent = line [: len (line ) - len (line .lstrip ())]
214+ while i < len (lines ) and (not lines [i ].strip () or lines [i ].startswith (indent )):
217215 i += 1
218216 return i
219217
220218
221- def line_counts_as_uncovered (line : str ,
222- is_from_cover_annotation_file : bool ) -> bool :
219+ def line_counts_as_uncovered (line : str , is_from_cover_annotation_file : bool ) -> bool :
223220 """
224221 Args:
225222 line: The line of code (including coverage annotation).
@@ -244,14 +241,13 @@ def line_counts_as_uncovered(line: str,
244241 # Ignore end-of-line comments.
245242 # TODO: avoid # in strings, etc.
246243 if '#' in content :
247- content = content [:content .index ('#' )].strip ()
244+ content = content [: content .index ('#' )].strip ()
248245
249246 # Ignored line pattern?
250247 if any (re .search (pat , content ) for pat in IGNORED_LINE_PATTERNS ):
251248 return False
252249
253- return (is_from_cover_annotation_file or
254- line_content_counts_as_uncovered_manual (content ))
250+ return is_from_cover_annotation_file or line_content_counts_as_uncovered_manual (content )
255251
256252
257253def is_applicable_python_file (rel_path : str ) -> bool :
@@ -263,8 +259,9 @@ def is_applicable_python_file(rel_path: str) -> bool:
263259 Returns:
264260 Whether to include the file.
265261 """
266- return (rel_path .endswith ('.py' ) and
267- not any (re .search (pat , rel_path ) for pat in IGNORED_FILE_PATTERNS ))
262+ return rel_path .endswith ('.py' ) and not any (
263+ re .search (pat , rel_path ) for pat in IGNORED_FILE_PATTERNS
264+ )
268265
269266
270267def check_for_uncovered_lines (env : env_tools .PreparedEnv ) -> int :
@@ -279,31 +276,36 @@ def check_for_uncovered_lines(env: env_tools.PreparedEnv) -> int:
279276
280277 base_path = cast (str , env .destination_directory )
281278 uncovered_lines = get_incremental_uncovered_lines (
282- os .path .join (base_path , changed_file ), env .compare_commit_id ,
283- env . actual_commit_id )
279+ os .path .join (base_path , changed_file ), env .compare_commit_id , env . actual_commit_id
280+ )
284281
285282 if uncovered_lines :
286283 uncovered_count += len (uncovered_lines )
287284 print (
288- shell_tools .highlight ('************* {} ({} uncovered)' .format (
289- changed_file , len (uncovered_lines )),
290- color_code = shell_tools .RED ))
285+ shell_tools .highlight (
286+ '************* {} ({} uncovered)' .format (changed_file , len (uncovered_lines )),
287+ color_code = shell_tools .RED ,
288+ )
289+ )
291290 for index , line , reason in uncovered_lines :
292- print ('Line {} {} but not covered: {}' .format (
293- shell_tools .highlight (str (index ).rjust (4 ),
294- color_code = shell_tools .BOLD ), reason ,
295- shell_tools .highlight (line , color_code = shell_tools .YELLOW )))
291+ print (
292+ 'Line {} {} but not covered: {}' .format (
293+ shell_tools .highlight (str (index ).rjust (4 ), color_code = shell_tools .BOLD ),
294+ reason ,
295+ shell_tools .highlight (line , color_code = shell_tools .YELLOW ),
296+ )
297+ )
296298
297299 # Inform of aggregate result.
298300 print ()
299301 if uncovered_count :
300302 print (
301303 shell_tools .highlight (
302304 'Found {} uncovered touched lines.' .format (uncovered_count ),
303- color_code = shell_tools .RED ))
305+ color_code = shell_tools .RED ,
306+ )
307+ )
304308 else :
305- print (
306- shell_tools .highlight ('All touched lines covered' ,
307- color_code = shell_tools .GREEN ))
309+ print (shell_tools .highlight ('All touched lines covered' , color_code = shell_tools .GREEN ))
308310 print ()
309311 return uncovered_count
0 commit comments