Skip to content

Fix: instance variables link to other variables of the same name#14389

Closed
piyushhhxyz wants to merge 1 commit intosphinx-doc:masterfrom
piyushhhxyz:fix-sphinx-doc__sphinx-8638
Closed

Fix: instance variables link to other variables of the same name#14389
piyushhhxyz wants to merge 1 commit intosphinx-doc:masterfrom
piyushhhxyz:fix-sphinx-doc__sphinx-8638

Conversation

@piyushhhxyz
Copy link
Copy Markdown

Summary

Removes rolename='obj' from the 'variable' PyTypedField definition in sphinx/domains/python.py so that variable names documented with :var:, :ivar:, or :cvar: are no longer automatically turned into cross-reference links.

Problem

When using autodoc (especially via apidoc), instance variables documented with :ivar: or :var: had their names automatically wrapped in pending_xref nodes with role obj. This caused Sphinx's cross-reference resolution (including fuzzy matching in find_obj) to link them to completely unrelated variables with the same name elsewhere in the project.

For example, somepackage.subA.Foo.limit would auto-link to somepackage.subB.Bar.limit even though they are completely unrelated.

Fix

Removed rolename='obj' from the variable field type definition:

# Before
PyTypedField('variable', label=_('Variables'), rolename='obj',
             names=('var', 'ivar', 'cvar'),
             typerolename='class', typenames=('vartype',),
             can_collapse=True),

# After  
PyTypedField('variable', label=_('Variables'),
             names=('var', 'ivar', 'cvar'),
             typerolename='class', typenames=('vartype',),
             can_collapse=True),

The typerolename='class' is preserved, so variable types are still cross-referenced as expected. Only the variable names stop being auto-linked.

Fixes #8638

Instance variables documented with :var:, :ivar:, or :cvar: had their names
automatically turned into cross-references (using rolename='obj'), which caused
them to link to unrelated variables with the same name elsewhere in the project.

The fix removes rolename='obj' from the 'variable' PyTypedField definition so
variable names are no longer wrapped in pending_xref nodes. The type annotations
(typerolename='class') are preserved, so variable types are still cross-referenced.

Fixes sphinx-doc#8638
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant