Skip to content

Instantly share code, notes, and snippets.

@michaelosthege
Created February 5, 2022 21:23
Show Gist options
  • Save michaelosthege/24d0703e5f37850c9e5679f69598930a to your computer and use it in GitHub Desktop.
Save michaelosthege/24d0703e5f37850c9e5679f69598930a to your computer and use it in GitHub Desktop.
Reformats piped mypy output to group by the error code.
"""
Reformats piped mypy output to group by the error code.
Author: Michael Osthege
License: MIT
Usage
-----
mypy -p mypackage --show-error-codes | python mypy_groupby.py
"""
import pandas
import sys
from typing import Iterator
def mypy_to_pandas(input_lines: Iterator[str]) -> pandas.DataFrame:
current_section = None
data = {
"file": [],
"line": [],
"type": [],
"section": [],
"message": [],
}
for line in input_lines:
line = line.strip()
elems = line.split(":")
if len(elems) < 3:
continue
try:
file, lineno, message_type, *_ = elems[0:3]
message_type = message_type.strip()
if message_type == "error":
current_section = line.split(" [")[-1][:-1]
message = line.replace(f"{file}:{lineno}: {message_type}: ", "").replace(
f" [{current_section}]", ""
)
data["file"].append(file)
data["line"].append(lineno)
data["type"].append(message_type)
data["section"].append(current_section)
data["message"].append(message)
except Exception as ex:
print(elems)
print(ex)
return pandas.DataFrame(data=data).set_index(["file", "line"])
if __name__ == "__main__":
df = mypy_to_pandas(sys.stdin)
for section, sdf in df.reset_index().groupby("section"):
print(f"\n\n[{section}]")
for row in sdf.itertuples():
print(f"{row.file}:{row.line}: {row.type}: {row.message}")
print()
print(f"{len(df)} rows in total.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment