|
| 1 | +"""The API reference, generated from the package that ships. |
| 2 | +
|
| 3 | +A reference written by hand beside the code is a reference that is |
| 4 | +wrong by the second release, and wrong in the way that costs the most: |
| 5 | +it looks maintained. So this one is built from the installed package, |
| 6 | +by inspection, and the release builds it from the same wheel it is |
| 7 | +about to publish. |
| 8 | +
|
| 9 | + python tools/reference.py docs/api |
| 10 | +
|
| 11 | +pdoc is the generator because it reads a package the way the |
| 12 | +interpreter does and asks for nothing in return: no configuration file, |
| 13 | +no theme to keep, no second copy of the module list. What it produces |
| 14 | +is HTML that opens from disk, which matters for a client whose docs go |
| 15 | +out as an artifact of the release rather than as a deployment. |
| 16 | +
|
| 17 | +Half of this package is compiled, and that is the one thing the |
| 18 | +inspection cannot see through. `zudb.connect` is a function object |
| 19 | +pyo3 built, so its signature is `(path, *, read_only=False, ...)` and |
| 20 | +the types are in `_zudb.pyi`, where the type checkers read them. pdoc |
| 21 | +knows about stub files and looks for one named after the module it is |
| 22 | +documenting, which is `zudb`, and the stub is named after the module |
| 23 | +the names were defined in, which is `zudb._zudb`. So the stub is put |
| 24 | +where pdoc looks, under the `-stubs` name PEP 561 reserves for exactly |
| 25 | +this, and the one relative import in it is rewritten because a stub |
| 26 | +loaded outside the import machinery has no package to be relative to. |
| 27 | +
|
| 28 | +Derived rather than written, which is the point: there is no second |
| 29 | +declaration of the surface to keep in step, and `bare()` below fails |
| 30 | +the build if the overlay stops landing. Without that check a reference |
| 31 | +that quietly lost every type annotation would still be a reference that |
| 32 | +built, and nobody reads their own generated docs closely enough to |
| 33 | +notice. |
| 34 | +""" |
| 35 | + |
| 36 | +from __future__ import annotations |
| 37 | + |
| 38 | +import contextlib |
| 39 | +import shutil |
| 40 | +import sys |
| 41 | +import tempfile |
| 42 | +from collections.abc import Iterator |
| 43 | +from pathlib import Path |
| 44 | + |
| 45 | +#: What the reference covers, which is what a reader can import. The |
| 46 | +#: compiled module is not in the list: it is an implementation detail |
| 47 | +#: with an underscore on it, and every name it defines is re-exported |
| 48 | +#: from `zudb` and documented there. |
| 49 | +MODULES = [ |
| 50 | + "zudb", |
| 51 | + "zudb.aio", |
| 52 | + "zudb.dbapi", |
| 53 | + "zudb.errors", |
| 54 | + "zudb.magic", |
| 55 | + "zudb.types", |
| 56 | +] |
| 57 | + |
| 58 | +#: The name pdoc looks under, and PEP 561's name for a stub package |
| 59 | +#: distributed apart from the package it describes. `zudb/__init__.pyi` |
| 60 | +#: beside the real `__init__.py` would work as well and would be a |
| 61 | +#: second file in the wheel claiming to be the public surface, which is |
| 62 | +#: the thing this whole file exists to avoid. |
| 63 | +STUBS = "zudb-stubs" |
| 64 | + |
| 65 | + |
| 66 | +def overlay(package: Path, into: Path) -> Path: |
| 67 | + """Write the stub pdoc will find for `zudb`, and answer where. |
| 68 | +
|
| 69 | + The content is `_zudb.pyi`, unchanged except for its one relative |
| 70 | + import. pdoc loads a stub with a loader of its own rather than |
| 71 | + through `import`, so the module has no package and `from .types |
| 72 | + import Value` raises before anything is read; naming the package |
| 73 | + absolutely is the whole edit. |
| 74 | + """ |
| 75 | + stub = (package / "_zudb.pyi").read_text(encoding="utf-8") |
| 76 | + written = into / STUBS |
| 77 | + written.mkdir(parents=True, exist_ok=True) |
| 78 | + (written / "__init__.pyi").write_text( |
| 79 | + stub.replace("from .types import", "from zudb.types import"), encoding="utf-8" |
| 80 | + ) |
| 81 | + return written |
| 82 | + |
| 83 | + |
| 84 | +@contextlib.contextmanager |
| 85 | +def stubs() -> Iterator[Path]: |
| 86 | + """The overlay, on `sys.path`, for as long as the block runs. |
| 87 | +
|
| 88 | + pdoc remembers where a module's stub was and where it was not, and |
| 89 | + the answer changes here twice in one process. So the caches that |
| 90 | + hold it are dropped on the way in and on the way out, which is what |
| 91 | + lets a test ask the question both ways and get both answers. |
| 92 | + """ |
| 93 | + import pdoc.doc |
| 94 | + import pdoc.doc_pyi |
| 95 | + import zudb |
| 96 | + |
| 97 | + def forget() -> None: |
| 98 | + pdoc.doc_pyi.find_stub_file.cache_clear() |
| 99 | + pdoc.doc.Module.from_name.cache_clear() |
| 100 | + |
| 101 | + with tempfile.TemporaryDirectory() as scratch: |
| 102 | + written = overlay(Path(zudb.__file__ or "").parent, Path(scratch)) |
| 103 | + sys.path.insert(0, str(written.parent)) |
| 104 | + forget() |
| 105 | + try: |
| 106 | + yield written |
| 107 | + finally: |
| 108 | + sys.path.remove(str(written.parent)) |
| 109 | + forget() |
| 110 | + |
| 111 | + |
| 112 | +def bare(module: str) -> list[str]: |
| 113 | + """The names on `module` whose signature carries no annotation. |
| 114 | +
|
| 115 | + Which is the check that the overlay landed. Everything the compiled |
| 116 | + module defines is annotated in the stub, so after the overlay every |
| 117 | + parameter of every public function has a type on it, and a name |
| 118 | + here is a name pdoc documented from the object instead. An empty |
| 119 | + list is a reference worth publishing. |
| 120 | +
|
| 121 | + What a reader reads is what is counted: the members a namespace |
| 122 | + declares itself, under names with no underscore on the front. |
| 123 | + Inherited ones are somebody else's, and `object.__eq__` takes an |
| 124 | + untyped `value` on every class in the language. |
| 125 | + """ |
| 126 | + import pdoc.doc |
| 127 | + |
| 128 | + def functions(namespace: pdoc.doc.Namespace) -> list[pdoc.doc.Function]: |
| 129 | + found = [] |
| 130 | + for doc in namespace.own_members: |
| 131 | + if doc.name.startswith("_"): |
| 132 | + continue |
| 133 | + if isinstance(doc, pdoc.doc.Function): |
| 134 | + found.append(doc) |
| 135 | + elif isinstance(doc, pdoc.doc.Class): |
| 136 | + found.extend(functions(doc)) |
| 137 | + return found |
| 138 | + |
| 139 | + untyped = [] |
| 140 | + for one in functions(pdoc.doc.Module.from_name(module)): |
| 141 | + wanted = [ |
| 142 | + name |
| 143 | + for name, parameter in one.signature.parameters.items() |
| 144 | + if name not in ("self", "cls") and parameter.annotation is parameter.empty |
| 145 | + ] |
| 146 | + if wanted: |
| 147 | + untyped.append(f"{one.fullname}({', '.join(wanted)})") |
| 148 | + return untyped |
| 149 | + |
| 150 | + |
| 151 | +def build(into: Path) -> None: |
| 152 | + """The reference, in `into`, replacing whatever was there. |
| 153 | +
|
| 154 | + Replacing rather than merging, because a page for a module that was |
| 155 | + removed is worse than no page at all: it is a name a reader can |
| 156 | + still find, still linked from the index of a search built beside |
| 157 | + it, and gone from the package. |
| 158 | + """ |
| 159 | + import pdoc |
| 160 | + |
| 161 | + with stubs(): |
| 162 | + untyped = bare("zudb") |
| 163 | + if untyped: |
| 164 | + raise SystemExit( |
| 165 | + "the stub overlay did not land, so the reference would publish " |
| 166 | + f"{len(untyped)} name(s) with no types on them: {', '.join(sorted(untyped))}" |
| 167 | + ) |
| 168 | + shutil.rmtree(into, ignore_errors=True) |
| 169 | + pdoc.pdoc(*MODULES, output_directory=into) |
| 170 | + |
| 171 | + |
| 172 | +def main(argv: list[str]) -> int: |
| 173 | + if len(argv) != 2: |
| 174 | + print(f"usage: {argv[0]} <directory>", file=sys.stderr) |
| 175 | + return 2 |
| 176 | + into = Path(argv[1]) |
| 177 | + build(into) |
| 178 | + pages = sorted(path for path in into.rglob("*.html")) |
| 179 | + missing = [name for name in MODULES if not (into / (name.replace(".", "/") + ".html")).exists()] |
| 180 | + for name in missing: |
| 181 | + print(f"{name} was documented and has no page", file=sys.stderr) |
| 182 | + print(f"{len(pages)} pages in {into}, {len(MODULES)} modules") |
| 183 | + return 1 if missing else 0 |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + raise SystemExit(main(sys.argv)) |
0 commit comments