click_extra.sphinx.todos module

List each docstring :todo: once on a todolist, and report those it misses.

sphinx.ext.todo collects doctree nodes, not documented objects. A :todo: written once in a docstring therefore lands on the todolist page once per rendering of that docstring, and two conventions common to autodoc projects render the same docstring several times:

  • A full-API page plus per-feature pages. A project documenting every module on one page, then documenting the same modules again next to the prose that explains them, renders each docstring twice. :no-index: on the second block does not help: it suppresses the cross-reference target and the search-index entry, leaving the docstring (and its todo node) rendered in full.

  • A package re-exporting its members. automodule documents the imported names a package lists in __all__, so a symbol appears once under the package and once under the module that defines it. Both renderings can even land on the same page.

The two multiply, and nothing upstream deduplicates: sphinx.ext.todo.TodoListProcessor flattens the whole todo domain into the page in read order. This module removes the surplus nodes from that domain just before the processor reads it, so the rendered list carries one entry per directive.

The same cause has the opposite effect on a docstring that no page renders: the docstring of a private module, of a private member that :members: skips, or of a nested function. It never becomes a node, so its :todo: reaches no todolist. This module reads the source of the documented packages instead, and logs a UNRENDERED_TODO_WARNING for each such :todo:. Its author can then move it into a docstring that a page renders.

A parallel read also changes the order of the list. The todo domain keys its entries on their page, and the todolist renders them in the order of that mapping, which a parallel read fills in the order its workers merge. This module sorts the mapping by page once the read is over, which is the order a serial read gives (sphinx-doc/sphinx#14809).

Todo

Delete sort_todos() once sphinx-doc/sphinx#14809 ships, and the sphinx extra’s floor passes the release that carries it.

Todo

Propose the deduplication upstream, as a sphinx.ext.todo feature rather than a third-party hook.

The repetition is a property of how autodoc renders a docstring, not of how a project writes one, so every autodoc project documenting a module twice hits it and none of them can fix it in their own source: :no-index: reads like the cure and is not. sphinx.ext.todo.TodoListProcessor already flattens the whole domain in one place, which is where a todo_deduplicate config value would apply; the helpers this module needed (todo_identity(), is_reexport() and the comparison of the objects behind two labels) are the whole of the logic.

Should it land, keep this module as a shim for the Sphinx releases below that floor, then drop it once the floor moves past them.

click_extra.sphinx.todos.AUTODOC_DOCSTRING_MARKER = 'docstring of '

Separator autodoc puts in front of the object a docstring belongs to.

A node produced from a docstring carries a synthetic source naming the object autodoc documented: {file}:docstring of {dotted.path} when a source file was analyzed, and plain docstring of {dotted.path} when none was. Both forms live inside the supported sphinx>=8 range, so the split has to accept the two of them. It is what lets todo_identity() recognize two renderings of a single docstring reached through two import paths.

click_extra.sphinx.todos.DEDUPE_TODOS_CONFIG = 'click_extra_dedupe_todos'

Name of the conf.py value gating the deduplication.

True by default: a todolist page listing the same item three times is a defect in every project I know of, and a project that has not enabled sphinx.ext.todo never notices the hook either way. Set it to False to get Sphinx’s raw output back, one entry per rendering.

click_extra.sphinx.todos.UNRENDERED_TODO_WARNING = 'click_extra.todo_unrendered'

Type and subtype of the warning about a docstring :todo: no page renders.

List it in suppress_warnings to silence the warning, or in click_extra.sphinx.fail_on_warnings.FAIL_ON_WARNINGS_CONFIG to fail the build on it.

click_extra.sphinx.todos.todo_identity(node)[source]

Identify the {todo} directive a node was rendered from.

Two nodes share an identity when they come from the same line of the same docstring or document, whatever page rendered them and whichever import path autodoc reached the object through. The file prefix is dropped from a docstring source precisely so a re-exported symbol matches the module that defines it.

Return type:

tuple[str, int | None]

click_extra.sphinx.todos.is_reexport(node)[source]

Tell whether a node was rendered through a package’s __init__.

Used to rank competing renderings: the entry surviving deduplication keeps its backlink and its “located in …” attribution, so a rendering reached through the defining module is worth more to a reader than the same docstring reached through a re-exporting package.

Return type:

bool

click_extra.sphinx.todos.deduplicate_todos(app, doctree, docname)[source]

Drop every todo node duplicating one already held by the domain.

Connected to doctree-resolved below the priority sphinx.ext.todo.TodoListProcessor runs at, and a no-op on any document holding no todolist, so the work happens once on the page that consumes the domain.

Two renderings come from one docstring when their labels import the same object. autodoc labels a function or a class with the path of its defining module, whatever path reached it, but labels an attribute or a property with the path it documented: a re-exported attribute carries a different label under its package and under its module, so only the object matches them.

Among the renderings of one directive, the surviving node is the first in (rendered through a defining module, document name, position in the document) order. That ordering is total and reads the same on a clean build and an incremental one, so the backlink a reader follows does not move around between builds.

Note

The todo domain is mutated in place. That is safe because Sphinx pickles the environment at the end of the reading phase, before the writing phase this hook belongs to: the removals reach the page being written and never the cached environment, so an incremental rebuild still starts from the full set.

Return type:

None

click_extra.sphinx.todos.docstring_todos(source, module)[source]

Yield the owner and the line of each todo directive in docstrings.

source is the text of the module named module. The owner of a docstring is the dotted path autodoc labels it with: module for the module docstring, and {module}.{qualname} for a class or a function, with <locals> for a definition nested in a function. An attribute docstring, the string literal right after an assignment, belongs to each name the assignment binds, as autodoc reads it. Lines count from 1.

A directive counts in both syntaxes a docstring can use: a MyST fence, with backticks or colons, and a reST directive. A directive nested in another one counts too. A directive quoted in a code block does not.

Raises:

SyntaxError – If source does not parse.

Return type:

Iterator[tuple[str, int]]

click_extra.sphinx.todos.report_unrendered_todos(app, env)[source]

Warn about each docstring {todo} that no page renders.

The warning has the UNRENDERED_TODO_WARNING type, and points at the line of the directive. The check is connected to env-check-consistency, which fires once the build has read its documents and merged the domains of a parallel read: the todo domain then holds a node for each :todo: a page renders. Like Sphinx’s own consistency checks, it does not fire on a build that reads no document.

The packages read are the ones holding a module of the Python domain, which is where automodule registers the modules it documents. Every module of such a package is read, private ones included, without being imported.

A docstring rendered under another path still counts. autodoc labels a function or a class with the path of its defining module, whatever path reached it, but labels an attribute or a property with the path it documented. So when the labels differ, the check imports the object behind both paths and compares them.

Inert unless the project enables sphinx.ext.todo with todo_include_todos: without it, no {todo} renders anywhere, so none can be missing.

Return type:

None

click_extra.sphinx.todos.sort_todos(app, env)[source]

Order the entries of the todo domain by page, as a serial read does.

Connected to env-updated, which fires once the read is over: after a parallel read merged the entries of its workers, and before Sphinx pickles the environment. Inert without sphinx.ext.todo, which registers the domain.

Return type:

None

click_extra.sphinx.todos.setup(app)[source]

Register the todo-list hooks on app.

Called from click_extra.sphinx.setup() so projects only need to list "click_extra.sphinx" in their extensions. Priority 400 places the deduplication hook below the default 500 sphinx.ext.todo.TodoListProcessor is connected at, which is what makes the domain already trimmed by the time the list is rendered.

Return type:

None