| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import json |
| 4 |
import os |
| 5 |
import textwrap |
| 6 |
from pathlib import Path |
| 7 |
from typing import Any |
| 8 |
|
| 9 |
from sphinx.addnodes import document |
| 10 |
from sphinx.application import Sphinx |
| 11 |
from sphinx.builders.html import StandaloneHTMLBuilder |
| 12 |
from sphinx.environment import BuildEnvironment |
| 13 |
|
| 14 |
from mypy.main import define_options |
| 15 |
|
| 16 |
|
| 17 |
class MypyHTMLBuilder(StandaloneHTMLBuilder): |
| 18 |
strict_file: Path |
| 19 |
|
| 20 |
def __init__(self, app: Sphinx, env: BuildEnvironment) -> None: |
| 21 |
super().__init__(app, env) |
| 22 |
self._ref_to_doc = {} |
| 23 |
self.strict_file = Path(self.srcdir) / "strict_list.rst" |
| 24 |
self._add_strict_list() |
| 25 |
|
| 26 |
def write_doc(self, docname: str, doctree: document) -> None: |
| 27 |
super().write_doc(docname, doctree) |
| 28 |
self._ref_to_doc.update({_id: docname for _id in doctree.ids}) |
| 29 |
|
| 30 |
def _add_strict_list(self) -> None: |
| 31 |
strict_flags: list[str] |
| 32 |
_, strict_flags, _ = define_options() |
| 33 |
strict_part = ", ".join(f":option:`{s} <mypy {s}>`" for s in strict_flags) |
| 34 |
if ( |
| 35 |
not strict_part |
| 36 |
or strict_part.isspace() |
| 37 |
or len(strict_part) < 20 |
| 38 |
or len(strict_part) > 2000 |
| 39 |
): |
| 40 |
raise ValueError(f"{strict_part=}, which doesn't look right (by a simple heuristic).") |
| 41 |
self.strict_file.write_text( |
| 42 |
"For this version of mypy, the list of flags enabled by strict is: " + strict_part |
| 43 |
) |
| 44 |
|
| 45 |
def _verify_error_codes(self) -> None: |
| 46 |
from mypy.errorcodes import error_codes |
| 47 |
|
| 48 |
missing_error_codes = {c for c in error_codes if f"code-{c}" not in self._ref_to_doc} |
| 49 |
if missing_error_codes: |
| 50 |
raise ValueError( |
| 51 |
f"Some error codes are not documented: {', '.join(sorted(missing_error_codes))}" |
| 52 |
) |
| 53 |
|
| 54 |
def _write_ref_redirector(self) -> None: |
| 55 |
if os.getenv("VERIFY_MYPY_ERROR_CODES"): |
| 56 |
self._verify_error_codes() |
| 57 |
p = Path(self.outdir) / "_refs.html" |
| 58 |
data = f""" |
| 59 |
<html> |
| 60 |
<body> |
| 61 |
<script> |
| 62 |
const ref_to_doc = {json.dumps(self._ref_to_doc)}; |
| 63 |
const hash = window.location.hash.substring(1); |
| 64 |
const doc = ref_to_doc[hash]; |
| 65 |
if (doc) {{ |
| 66 |
window.location.href = doc + '.html' + '#' + hash; |
| 67 |
}} else {{ |
| 68 |
window.document.innerText = 'Unknown reference: ' + hash; |
| 69 |
}} |
| 70 |
</script> |
| 71 |
</body> |
| 72 |
</html> |
| 73 |
""" |
| 74 |
p.write_text(textwrap.dedent(data)) |
| 75 |
|
| 76 |
def finish(self) -> None: |
| 77 |
super().finish() |
| 78 |
self._write_ref_redirector() |
| 79 |
self.strict_file.unlink() |
| 80 |
|
| 81 |
|
| 82 |
def setup(app: Sphinx) -> dict[str, Any]: |
| 83 |
app.add_builder(MypyHTMLBuilder, override=True) |
| 84 |
|
| 85 |
return {"version": "0.1", "parallel_read_safe": True, "parallel_write_safe": True} |