| 1 |
"""Translates Mypy's output into GitHub's error/warning annotation syntax. |
| 2 |
|
| 3 |
See: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions |
| 4 |
|
| 5 |
This first is run with Mypy's output piped in, to collect messages in |
| 6 |
mypy_annotate.dat. After all platforms run, we run this again, which prints the |
| 7 |
messages in GitHub's format but with cross-platform failures deduplicated. |
| 8 |
""" |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
import argparse |
| 13 |
import pickle |
| 14 |
import re |
| 15 |
import sys |
| 16 |
|
| 17 |
import attrs |
| 18 |
|
| 19 |
# Example: 'package/filename.py:42:1:46:3: error: Type error here [code]' |
| 20 |
report_re = re.compile( |
| 21 |
r""" |
| 22 |
([^:]+): # Filename (anything but ":") |
| 23 |
([0-9]+): # Line number (start) |
| 24 |
(?:([0-9]+): # Optional column number |
| 25 |
(?:([0-9]+):([0-9]+):)? # then also optionally, 2 more numbers for end columns |
| 26 |
)? |
| 27 |
\s*(error|warn|note): # Kind, prefixed with space |
| 28 |
(.+) # Message |
| 29 |
""", |
| 30 |
re.VERBOSE, |
| 31 |
) |
| 32 |
|
| 33 |
mypy_to_github = { |
| 34 |
"error": "error", |
| 35 |
"warn": "warning", |
| 36 |
"note": "notice", |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
@attrs.frozen(kw_only=True) |
| 41 |
class Result: |
| 42 |
"""Accumulated results, used as a dict key to deduplicate.""" |
| 43 |
|
| 44 |
filename: str |
| 45 |
start_line: int |
| 46 |
kind: str |
| 47 |
message: str |
| 48 |
start_col: int | None = None |
| 49 |
end_line: int | None = None |
| 50 |
end_col: int | None = None |
| 51 |
|
| 52 |
|
| 53 |
def process_line(line: str) -> Result | None: |
| 54 |
if match := report_re.fullmatch(line.rstrip()): |
| 55 |
filename, st_line, st_col, end_line, end_col, kind, message = match.groups() |
| 56 |
return Result( |
| 57 |
filename=filename, |
| 58 |
start_line=int(st_line), |
| 59 |
start_col=int(st_col) if st_col is not None else None, |
| 60 |
end_line=int(end_line) if end_line is not None else None, |
| 61 |
end_col=int(end_col) if end_col is not None else None, |
| 62 |
kind=mypy_to_github[kind], |
| 63 |
message=message, |
| 64 |
) |
| 65 |
else: |
| 66 |
return None |
| 67 |
|
| 68 |
|
| 69 |
def export(results: dict[Result, list[str]]) -> None: |
| 70 |
"""Display the collected results.""" |
| 71 |
for res, platforms in results.items(): |
| 72 |
print(f"::{res.kind} file={res.filename},line={res.start_line},", end="") |
| 73 |
if res.start_col is not None: |
| 74 |
print(f"col={res.start_col},", end="") |
| 75 |
if res.end_col is not None and res.end_line is not None: |
| 76 |
print(f"endLine={res.end_line},endColumn={res.end_col},", end="") |
| 77 |
message = f"({res.start_line}:{res.start_col} - {res.end_line}:{res.end_col}):{res.message}" |
| 78 |
else: |
| 79 |
message = f"({res.start_line}:{res.start_col}):{res.message}" |
| 80 |
else: |
| 81 |
message = f"{res.start_line}:{res.message}" |
| 82 |
print(f"title=Mypy-{'+'.join(platforms)}::{res.filename}:{message}") |
| 83 |
|
| 84 |
|
| 85 |
def main(argv: list[str]) -> None: |
| 86 |
"""Look for error messages, and convert the format.""" |
| 87 |
parser = argparse.ArgumentParser(description=__doc__) |
| 88 |
parser.add_argument( |
| 89 |
"--dumpfile", |
| 90 |
help="File to write pickled messages to.", |
| 91 |
required=True, |
| 92 |
) |
| 93 |
parser.add_argument( |
| 94 |
"--platform", |
| 95 |
help="OS name, if set Mypy should be piped to stdin.", |
| 96 |
default=None, |
| 97 |
) |
| 98 |
cmd_line = parser.parse_args(argv) |
| 99 |
|
| 100 |
results: dict[Result, list[str]] |
| 101 |
try: |
| 102 |
with open(cmd_line.dumpfile, "rb") as f: |
| 103 |
results = pickle.load(f) |
| 104 |
except (FileNotFoundError, pickle.UnpicklingError): |
| 105 |
# If we fail to load, assume it's an old result. |
| 106 |
results = {} |
| 107 |
|
| 108 |
if cmd_line.platform is None: |
| 109 |
# Write out the results. |
| 110 |
export(results) |
| 111 |
else: |
| 112 |
platform: str = cmd_line.platform |
| 113 |
for line in sys.stdin: |
| 114 |
parsed = process_line(line) |
| 115 |
if parsed is not None: |
| 116 |
try: |
| 117 |
results[parsed].append(platform) |
| 118 |
except KeyError: |
| 119 |
results[parsed] = [platform] |
| 120 |
sys.stdout.write(line) |
| 121 |
with open(cmd_line.dumpfile, "wb") as f: |
| 122 |
pickle.dump(results, f) |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__": |
| 126 |
main(sys.argv[1:]) |