Skip to content

Stop using mkdocstrings relative cross-references #655

Open

Description

@llucax

What's needed?

I think we should probably stop using mkdocstrings relative cross-references and go back to absolute cross-references to public paths. In combination with inherited_members: true, which all our projects use, they turned out to be too fragile.

Since mkdocstrings-python 2.0.9 (mkdocstrings/python#342), the dots are counted from the path where an object is defined, not where it's documented. The maintainer considers the old behavior a bug, and Zensical works the same way (explanation). With our layout of private modules re-exported from a public package, this has several consequences:

  • Existing refs break: updating to 2.0.9 makes the strict docs build fail in core (3 warnings), channels (28), dispatch (6) and client-common (330), see regression(2.0.9): relative cross-references in re-exported and inherited objects expand against private modules mkdocstrings/python#346.
  • The fix is fragile: counting the dots from the private module works. But the fixed refs depend on the private module layout, and they fail on 2.0.8, so they have to land together with the bump.
  • Some refs silently change target: when a relative ref expands to the private path of an inherited member, every subclass's copy matches it. mkdocs-autorefs then picks the closest URL instead of warning, which on a single page means the first class registered. For example, Receiver.ready() in channels now links to BroadcastReceiver.receive instead of Receiver.receive, and the ElectricalComponent docstrings in client-common link to AcEvCharger. Comparing every link in the built sites against 2.0.8, I found 11 such refs (123 links) in channels and client-common. Strict mode doesn't catch any of them.
  • They are hard to read outside the docs: IDEs show the raw docstrings, and something like [receive()][....Receiver.receive] says nothing about where receive() is publicly available. A private path like [x][.._priv.x] is even worse, as it can steer users into importing from private modules.
  • They depend on the consumer: they only work across packages if every project rendering the inherited docstrings enables the option (bug: relative cross-references in inherited members still depend on the consumer's relative_crossrefs setting mkdocstrings/python#344). Even then, they can silently resolve to an unrelated object with the same name in the consumer's tree (details).

Absolute refs to public paths have none of these problems (its only disadvantage is verboseness). They don't depend on the mkdocstrings-python version, the consumer's configuration or where the object is defined. A public path also has a single anchor, so autorefs either finds it or warns.

Proposed solution

  1. Revert Enable mkdocstrings relative cross-references #646 before releasing v0.20.0, so the template and the migration script stop enabling relative_crossrefs. It's not released yet, so no project got it from the migration.
  2. Convert the relative refs in this repository's own docstrings to absolute ones, and remove the option from its mkdocs.yml.
  3. Document the convention (absolute cross-references to public paths) wherever we document docstring conventions.

The migration script shouldn't disable the option in existing projects. At most we could trigger a manual step saying relative cross-refs should not be used to increase this issue visibility (linking to this issue probably).

A project can only turn it off once neither its own docstrings nor the ones it inherits from its dependencies use relative refs (mkdocstrings/python#344 again, in reverse). So the conversion has to go dependencies first (core, channels, quantities, ...), with a release, and then the projects using them.

We can do this as a first step before releasing v0.20.0.

Use cases

Every project with private modules and inherited_members: true, which is basically all of them. The most urgent ones are the pending mkdocstrings-python 2.0.9 bumps: frequenz-floss/frequenz-core-python#204, frequenz-floss/frequenz-channels-python#560, frequenz-floss/frequenz-dispatch-python#306 and frequenz-floss/frequenz-client-common-python#291. The broken refs in them have to be rewritten anyway, so this is a good time to decide which form they take.

Alternatives and workarounds

  • Relative refs counted from the defining module, targeting public paths (pawamoy's option 2): I tested it in the four repos above. After also fixing the silently retargeted refs, it builds cleanly and gives exactly the 2.0.8 links in core, channels and dispatch, but it keeps all the problems listed above.
  • Relative refs to canonical private paths (pawamoy's option 1, like [..._ignoring.ignoring_warnings]): these resolve through secondary anchors, so they are exposed to the closest-URL pick whenever the object is rendered in more than one place, inherited members included. They are also the worst for readability in IDEs.
  • Pinning mkdocstrings-python to 2.0.8: this only postpones the problem, since the 2.0.9 behavior is intended.

Additional context

This reverses the decision in #645. Relative refs were originally requested in #235 because absolute paths are long, and that's still their cost.

Activity

  1. added
    priority:❓
    type:enhancement
    part:cookiecutter
    part:mkdocs
    priority:high
    and removed
    priority:❓
    on Oct 13, 2026
  2. added this to the v0.20.0 milestone on Oct 13, 2026
  3. llucax commented on Oct 13, 2026

    @llucax
    ContributorAuthor

    I will wait on this to see if p gives an answer in mkdocstrings/python#346 (comment) about future plans in Zensical, but even if there are plans to get this sorted out in the future in Zensical, I think we are still to far away from a whole migration, so since this was not rolled out everywhere yet, it might be wiser to just revert before the release than include it and deal with the fallout, waiting for a potential migration and solution in the future...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    part:cookiecutterpart:mkdocspriority:hightype:enhancement

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions