report.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. """Classes for producing HTML reports about imprecision."""
  2. from __future__ import annotations
  3. import collections
  4. import itertools
  5. import json
  6. import os
  7. import shutil
  8. import sys
  9. import time
  10. import tokenize
  11. from abc import ABCMeta, abstractmethod
  12. from operator import attrgetter
  13. from typing import Any, Callable, Dict, Iterator, Tuple
  14. from typing_extensions import Final, TypeAlias as _TypeAlias
  15. from urllib.request import pathname2url
  16. from mypy import stats
  17. from mypy.defaults import REPORTER_NAMES
  18. from mypy.nodes import Expression, FuncDef, MypyFile
  19. from mypy.options import Options
  20. from mypy.traverser import TraverserVisitor
  21. from mypy.types import Type, TypeOfAny
  22. from mypy.version import __version__
  23. try:
  24. from lxml import etree # type: ignore[import]
  25. LXML_INSTALLED = True
  26. except ImportError:
  27. LXML_INSTALLED = False
  28. type_of_any_name_map: Final[collections.OrderedDict[int, str]] = collections.OrderedDict(
  29. [
  30. (TypeOfAny.unannotated, "Unannotated"),
  31. (TypeOfAny.explicit, "Explicit"),
  32. (TypeOfAny.from_unimported_type, "Unimported"),
  33. (TypeOfAny.from_omitted_generics, "Omitted Generics"),
  34. (TypeOfAny.from_error, "Error"),
  35. (TypeOfAny.special_form, "Special Form"),
  36. (TypeOfAny.implementation_artifact, "Implementation Artifact"),
  37. ]
  38. )
  39. ReporterClasses: _TypeAlias = Dict[
  40. str, Tuple[Callable[["Reports", str], "AbstractReporter"], bool]
  41. ]
  42. reporter_classes: Final[ReporterClasses] = {}
  43. class Reports:
  44. def __init__(self, data_dir: str, report_dirs: dict[str, str]) -> None:
  45. self.data_dir = data_dir
  46. self.reporters: list[AbstractReporter] = []
  47. self.named_reporters: dict[str, AbstractReporter] = {}
  48. for report_type, report_dir in sorted(report_dirs.items()):
  49. self.add_report(report_type, report_dir)
  50. def add_report(self, report_type: str, report_dir: str) -> AbstractReporter:
  51. try:
  52. return self.named_reporters[report_type]
  53. except KeyError:
  54. pass
  55. reporter_cls, needs_lxml = reporter_classes[report_type]
  56. if needs_lxml and not LXML_INSTALLED:
  57. print(
  58. (
  59. "You must install the lxml package before you can run mypy"
  60. " with `--{}-report`.\n"
  61. "You can do this with `python3 -m pip install lxml`."
  62. ).format(report_type),
  63. file=sys.stderr,
  64. )
  65. raise ImportError
  66. reporter = reporter_cls(self, report_dir)
  67. self.reporters.append(reporter)
  68. self.named_reporters[report_type] = reporter
  69. return reporter
  70. def file(
  71. self,
  72. tree: MypyFile,
  73. modules: dict[str, MypyFile],
  74. type_map: dict[Expression, Type],
  75. options: Options,
  76. ) -> None:
  77. for reporter in self.reporters:
  78. reporter.on_file(tree, modules, type_map, options)
  79. def finish(self) -> None:
  80. for reporter in self.reporters:
  81. reporter.on_finish()
  82. class AbstractReporter(metaclass=ABCMeta):
  83. def __init__(self, reports: Reports, output_dir: str) -> None:
  84. self.output_dir = output_dir
  85. if output_dir != "<memory>":
  86. stats.ensure_dir_exists(output_dir)
  87. @abstractmethod
  88. def on_file(
  89. self,
  90. tree: MypyFile,
  91. modules: dict[str, MypyFile],
  92. type_map: dict[Expression, Type],
  93. options: Options,
  94. ) -> None:
  95. pass
  96. @abstractmethod
  97. def on_finish(self) -> None:
  98. pass
  99. def register_reporter(
  100. report_name: str,
  101. reporter: Callable[[Reports, str], AbstractReporter],
  102. needs_lxml: bool = False,
  103. ) -> None:
  104. reporter_classes[report_name] = (reporter, needs_lxml)
  105. def alias_reporter(source_reporter: str, target_reporter: str) -> None:
  106. reporter_classes[target_reporter] = reporter_classes[source_reporter]
  107. def should_skip_path(path: str) -> bool:
  108. if stats.is_special_module(path):
  109. return True
  110. if path.startswith(".."):
  111. return True
  112. if "stubs" in path.split("/") or "stubs" in path.split(os.sep):
  113. return True
  114. return False
  115. def iterate_python_lines(path: str) -> Iterator[tuple[int, str]]:
  116. """Return an iterator over (line number, line text) from a Python file."""
  117. try:
  118. with tokenize.open(path) as input_file:
  119. yield from enumerate(input_file, 1)
  120. except IsADirectoryError:
  121. # can happen with namespace packages
  122. pass
  123. class FuncCounterVisitor(TraverserVisitor):
  124. def __init__(self) -> None:
  125. super().__init__()
  126. self.counts = [0, 0]
  127. def visit_func_def(self, defn: FuncDef) -> None:
  128. self.counts[defn.type is not None] += 1
  129. class LineCountReporter(AbstractReporter):
  130. def __init__(self, reports: Reports, output_dir: str) -> None:
  131. super().__init__(reports, output_dir)
  132. self.counts: dict[str, tuple[int, int, int, int]] = {}
  133. def on_file(
  134. self,
  135. tree: MypyFile,
  136. modules: dict[str, MypyFile],
  137. type_map: dict[Expression, Type],
  138. options: Options,
  139. ) -> None:
  140. # Count physical lines. This assumes the file's encoding is a
  141. # superset of ASCII (or at least uses \n in its line endings).
  142. with open(tree.path, "rb") as f:
  143. physical_lines = len(f.readlines())
  144. func_counter = FuncCounterVisitor()
  145. tree.accept(func_counter)
  146. unannotated_funcs, annotated_funcs = func_counter.counts
  147. total_funcs = annotated_funcs + unannotated_funcs
  148. # Don't count lines or functions as annotated if they have their errors ignored.
  149. if options.ignore_errors:
  150. annotated_funcs = 0
  151. imputed_annotated_lines = (
  152. physical_lines * annotated_funcs // total_funcs if total_funcs else physical_lines
  153. )
  154. self.counts[tree._fullname] = (
  155. imputed_annotated_lines,
  156. physical_lines,
  157. annotated_funcs,
  158. total_funcs,
  159. )
  160. def on_finish(self) -> None:
  161. counts: list[tuple[tuple[int, int, int, int], str]] = sorted(
  162. ((c, p) for p, c in self.counts.items()), reverse=True
  163. )
  164. total_counts = tuple(sum(c[i] for c, p in counts) for i in range(4))
  165. with open(os.path.join(self.output_dir, "linecount.txt"), "w") as f:
  166. f.write("{:7} {:7} {:6} {:6} total\n".format(*total_counts))
  167. for c, p in counts:
  168. f.write(f"{c[0]:7} {c[1]:7} {c[2]:6} {c[3]:6} {p}\n")
  169. register_reporter("linecount", LineCountReporter)
  170. class AnyExpressionsReporter(AbstractReporter):
  171. """Report frequencies of different kinds of Any types."""
  172. def __init__(self, reports: Reports, output_dir: str) -> None:
  173. super().__init__(reports, output_dir)
  174. self.counts: dict[str, tuple[int, int]] = {}
  175. self.any_types_counter: dict[str, collections.Counter[int]] = {}
  176. def on_file(
  177. self,
  178. tree: MypyFile,
  179. modules: dict[str, MypyFile],
  180. type_map: dict[Expression, Type],
  181. options: Options,
  182. ) -> None:
  183. visitor = stats.StatisticsVisitor(
  184. inferred=True,
  185. filename=tree.fullname,
  186. modules=modules,
  187. typemap=type_map,
  188. all_nodes=True,
  189. visit_untyped_defs=False,
  190. )
  191. tree.accept(visitor)
  192. self.any_types_counter[tree.fullname] = visitor.type_of_any_counter
  193. num_unanalyzed_lines = list(visitor.line_map.values()).count(stats.TYPE_UNANALYZED)
  194. # count each line of dead code as one expression of type "Any"
  195. num_any = visitor.num_any_exprs + num_unanalyzed_lines
  196. num_total = visitor.num_imprecise_exprs + visitor.num_precise_exprs + num_any
  197. if num_total > 0:
  198. self.counts[tree.fullname] = (num_any, num_total)
  199. def on_finish(self) -> None:
  200. self._report_any_exprs()
  201. self._report_types_of_anys()
  202. def _write_out_report(
  203. self, filename: str, header: list[str], rows: list[list[str]], footer: list[str]
  204. ) -> None:
  205. row_len = len(header)
  206. assert all(len(row) == row_len for row in rows + [header, footer])
  207. min_column_distance = 3 # minimum distance between numbers in two columns
  208. widths = [-1] * row_len
  209. for row in rows + [header, footer]:
  210. for i, value in enumerate(row):
  211. widths[i] = max(widths[i], len(value))
  212. for i, w in enumerate(widths):
  213. # Do not add min_column_distance to the first column.
  214. if i > 0:
  215. widths[i] = w + min_column_distance
  216. with open(os.path.join(self.output_dir, filename), "w") as f:
  217. header_str = ("{:>{}}" * len(widths)).format(*itertools.chain(*zip(header, widths)))
  218. separator = "-" * len(header_str)
  219. f.write(header_str + "\n")
  220. f.write(separator + "\n")
  221. for row_values in rows:
  222. r = ("{:>{}}" * len(widths)).format(*itertools.chain(*zip(row_values, widths)))
  223. f.write(r + "\n")
  224. f.write(separator + "\n")
  225. footer_str = ("{:>{}}" * len(widths)).format(*itertools.chain(*zip(footer, widths)))
  226. f.write(footer_str + "\n")
  227. def _report_any_exprs(self) -> None:
  228. total_any = sum(num_any for num_any, _ in self.counts.values())
  229. total_expr = sum(total for _, total in self.counts.values())
  230. total_coverage = 100.0
  231. if total_expr > 0:
  232. total_coverage = (float(total_expr - total_any) / float(total_expr)) * 100
  233. column_names = ["Name", "Anys", "Exprs", "Coverage"]
  234. rows: list[list[str]] = []
  235. for filename in sorted(self.counts):
  236. (num_any, num_total) = self.counts[filename]
  237. coverage = (float(num_total - num_any) / float(num_total)) * 100
  238. coverage_str = f"{coverage:.2f}%"
  239. rows.append([filename, str(num_any), str(num_total), coverage_str])
  240. rows.sort(key=lambda x: x[0])
  241. total_row = ["Total", str(total_any), str(total_expr), f"{total_coverage:.2f}%"]
  242. self._write_out_report("any-exprs.txt", column_names, rows, total_row)
  243. def _report_types_of_anys(self) -> None:
  244. total_counter: collections.Counter[int] = collections.Counter()
  245. for counter in self.any_types_counter.values():
  246. for any_type, value in counter.items():
  247. total_counter[any_type] += value
  248. file_column_name = "Name"
  249. total_row_name = "Total"
  250. column_names = [file_column_name] + list(type_of_any_name_map.values())
  251. rows: list[list[str]] = []
  252. for filename, counter in self.any_types_counter.items():
  253. rows.append([filename] + [str(counter[typ]) for typ in type_of_any_name_map])
  254. rows.sort(key=lambda x: x[0])
  255. total_row = [total_row_name] + [str(total_counter[typ]) for typ in type_of_any_name_map]
  256. self._write_out_report("types-of-anys.txt", column_names, rows, total_row)
  257. register_reporter("any-exprs", AnyExpressionsReporter)
  258. class LineCoverageVisitor(TraverserVisitor):
  259. def __init__(self, source: list[str]) -> None:
  260. self.source = source
  261. # For each line of source, we maintain a pair of
  262. # * the indentation level of the surrounding function
  263. # (-1 if not inside a function), and
  264. # * whether the surrounding function is typed.
  265. # Initially, everything is covered at indentation level -1.
  266. self.lines_covered = [(-1, True) for l in source]
  267. # The Python AST has position information for the starts of
  268. # elements, but not for their ends. Fortunately the
  269. # indentation-based syntax makes it pretty easy to find where a
  270. # block ends without doing any real parsing.
  271. # TODO: Handle line continuations (explicit and implicit) and
  272. # multi-line string literals. (But at least line continuations
  273. # are normally more indented than their surrounding block anyways,
  274. # by PEP 8.)
  275. def indentation_level(self, line_number: int) -> int | None:
  276. """Return the indentation of a line of the source (specified by
  277. zero-indexed line number). Returns None for blank lines or comments."""
  278. line = self.source[line_number]
  279. indent = 0
  280. for char in list(line):
  281. if char == " ":
  282. indent += 1
  283. elif char == "\t":
  284. indent = 8 * ((indent + 8) // 8)
  285. elif char == "#":
  286. # Line is a comment; ignore it
  287. return None
  288. elif char == "\n":
  289. # Line is entirely whitespace; ignore it
  290. return None
  291. # TODO line continuation (\)
  292. else:
  293. # Found a non-whitespace character
  294. return indent
  295. # Line is entirely whitespace, and at end of file
  296. # with no trailing newline; ignore it
  297. return None
  298. def visit_func_def(self, defn: FuncDef) -> None:
  299. start_line = defn.line - 1
  300. start_indent = None
  301. # When a function is decorated, sometimes the start line will point to
  302. # whitespace or comments between the decorator and the function, so
  303. # we have to look for the start.
  304. while start_line < len(self.source):
  305. start_indent = self.indentation_level(start_line)
  306. if start_indent is not None:
  307. break
  308. start_line += 1
  309. # If we can't find the function give up and don't annotate anything.
  310. # Our line numbers are not reliable enough to be asserting on.
  311. if start_indent is None:
  312. return
  313. cur_line = start_line + 1
  314. end_line = cur_line
  315. # After this loop, function body will be lines [start_line, end_line)
  316. while cur_line < len(self.source):
  317. cur_indent = self.indentation_level(cur_line)
  318. if cur_indent is None:
  319. # Consume the line, but don't mark it as belonging to the function yet.
  320. cur_line += 1
  321. elif cur_indent > start_indent:
  322. # A non-blank line that belongs to the function.
  323. cur_line += 1
  324. end_line = cur_line
  325. else:
  326. # We reached a line outside the function definition.
  327. break
  328. is_typed = defn.type is not None
  329. for line in range(start_line, end_line):
  330. old_indent, _ = self.lines_covered[line]
  331. # If there was an old indent level for this line, and the new
  332. # level isn't increasing the indentation, ignore it.
  333. # This is to be defensive against funniness in our line numbers,
  334. # which are not always reliable.
  335. if old_indent <= start_indent:
  336. self.lines_covered[line] = (start_indent, is_typed)
  337. # Visit the body, in case there are nested functions
  338. super().visit_func_def(defn)
  339. class LineCoverageReporter(AbstractReporter):
  340. """Exact line coverage reporter.
  341. This reporter writes a JSON dictionary with one field 'lines' to
  342. the file 'coverage.json' in the specified report directory. The
  343. value of that field is a dictionary which associates to each
  344. source file's absolute pathname the list of line numbers that
  345. belong to typed functions in that file.
  346. """
  347. def __init__(self, reports: Reports, output_dir: str) -> None:
  348. super().__init__(reports, output_dir)
  349. self.lines_covered: dict[str, list[int]] = {}
  350. def on_file(
  351. self,
  352. tree: MypyFile,
  353. modules: dict[str, MypyFile],
  354. type_map: dict[Expression, Type],
  355. options: Options,
  356. ) -> None:
  357. with open(tree.path) as f:
  358. tree_source = f.readlines()
  359. coverage_visitor = LineCoverageVisitor(tree_source)
  360. tree.accept(coverage_visitor)
  361. covered_lines = []
  362. for line_number, (_, typed) in enumerate(coverage_visitor.lines_covered):
  363. if typed:
  364. covered_lines.append(line_number + 1)
  365. self.lines_covered[os.path.abspath(tree.path)] = covered_lines
  366. def on_finish(self) -> None:
  367. with open(os.path.join(self.output_dir, "coverage.json"), "w") as f:
  368. json.dump({"lines": self.lines_covered}, f)
  369. register_reporter("linecoverage", LineCoverageReporter)
  370. class FileInfo:
  371. def __init__(self, name: str, module: str) -> None:
  372. self.name = name
  373. self.module = module
  374. self.counts = [0] * len(stats.precision_names)
  375. def total(self) -> int:
  376. return sum(self.counts)
  377. def attrib(self) -> dict[str, str]:
  378. return {name: str(val) for name, val in sorted(zip(stats.precision_names, self.counts))}
  379. class MemoryXmlReporter(AbstractReporter):
  380. """Internal reporter that generates XML in memory.
  381. This is used by all other XML-based reporters to avoid duplication.
  382. """
  383. def __init__(self, reports: Reports, output_dir: str) -> None:
  384. super().__init__(reports, output_dir)
  385. self.xslt_html_path = os.path.join(reports.data_dir, "xml", "mypy-html.xslt")
  386. self.xslt_txt_path = os.path.join(reports.data_dir, "xml", "mypy-txt.xslt")
  387. self.css_html_path = os.path.join(reports.data_dir, "xml", "mypy-html.css")
  388. xsd_path = os.path.join(reports.data_dir, "xml", "mypy.xsd")
  389. self.schema = etree.XMLSchema(etree.parse(xsd_path))
  390. self.last_xml: Any | None = None
  391. self.files: list[FileInfo] = []
  392. # XML doesn't like control characters, but they are sometimes
  393. # legal in source code (e.g. comments, string literals).
  394. # Tabs (#x09) are allowed in XML content.
  395. control_fixer: Final = str.maketrans("".join(chr(i) for i in range(32) if i != 9), "?" * 31)
  396. def on_file(
  397. self,
  398. tree: MypyFile,
  399. modules: dict[str, MypyFile],
  400. type_map: dict[Expression, Type],
  401. options: Options,
  402. ) -> None:
  403. self.last_xml = None
  404. try:
  405. path = os.path.relpath(tree.path)
  406. except ValueError:
  407. return
  408. if should_skip_path(path) or os.path.isdir(path):
  409. return # `path` can sometimes be a directory, see #11334
  410. visitor = stats.StatisticsVisitor(
  411. inferred=True,
  412. filename=tree.fullname,
  413. modules=modules,
  414. typemap=type_map,
  415. all_nodes=True,
  416. )
  417. tree.accept(visitor)
  418. root = etree.Element("mypy-report-file", name=path, module=tree._fullname)
  419. doc = etree.ElementTree(root)
  420. file_info = FileInfo(path, tree._fullname)
  421. for lineno, line_text in iterate_python_lines(path):
  422. status = visitor.line_map.get(lineno, stats.TYPE_EMPTY)
  423. file_info.counts[status] += 1
  424. etree.SubElement(
  425. root,
  426. "line",
  427. any_info=self._get_any_info_for_line(visitor, lineno),
  428. content=line_text.rstrip("\n").translate(self.control_fixer),
  429. number=str(lineno),
  430. precision=stats.precision_names[status],
  431. )
  432. # Assumes a layout similar to what XmlReporter uses.
  433. xslt_path = os.path.relpath("mypy-html.xslt", path)
  434. transform_pi = etree.ProcessingInstruction(
  435. "xml-stylesheet", f'type="text/xsl" href="{pathname2url(xslt_path)}"'
  436. )
  437. root.addprevious(transform_pi)
  438. self.schema.assertValid(doc)
  439. self.last_xml = doc
  440. self.files.append(file_info)
  441. @staticmethod
  442. def _get_any_info_for_line(visitor: stats.StatisticsVisitor, lineno: int) -> str:
  443. if lineno in visitor.any_line_map:
  444. result = "Any Types on this line: "
  445. counter: collections.Counter[int] = collections.Counter()
  446. for typ in visitor.any_line_map[lineno]:
  447. counter[typ.type_of_any] += 1
  448. for any_type, occurrences in counter.items():
  449. result += f"\n{type_of_any_name_map[any_type]} (x{occurrences})"
  450. return result
  451. else:
  452. return "No Anys on this line!"
  453. def on_finish(self) -> None:
  454. self.last_xml = None
  455. # index_path = os.path.join(self.output_dir, 'index.xml')
  456. output_files = sorted(self.files, key=lambda x: x.module)
  457. root = etree.Element("mypy-report-index", name="index")
  458. doc = etree.ElementTree(root)
  459. for file_info in output_files:
  460. etree.SubElement(
  461. root,
  462. "file",
  463. file_info.attrib(),
  464. module=file_info.module,
  465. name=pathname2url(file_info.name),
  466. total=str(file_info.total()),
  467. )
  468. xslt_path = os.path.relpath("mypy-html.xslt", ".")
  469. transform_pi = etree.ProcessingInstruction(
  470. "xml-stylesheet", f'type="text/xsl" href="{pathname2url(xslt_path)}"'
  471. )
  472. root.addprevious(transform_pi)
  473. self.schema.assertValid(doc)
  474. self.last_xml = doc
  475. register_reporter("memory-xml", MemoryXmlReporter, needs_lxml=True)
  476. def get_line_rate(covered_lines: int, total_lines: int) -> str:
  477. if total_lines == 0:
  478. return str(1.0)
  479. else:
  480. return f"{covered_lines / total_lines:.4f}"
  481. class CoberturaPackage:
  482. """Container for XML and statistics mapping python modules to Cobertura package."""
  483. def __init__(self, name: str) -> None:
  484. self.name = name
  485. self.classes: dict[str, Any] = {}
  486. self.packages: dict[str, CoberturaPackage] = {}
  487. self.total_lines = 0
  488. self.covered_lines = 0
  489. def as_xml(self) -> Any:
  490. package_element = etree.Element("package", complexity="1.0", name=self.name)
  491. package_element.attrib["branch-rate"] = "0"
  492. package_element.attrib["line-rate"] = get_line_rate(self.covered_lines, self.total_lines)
  493. classes_element = etree.SubElement(package_element, "classes")
  494. for class_name in sorted(self.classes):
  495. classes_element.append(self.classes[class_name])
  496. self.add_packages(package_element)
  497. return package_element
  498. def add_packages(self, parent_element: Any) -> None:
  499. if self.packages:
  500. packages_element = etree.SubElement(parent_element, "packages")
  501. for package in sorted(self.packages.values(), key=attrgetter("name")):
  502. packages_element.append(package.as_xml())
  503. class CoberturaXmlReporter(AbstractReporter):
  504. """Reporter for generating Cobertura compliant XML."""
  505. def __init__(self, reports: Reports, output_dir: str) -> None:
  506. super().__init__(reports, output_dir)
  507. self.root = etree.Element("coverage", timestamp=str(int(time.time())), version=__version__)
  508. self.doc = etree.ElementTree(self.root)
  509. self.root_package = CoberturaPackage(".")
  510. def on_file(
  511. self,
  512. tree: MypyFile,
  513. modules: dict[str, MypyFile],
  514. type_map: dict[Expression, Type],
  515. options: Options,
  516. ) -> None:
  517. path = os.path.relpath(tree.path)
  518. visitor = stats.StatisticsVisitor(
  519. inferred=True,
  520. filename=tree.fullname,
  521. modules=modules,
  522. typemap=type_map,
  523. all_nodes=True,
  524. )
  525. tree.accept(visitor)
  526. class_name = os.path.basename(path)
  527. file_info = FileInfo(path, tree._fullname)
  528. class_element = etree.Element("class", complexity="1.0", filename=path, name=class_name)
  529. etree.SubElement(class_element, "methods")
  530. lines_element = etree.SubElement(class_element, "lines")
  531. class_lines_covered = 0
  532. class_total_lines = 0
  533. for lineno, _ in iterate_python_lines(path):
  534. status = visitor.line_map.get(lineno, stats.TYPE_EMPTY)
  535. hits = 0
  536. branch = False
  537. if status == stats.TYPE_EMPTY:
  538. continue
  539. class_total_lines += 1
  540. if status != stats.TYPE_ANY:
  541. class_lines_covered += 1
  542. hits = 1
  543. if status == stats.TYPE_IMPRECISE:
  544. branch = True
  545. file_info.counts[status] += 1
  546. line_element = etree.SubElement(
  547. lines_element,
  548. "line",
  549. branch=str(branch).lower(),
  550. hits=str(hits),
  551. number=str(lineno),
  552. precision=stats.precision_names[status],
  553. )
  554. if branch:
  555. line_element.attrib["condition-coverage"] = "50% (1/2)"
  556. class_element.attrib["branch-rate"] = "0"
  557. class_element.attrib["line-rate"] = get_line_rate(class_lines_covered, class_total_lines)
  558. # parent_module is set to whichever module contains this file. For most files, we want
  559. # to simply strip the last element off of the module. But for __init__.py files,
  560. # the module == the parent module.
  561. parent_module = file_info.module.rsplit(".", 1)[0]
  562. if file_info.name.endswith("__init__.py"):
  563. parent_module = file_info.module
  564. if parent_module not in self.root_package.packages:
  565. self.root_package.packages[parent_module] = CoberturaPackage(parent_module)
  566. current_package = self.root_package.packages[parent_module]
  567. packages_to_update = [self.root_package, current_package]
  568. for package in packages_to_update:
  569. package.total_lines += class_total_lines
  570. package.covered_lines += class_lines_covered
  571. current_package.classes[class_name] = class_element
  572. def on_finish(self) -> None:
  573. self.root.attrib["line-rate"] = get_line_rate(
  574. self.root_package.covered_lines, self.root_package.total_lines
  575. )
  576. self.root.attrib["branch-rate"] = "0"
  577. sources = etree.SubElement(self.root, "sources")
  578. source_element = etree.SubElement(sources, "source")
  579. source_element.text = os.getcwd()
  580. self.root_package.add_packages(self.root)
  581. out_path = os.path.join(self.output_dir, "cobertura.xml")
  582. self.doc.write(out_path, encoding="utf-8", pretty_print=True)
  583. print("Generated Cobertura report:", os.path.abspath(out_path))
  584. register_reporter("cobertura-xml", CoberturaXmlReporter, needs_lxml=True)
  585. class AbstractXmlReporter(AbstractReporter):
  586. """Internal abstract class for reporters that work via XML."""
  587. def __init__(self, reports: Reports, output_dir: str) -> None:
  588. super().__init__(reports, output_dir)
  589. memory_reporter = reports.add_report("memory-xml", "<memory>")
  590. assert isinstance(memory_reporter, MemoryXmlReporter)
  591. # The dependency will be called first.
  592. self.memory_xml = memory_reporter
  593. class XmlReporter(AbstractXmlReporter):
  594. """Public reporter that exports XML.
  595. The produced XML files contain a reference to the absolute path
  596. of the html transform, so they will be locally viewable in a browser.
  597. However, there is a bug in Chrome and all other WebKit-based browsers
  598. that makes it fail from file:// URLs but work on http:// URLs.
  599. """
  600. def on_file(
  601. self,
  602. tree: MypyFile,
  603. modules: dict[str, MypyFile],
  604. type_map: dict[Expression, Type],
  605. options: Options,
  606. ) -> None:
  607. last_xml = self.memory_xml.last_xml
  608. if last_xml is None:
  609. return
  610. path = os.path.relpath(tree.path)
  611. if path.startswith(".."):
  612. return
  613. out_path = os.path.join(self.output_dir, "xml", path + ".xml")
  614. stats.ensure_dir_exists(os.path.dirname(out_path))
  615. last_xml.write(out_path, encoding="utf-8")
  616. def on_finish(self) -> None:
  617. last_xml = self.memory_xml.last_xml
  618. assert last_xml is not None
  619. out_path = os.path.join(self.output_dir, "index.xml")
  620. out_xslt = os.path.join(self.output_dir, "mypy-html.xslt")
  621. out_css = os.path.join(self.output_dir, "mypy-html.css")
  622. last_xml.write(out_path, encoding="utf-8")
  623. shutil.copyfile(self.memory_xml.xslt_html_path, out_xslt)
  624. shutil.copyfile(self.memory_xml.css_html_path, out_css)
  625. print("Generated XML report:", os.path.abspath(out_path))
  626. register_reporter("xml", XmlReporter, needs_lxml=True)
  627. class XsltHtmlReporter(AbstractXmlReporter):
  628. """Public reporter that exports HTML via XSLT.
  629. This is slightly different than running `xsltproc` on the .xml files,
  630. because it passes a parameter to rewrite the links.
  631. """
  632. def __init__(self, reports: Reports, output_dir: str) -> None:
  633. super().__init__(reports, output_dir)
  634. self.xslt_html = etree.XSLT(etree.parse(self.memory_xml.xslt_html_path))
  635. self.param_html = etree.XSLT.strparam("html")
  636. def on_file(
  637. self,
  638. tree: MypyFile,
  639. modules: dict[str, MypyFile],
  640. type_map: dict[Expression, Type],
  641. options: Options,
  642. ) -> None:
  643. last_xml = self.memory_xml.last_xml
  644. if last_xml is None:
  645. return
  646. path = os.path.relpath(tree.path)
  647. if path.startswith(".."):
  648. return
  649. out_path = os.path.join(self.output_dir, "html", path + ".html")
  650. stats.ensure_dir_exists(os.path.dirname(out_path))
  651. transformed_html = bytes(self.xslt_html(last_xml, ext=self.param_html))
  652. with open(out_path, "wb") as out_file:
  653. out_file.write(transformed_html)
  654. def on_finish(self) -> None:
  655. last_xml = self.memory_xml.last_xml
  656. assert last_xml is not None
  657. out_path = os.path.join(self.output_dir, "index.html")
  658. out_css = os.path.join(self.output_dir, "mypy-html.css")
  659. transformed_html = bytes(self.xslt_html(last_xml, ext=self.param_html))
  660. with open(out_path, "wb") as out_file:
  661. out_file.write(transformed_html)
  662. shutil.copyfile(self.memory_xml.css_html_path, out_css)
  663. print("Generated HTML report (via XSLT):", os.path.abspath(out_path))
  664. register_reporter("xslt-html", XsltHtmlReporter, needs_lxml=True)
  665. class XsltTxtReporter(AbstractXmlReporter):
  666. """Public reporter that exports TXT via XSLT.
  667. Currently this only does the summary, not the individual reports.
  668. """
  669. def __init__(self, reports: Reports, output_dir: str) -> None:
  670. super().__init__(reports, output_dir)
  671. self.xslt_txt = etree.XSLT(etree.parse(self.memory_xml.xslt_txt_path))
  672. def on_file(
  673. self,
  674. tree: MypyFile,
  675. modules: dict[str, MypyFile],
  676. type_map: dict[Expression, Type],
  677. options: Options,
  678. ) -> None:
  679. pass
  680. def on_finish(self) -> None:
  681. last_xml = self.memory_xml.last_xml
  682. assert last_xml is not None
  683. out_path = os.path.join(self.output_dir, "index.txt")
  684. transformed_txt = bytes(self.xslt_txt(last_xml))
  685. with open(out_path, "wb") as out_file:
  686. out_file.write(transformed_txt)
  687. print("Generated TXT report (via XSLT):", os.path.abspath(out_path))
  688. register_reporter("xslt-txt", XsltTxtReporter, needs_lxml=True)
  689. alias_reporter("xslt-html", "html")
  690. alias_reporter("xslt-txt", "txt")
  691. class LinePrecisionReporter(AbstractReporter):
  692. """Report per-module line counts for typing precision.
  693. Each line is classified into one of these categories:
  694. * precise (fully type checked)
  695. * imprecise (Any types in a type component, such as List[Any])
  696. * any (something with an Any type, implicit or explicit)
  697. * empty (empty line, comment or docstring)
  698. * unanalyzed (mypy considers line unreachable)
  699. The meaning of these categories varies slightly depending on
  700. context.
  701. """
  702. def __init__(self, reports: Reports, output_dir: str) -> None:
  703. super().__init__(reports, output_dir)
  704. self.files: list[FileInfo] = []
  705. def on_file(
  706. self,
  707. tree: MypyFile,
  708. modules: dict[str, MypyFile],
  709. type_map: dict[Expression, Type],
  710. options: Options,
  711. ) -> None:
  712. try:
  713. path = os.path.relpath(tree.path)
  714. except ValueError:
  715. return
  716. if should_skip_path(path):
  717. return
  718. visitor = stats.StatisticsVisitor(
  719. inferred=True,
  720. filename=tree.fullname,
  721. modules=modules,
  722. typemap=type_map,
  723. all_nodes=True,
  724. )
  725. tree.accept(visitor)
  726. file_info = FileInfo(path, tree._fullname)
  727. for lineno, _ in iterate_python_lines(path):
  728. status = visitor.line_map.get(lineno, stats.TYPE_EMPTY)
  729. file_info.counts[status] += 1
  730. self.files.append(file_info)
  731. def on_finish(self) -> None:
  732. if not self.files:
  733. # Nothing to do.
  734. return
  735. output_files = sorted(self.files, key=lambda x: x.module)
  736. report_file = os.path.join(self.output_dir, "lineprecision.txt")
  737. width = max(4, max(len(info.module) for info in output_files))
  738. titles = ("Lines", "Precise", "Imprecise", "Any", "Empty", "Unanalyzed")
  739. widths = (width,) + tuple(len(t) for t in titles)
  740. fmt = "{:%d} {:%d} {:%d} {:%d} {:%d} {:%d} {:%d}\n" % widths
  741. with open(report_file, "w") as f:
  742. f.write(fmt.format("Name", *titles))
  743. f.write("-" * (width + 51) + "\n")
  744. for file_info in output_files:
  745. counts = file_info.counts
  746. f.write(
  747. fmt.format(
  748. file_info.module.ljust(width),
  749. file_info.total(),
  750. counts[stats.TYPE_PRECISE],
  751. counts[stats.TYPE_IMPRECISE],
  752. counts[stats.TYPE_ANY],
  753. counts[stats.TYPE_EMPTY],
  754. counts[stats.TYPE_UNANALYZED],
  755. )
  756. )
  757. register_reporter("lineprecision", LinePrecisionReporter)
  758. # Reporter class names are defined twice to speed up mypy startup, as this
  759. # module is slow to import. Ensure that the two definitions match.
  760. assert set(reporter_classes) == set(REPORTER_NAMES)