Title: change: scoped cross-references should give precedence to exact matches · Issue #343 · mkdocstrings/python · GitHub
Open Graph Title: change: scoped cross-references should give precedence to exact matches · Issue #343 · mkdocstrings/python
X Title: change: scoped cross-references should give precedence to exact matches · Issue #343 · mkdocstrings/python
Description: Opening to keep as a memo for when working on a better solution in Zensical. Scoped cross-reference resolution Problem With scoped_crossrefs=True, mkdocstrings-python currently expands every reference before mkdocs-autorefs resolves it. ...
Open Graph Description: Opening to keep as a memo for when working on a better solution in Zensical. Scoped cross-reference resolution Problem With scoped_crossrefs=True, mkdocstrings-python currently expands every refere...
X Description: Opening to keep as a memo for when working on a better solution in Zensical. Scoped cross-reference resolution Problem With scoped_crossrefs=True, mkdocstrings-python currently expands every refere...
Opengraph URL: https://github.com/mkdocstrings/python/issues/343
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"change: scoped cross-references should give precedence to exact matches","articleBody":"Opening to keep as a memo for when working on a better solution in Zensical.\n\n# Scoped cross-reference resolution\n\n\n## Problem\n\n\nWith `scoped_crossrefs=True`, mkdocstrings-python currently expands every reference before mkdocs-autorefs resolves it.\n\n\nFor example, consider `markdown.something` in `markdown.core`, where `markdown` is also a local function. The handler changes this identifier to `markdown.core.markdown.something`.\n\n\nThis change removes the exact identifier before autorefs checks its local anchor maps or external inventories. A documented or imported `markdown.something` therefore cannot win over the scoped interpretation.\n\n\n## Resolution timing\n\n\n`AutorefsHook.expand_identifier()` runs during docstring Markdown conversion. It runs when `AutorefsInlineProcessor` creates the temporary `\u003cautoref>` element.\n\n\nAt that time, autorefs does not yet have its complete identifier-to-URL maps:\n\n\n- MkDocs can render and register targets on later pages.\n- Mkdocstrings registers external inventory URLs during `on_env`.\n- A Griffe object can exist without a rendered heading or registered URL.\n\n\nChecking autorefs during `expand_identifier()` would therefore make resolution depend on page order. Checking Griffe would only prove that a Python object exists, not that autorefs can link to it.\n\n\nAutorefs resolves `\u003cautoref>` elements during `on_env`, after MkDocs has rendered all pages and mkdocstrings has registered external inventories. Both candidates must survive until this step.\n\n\n## Applicable reference forms\n\n\nRelative and scoped cross-references are only manually written. They use an explicit target such as `[text][identifier]` or a code-style target such as ``[`identifier`][]``.\n\n\nAutorefs treats both forms as exact references and does not create a normal Markdown slug for them. Automatic cross-references are already absolute and do not need scoped resolution.\n\n\nThe required order is therefore:\n\n\n1. For an ordinary manual reference, try the identifier exactly as written.\n2. If `scoped_crossrefs=True`, try the identifier expanded in the current Python scope.\n3. For an explicit relative reference, use only its context-directed expansion.\n\n\nWhen `scoped_crossrefs=False`, do not emit or try a scoped fallback.\n\n\n## Implementation in mkdocstrings-python\n\n\nUse the existing `identifier` and `slug` attributes of `\u003cautoref>`. Autorefs already tries `identifier` first and `slug` second.\n\n\nFor an ordinary reference, `AutorefsHook.expand_identifier()` must preserve the exact identifier. When scoped cross-references are enabled, it must also compute and temporarily store the scoped identifier.\n\n\n`AutorefsHook.get_context()` must return a context whose `as_dict()` adds the scoped identifier as `slug`. Here, `slug` transports a fallback identifier; it is not a Markdown slug.\n\n\nFor example, the hook must produce the equivalent of:\n\n\n```html\n\u003cautoref identifier=\"markdown.something\" slug=\"markdown.core.markdown.something\">...\u003c/autoref>\n```\n\n\nAutorefs will then try `markdown.something` before `markdown.core.markdown.something`. This happens after all local and external URLs are available.\n\n\nThe hook must clear its stored fallback before it processes each reference. It must not emit `slug` when the scoped identifier equals the exact identifier.\n\n\nIf an enabled relative reference starts with a dot, expand it with the existing relative-reference logic. Return the expanded identifier directly and do not emit a scoped fallback.\n\n\nThis design requires no changes or new capabilities in mkdocs-autorefs. It also requires no coordinated release or new minimum autorefs version.\n\n\n## Tests\n\n\nAdd integration coverage for these cases:\n\n\n1. With `scoped_crossrefs=True`, both the exact and scoped targets exist. The exact target wins.\n2. With `scoped_crossrefs=True`, only the scoped target exists. The scoped fallback resolves.\n3. With `scoped_crossrefs=False`, the handler does not emit or try a scoped fallback.\n4. An exact target from an external inventory wins over a local scoped interpretation.\n5. Explicit relative cross-references keep their context-directed behavior and do not emit a scoped fallback.\n6. Automatic cross-references remain absolute and unchanged.\n7. The hook clears its stored fallback between consecutive references.\n\n\n## Backlink limitation\n\n\nAutorefs records backlinks against `identifier` before it tries `slug`. If the scoped fallback wins, autorefs does not record the backlink against the scoped target.\n\n\nThis behavior already applies to normal slug fallbacks. Link resolution needs no autorefs change, but backlink parity would require separate work in autorefs.\n","author":{"url":"https://github.com/pawamoy","@type":"Person","name":"pawamoy"},"datePublished":"2026-09-22T13:41:56Z","url":"https://github.com/mkdocstrings/python/issues/343"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:726dbd05-15bc-ddf3-64be-b9340141ca1c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CE80:FBBFE:1129A86:16B5A03:6B0652E2 |
| html-safe-nonce | 82bf5a4184ff7e03a3f81ac090633cd0204172c6b1dc759a0daa6e85c707e9a9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRTgwOkZCQkZFOjExMjlBODY6MTZCNUEwMzo2QjA2NTJFMiIsInZpc2l0b3JfaWQiOiIzNzEyNTUyNzYzMDQyMTkwMDUwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6bbed77910d0ae4f5e64014ebb732d911a5e9df2d7a866b654280593a1d38637 |
| hovercard-subject-tag | issue:5540429546 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/mkdocstrings/python/343/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ae177097742d2bb9a4de33bc5a26a5970b165954399b3fafc59bf35761361427/mkdocstrings/python/issues/343 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ae177097742d2bb9a4de33bc5a26a5970b165954399b3fafc59bf35761361427/mkdocstrings/python/issues/343 |
| og:image:alt | Opening to keep as a memo for when working on a better solution in Zensical. Scoped cross-reference resolution Problem With scoped_crossrefs=True, mkdocstrings-python currently expands every refere... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pawamoy |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| turbo-cache-control | no-preview |
| go-import | github.com/mkdocstrings/python git https://github.com/mkdocstrings/python.git |
| octolytics-dimension-user_id | 75664361 |
| octolytics-dimension-user_login | mkdocstrings |
| octolytics-dimension-repository_id | 422878404 |
| octolytics-dimension-repository_nwo | mkdocstrings/python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 422878404 |
| octolytics-dimension-repository_network_root_nwo | mkdocstrings/python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width