17
"""Process Mypy line precision report into useful statistics."""
26
from typing import Iterable, List
29
import mypy.main as mypy_main
31
PATHS = ["lib/streamlit/", "scripts/*", "e2e/scripts/*"]
33
os.path.join("scripts", "add_license_headers.py"),
34
os.path.join("e2e", "scripts", "st_reuse_label.py"),
35
os.path.join("scripts", "license-template.txt"),
39
def shlex_join(split_command: Iterable[str]):
40
"""Return a shell-escaped string from *split_command*.
42
This function is backported from Python 3.8 - shlex.join
44
return ' '.join(shlex.quote(arg) for arg in split_command)
48
_COLUMNS = (56, 5, 5, 7)
49
_HEADERS = ("Module", "Lines", "Typed", "Percent")
51
def __init__(self, name: str, lines: int = 0, precise: int = 0):
54
self.precise = precise
57
def header(cls) -> str:
58
fmt = "%-*s %-*s %-*s %-*s"
59
return ("%s\n%s" % (fmt, fmt)) % tuple(
61
*zip(cls._COLUMNS, cls._HEADERS),
62
*zip(cls._COLUMNS, ["-" * len(h) for h in cls._HEADERS]),
66
def __str__(self) -> str:
68
return "%-*s %*d %*d % 7.02f%%" % (
75
self.precise * 100 / self.lines,
79
def process_report(path: str) -> None:
80
modules: List[Module] = []
81
totals = Module("Total")
84
for line in f.readlines()[2:]:
87
if name.endswith("_pb2"):
89
total_lines = int(parts[1])
90
empty_lines = int(parts[5])
91
lines = total_lines - empty_lines
94
precise = int(parts[2])
95
modules.append(Module(name, lines, precise))
97
totals.precise += precise
99
print(Module.header())
100
for module in sorted(modules, key=lambda m: m.name):
106
@click.option("--report", is_flag=True, help="Emit line coverage report for all files")
112
"Verbose mode. Causes this command to print mypy command being executed."
115
def main(report: bool = False, verbose: bool = False) -> None:
116
paths: List[str] = []
119
for filename in glob.glob(path):
120
if not (filename in EXCLUDE_FILES or filename.endswith(".sh")):
121
paths.append(filename)
125
args = ["--config-file=lib/mypy.ini", "--namespace-packages"]
127
tempdir = tempfile.TemporaryDirectory()
128
args.append("--lineprecision-report=%s" % tempdir.name)
133
shell_command = shlex_join(itertools.chain(['mypy'], args))
134
print("Executing command:", shell_command)
135
mypy_main.main(stdout=sys.stdout, stderr=sys.stderr, args=args)
137
process_report(os.path.join(tempdir.name, "lineprecision.txt"))
140
if __name__ == "__main__":