Closed
Labels
bugSomething isn't working
Description
Description of the bug
signature_crossrefs is creating links from local variables used to set instance attributes, where the docs say that it should only be doing it for type annotations.
This becomes a problem if the local variable name matches the name of a heading somewhere else in the docs.
To Reproduce
Setup:
pip install mkdocs-material mkdocs-api-autonav
touch mkdocs.yml
mkdir docs
touch docs/index.md
mkdir src
touch src/__init__.py# mkdocs.yml
site_name: Docs
theme:
name: material
plugins:
- autorefs
- mkdocstrings:
handlers:
python:
options:
show_if_no_docstring: true
separate_signature: true
signature_crossrefs: true
- api-autonav:
modules: ["src"]<!-- docs/index.md -->
# Intro# src/__init__.py
class Class:
def __init__(self):
intro = "Some text"
self.attr = introFinally:
- run
mkdocs serve - open http://127.0.0.1:8000/reference/src/
- notice that:
introhas become a link- that link takes you to http://127.0.0.1:8000/#intro, which is unrelated
Expected behavior
This link should not have been created.
Environment information
- System: macOS-26.1-arm64-arm-64bit
- Python: cpython 3.12.8 (~/.venv/bin/python)
- Environment variables:
- Installed packages:
mkdocstrings-pythonv1.19.0
Activity
[-]bug: `signature_crossrefs` is creating unwanted links from instance attribute definitions[/-][+]bug: `signature_crossrefs` is creating unwanted links when instance attributes are constructed using local variables[/+][-]bug: `signature_crossrefs` is creating unwanted links when instance attributes are constructed using local variables[/-][+]bug: `signature_crossrefs` is creating unwanted links when instance attributes set using local variables[/+]pawamoy commentedon Nov 21, 2025
Thanks for the report @hmellor, I was able to reproduce the issue.
pawamoy commentedon Nov 21, 2025
Not an easy one to fix! We'll probably have to keep track of locals declared in classes'
__init__methods, so that when trying to resolve a name, we see that it points to a local variable, meaning we shouldn't output a cross-reference.pawamoy commentedon Nov 21, 2025
For the explanation:
ExprName(intro)introin its scopeSo, as mentioned above, one solution is to keep track of locals in Python code to avoid outputing optional cross-refs that we know cannot be resolved (and worse, can wrongly be resolved to other irrelevant headings).
Another solution could be to return
self.nameas canonical path only when it's actually a built-in, with a hard-coded list. I avoided that in the past, because Python is dynamic and one could inject anything in__builtins__. But in the end, it's just about having the identifier in the known headings/ids, and something custom and injected wouldn't be documented as a builtin, but as a member of a third-party package. In short: even ifintrowas injected as a builtin (to be used globally), it wouldn't appear in Python's objects inventory anyway, so no point in trying to cross-ref it. So, I guess hard-coding builtins would work. That's still be a breaking change in our API, sincecanonical_pathalways return a string and now it would sometimes returnNone(probably?).pawamoy commentedon Mar 5, 2026
Moving this issue to Griffe.
feat: Expose `reference_path` in addition to `canonical_path`
pawamoy commentedon Oct 17, 2026
This still needs an update in mkdocstrings-python. Tracked here: mkdocstrings/python#347.