|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" Utility to compare to report the number of increased errors in the same |
| 3 | +code. |
| 4 | +
|
| 5 | +The exit code of the tool will be 0 if the number of reported errors stayed the |
| 6 | +same *or decreased*. Otherwise it will be number of additional errors reported. |
| 7 | +
|
| 8 | +This utility assumes both reports are produced by the *same version of Mypy*, |
| 9 | +and that *the generated report has stable messages*, otherwise the errors |
| 10 | +results are not stable. |
| 11 | +""" |
| 12 | +import re |
| 13 | +import sys |
| 14 | +from collections import defaultdict |
| 15 | +from itertools import groupby |
| 16 | +from typing import Dict, Iterator, NamedTuple, Optional, Tuple |
| 17 | + |
| 18 | +MYPY_LINE = re.compile( |
| 19 | + r"^" |
| 20 | + r"(?P<filename>([^:]|\\:)+):" |
| 21 | + r"((?P<linenum>([0-9]+)):)?" |
| 22 | + r"(?P<level>([^:]|\\:)+):" |
| 23 | + r"(?P<message>.+$)" |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +class FileErrorType(NamedTuple): |
| 28 | + filename: str |
| 29 | + errortype: str |
| 30 | + |
| 31 | + |
| 32 | +class Error(NamedTuple): |
| 33 | + filename: str |
| 34 | + linenum: str |
| 35 | + level: str |
| 36 | + message: str |
| 37 | + |
| 38 | + |
| 39 | +NewErrorCountPerFile = Dict[FileErrorType, int] |
| 40 | +UNKNOWN_REPORT_LINE = "Unrecognized line format" |
| 41 | + |
| 42 | + |
| 43 | +def compare_errors(error_old: Error, error_new: Error) -> int: |
| 44 | + # Errors for `filename` have been fixed according to the new report. |
| 45 | + if error_old.filename < error_new.filename: |
| 46 | + return -1 |
| 47 | + |
| 48 | + # Assuming stable output. |
| 49 | + message_old = (error_old.level, error_old.message) |
| 50 | + message_new = (error_new.level, error_new.message) |
| 51 | + |
| 52 | + # The error `(level, message)` is fixed according to the new report |
| 53 | + if message_old < message_new: |
| 54 | + return -1 |
| 55 | + |
| 56 | + if message_old == message_new: |
| 57 | + return 0 |
| 58 | + |
| 59 | + # Do not compare line numbers. If the error moved around it does not |
| 60 | + # matter, only new and fixed bugs. |
| 61 | + |
| 62 | + return 1 |
| 63 | + |
| 64 | + |
| 65 | +def next_error(f: Iterator[Error]) -> Optional[Error]: |
| 66 | + try: |
| 67 | + return next(f) |
| 68 | + except StopIteration: |
| 69 | + return None |
| 70 | + |
| 71 | + |
| 72 | +def get_errors(previous_report: str) -> Iterator[Error]: |
| 73 | + with open(previous_report, "r") as file: |
| 74 | + for line in file: |
| 75 | + match = MYPY_LINE.match(line) |
| 76 | + assert match, UNKNOWN_REPORT_LINE |
| 77 | + |
| 78 | + error = Error( |
| 79 | + filename=match["filename"], |
| 80 | + linenum=match["linenum"], |
| 81 | + level=match["level"], |
| 82 | + message=match["message"], |
| 83 | + ) |
| 84 | + yield error |
| 85 | + |
| 86 | + |
| 87 | +def sort_by_filename_level_error(error: Error) -> Tuple: |
| 88 | + return error.filename, error.level, error.message |
| 89 | + |
| 90 | + |
| 91 | +def compare_reports(previous_report: str, new_report: str) -> Tuple[NewErrorCountPerFile, int]: |
| 92 | + previous_errors_unsorted = get_errors(previous_report) |
| 93 | + new_errors_unsorted = get_errors(new_report) |
| 94 | + |
| 95 | + previous_errors = sorted(previous_errors_unsorted, key=sort_by_filename_level_error) |
| 96 | + new_errors = sorted(new_errors_unsorted, key=sort_by_filename_level_error) |
| 97 | + |
| 98 | + previous_errors_it = iter(previous_errors) |
| 99 | + new_errors_it = iter(new_errors) |
| 100 | + |
| 101 | + new_errors_count = 0 |
| 102 | + error_count: NewErrorCountPerFile = defaultdict(int) |
| 103 | + |
| 104 | + previous_error = next_error(previous_errors_it) |
| 105 | + new_error = next_error(new_errors_it) |
| 106 | + |
| 107 | + while previous_error is not None and new_error is not None: |
| 108 | + compare = compare_errors(previous_error, new_error) |
| 109 | + |
| 110 | + # The new report has a new error |
| 111 | + if compare > 0: |
| 112 | + new_errors_count += 1 |
| 113 | + error_count[FileErrorType(new_error.filename, new_error.message)] += 1 |
| 114 | + |
| 115 | + if compare < 0: |
| 116 | + previous_error = next_error(previous_errors_it) |
| 117 | + elif compare == 0: |
| 118 | + previous_error = next_error(previous_errors_it) |
| 119 | + new_error = next_error(new_errors_it) |
| 120 | + else: |
| 121 | + new_error = next_error(new_errors_it) |
| 122 | + |
| 123 | + # Extra lines in the new report are new errors |
| 124 | + while new_error is not None: |
| 125 | + new_errors_count += 1 |
| 126 | + error_count[FileErrorType(new_error.filename, new_error.message)] += 1 |
| 127 | + |
| 128 | + new_error = next_error(new_errors_it) |
| 129 | + |
| 130 | + return error_count, new_errors_count |
| 131 | + |
| 132 | + |
| 133 | +def print_changes(report: NewErrorCountPerFile) -> None: |
| 134 | + error_types_grouped_by_filename = groupby(sorted(report), key=lambda k: k.filename) |
| 135 | + |
| 136 | + for filename, error_types_it in error_types_grouped_by_filename: |
| 137 | + error_types = list(error_types_it) |
| 138 | + total_errors_for_file = sum(report[error] for error in error_types) |
| 139 | + |
| 140 | + print(f"{filename} :: +{total_errors_for_file}") |
| 141 | + for error in error_types: |
| 142 | + print(f" +{report[error]} :: {error.errortype}") |
| 143 | + print() |
| 144 | + print() |
| 145 | + |
| 146 | + |
| 147 | +def main() -> None: |
| 148 | + import argparse |
| 149 | + |
| 150 | + parser = argparse.ArgumentParser() |
| 151 | + parser.add_argument("previous_report") |
| 152 | + parser.add_argument("new_report") |
| 153 | + args = parser.parse_args() |
| 154 | + |
| 155 | + report, changes = compare_reports(args.previous_report, args.new_report) |
| 156 | + |
| 157 | + if changes > 0: |
| 158 | + print_changes(report) |
| 159 | + sys.exit(changes) |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + main() |
0 commit comments