The API reference

Reference pages made from your code, and names in backticks that link to them.

smalldocs writes a reference page for each module of your package. A reference page lists the classes and the functions of a module, with their parameters. smalldocs reads what the code already says, so you write nothing twice:

The Reference of this site is made from the code of smalldocs in this way.

This page is about Python code. For a Rust crate, a TypeScript package or an HTTP API, see Rust, TypeScript and HTTP references.

Write the code

Put one parameter on each line, with its comment beside it. Put the comment for the return value after the colon of the def line. fastcore calls these comments docments.

def add(
    a: int,  # The first number
    b: int = 2,  # The second number
) -> int:  # Their sum
    "Add two numbers."
    return a + b

smalldocs shows other kinds of class in their own way:

  • A dataclass shows its constructor, with the comment beside each field.
  • A pydantic model shows its fields. The next section tells where the description of each field comes from.
  • A TypedDict shows its keys, with the comment beside each key.
  • A click command shows its function, with the comment beside each parameter. Then it shows a table of its options, with the help of each option. A click group shows the same.

If the __init__.py of a package defines functions or classes, the package has a page of its own.

The fields of a pydantic model

For each field, the description in the reference is:

  1. The description of the field, when it has one. This covers Field(description="..."), and a docstring under the field when the model sets ConfigDict(use_attribute_docstrings=True).
  2. Otherwise, the comment beside the field.
  3. Otherwise, nothing, and smalldocs api check reports the gap, for example Page.title: no description.

A field with a description and a comment is documented, and the reference shows the description. A comment for a tool, such as # type: ignore or # noqa, is not a description.

class Page(BaseModel):
    """A page of Wikipedia: its title and the summary of its text."""

    title: str  # The title of the page
    extract: str = Field(description="The summary: the first paragraph of the page")

Each style is good for a different kind of model:

  • Field(description="...") is data that pydantic keeps when the program runs. It goes into model_json_schema(), and so into an OpenAPI schema, a FastAPI page or the tool schema of an LLM. Use it for a model that is the schema of an API. Then the one description goes into the reference and into the schema.
  • The comment is lighter, and it reads like the comments of a function. For a model that only parses data, nothing but the reference reads a description, so the comment is enough there.

Turn on the reference

Add an api section to _smalldocs.yml:

api:
  packages: [mypkg]                  # the importable packages to document
  out: api                           # the folder of pages, inside the site; `api` by default
  root: ..                           # the root of the repository, from the folder of the project
  source: https://github.com/me/mypkg/blob/main   # each entry links to its line here
  check: ["*.py", "tests/**/*.py"]   # more files that `smalldocs api check` holds to the convention
  check-shown: true                  # `smalldocs api check` holds the packages only to what the pages show

In a _quarto.yml, write the same section under smalldocs:, as smalldocs.api. A key that a Python section does not read gives a warning. To make several references in one site, give api as a list of sections. See Several references in one site.

Key What it does Default
packages The importable packages to document. package with one name works too None. An api section without it stops the build
out The folder of the reference pages, from the folder of the project. It must be inside that folder api
root The root of the repository, from the folder of the project. The globs of check start here .., or . when the folder of the project holds pyproject.toml or .git
source The address of the code. Each entry links to its line there None: no links to the code
check Globs of more files that smalldocs api check holds to the convention None: only the packages
check-shown smalldocs api check holds the packages only to what the pages show, as --shown does. Write true or false with no quotes Off: every name
linkify Names in backticks link to their entries. The top-level linkify wins over it On

Each build then writes a page api/<module>.qmd for each public module. A module is public when no part of its name starts with _. The page has the name of the module without its package, such as site.qmd for smalldocs.site. When two modules have the same last name, each page takes the full name. A package whose __init__.py defines functions or classes gets a page too.

smalldocs notes the pages that it writes in the file api/.smalldocs-api. It deletes a page only if an earlier build wrote the page and the new build did not. So smalldocs keeps a page of your own in the folder, such as api/index.qmd.

To list the pages in the sidebar, use a glob:

website:
  sidebar:
    contents:
      - section: Reference
        contents: api/*

smalldocs imports your package. So install smalldocs in the environment of the package. Before the import, smalldocs puts the root of the repository and its src folder first on the import path, unless a folder is on it already.

The index of names

Each build also writes _symbols.json, in the folder of the project, before it builds the pages. It is a JSON object. Each key is a name, and its value is the entry of that name, as a page and an anchor:

{
 "Site": "/api/site.qmd#Site",
 "smalldocs.site.Site": "/api/site.qmd#Site",
 "Site.build": "/api/site.qmd#Site.build",
 "smalldocs.site.Site.build": "/api/site.qmd#Site.build"
}

smalldocs makes the index from the reference. For each class, method and function, it adds two keys:

Two modules can have a function with the same name, such as build. Then the short name links to the first one, in the order of packages, and then of the names of the modules. The full names link to each one.

smalldocs writes the file only when its content changes. Linkify, the step of the build that links names in backticks, then reads it.

Use the reference at the terminal

smalldocs api build --dir docs        # write the pages and the index
smalldocs api module site --dir docs  # print one module as Markdown, for people and agents
smalldocs api list --dir docs         # every module, class, method and function, one line each
smalldocs api check --dir docs        # what is typed and documented, and every gap; exit 1 if any
smalldocs api check --shown --dir docs  # the same, for only what the pages show

check reports each gap in the code:

  • a module without a docstring
  • a function or a class without a docstring
  • a parameter without a type or a docment
  • a return value without a type or a docment
  • a field of a pydantic model without a description or a comment
  • a key of a TypedDict without a comment

By default, check looks at every function, class and method of a package, the private ones too. A private name starts with _. With --shown, or with check-shown: true in the api section, it looks only at what the pages show:

  • the public functions and classes of each module. When a module has __all__, only the public names in it
  • the public methods of each of these classes

--shown does not change the files of the check globs, because they have no pages. check still looks at every name in them.

The globs of api.check start at the root of the repository. In them, * stays inside one folder, and **/ is any number of folders, or none. So *.py finds the files at the root only, and tests/**/*.py finds tests/conftest.py and tests/unit/test_x.py. check leaves out the files of the packages in packages, because it already checks them as part of their package.

check names each file by its path from the root, such as tests/test_x.py. So the output is the same on every computer:

tests/test_x.py:test_add: no docstring

Run check in your tests, or before each commit.