Test 1
fail to passinfo field list var
Sphinx Python Domain: :var: Fields Incorrectly Cross-Reference Variable Names
sphinx-doc__sphinx-8638 · cardboard
You are working in the sphinx-doc/sphinx repository at commit 4b452338f914d4f6b54704222d70ae8a746e3db5.
In Sphinx's Python domain, docstring field lists accept :var:, :ivar:, and :cvar: field types to document instance, class, and plain variables on a class or module. These fields accept an optional type annotation and a variable name, for example:
.. py:class:: MyClass
:var int attr: Some integer attribute.The bug: the variable name (attr in the example above) is currently wrapped in a pending_xref node — the same mechanism Sphinx uses to resolve cross-references to other objects in the documentation. This causes Sphinx to attempt a cross-reference lookup for the bare variable name attr, which can match unrelated attributes in other classes or modules. The variable name should be rendered as addnodes.literal_strong (plain bold monospace text), not as a hyperlink to wherever Sphinx can find something named attr.
The type annotation (int in the example) must continue to produce a pending_xref with reftype='class' so that type names resolve normally.
1. File to modify: sphinx/domains/python.py
2. Target object: The PyTypedField instance named 'variable' (field names 'var', 'ivar', 'cvar') defined inside PyObject.doc_field_types. At the base commit this entry looks roughly like:
```python
PyTypedField('variable', label=_('Variables'),
names=('var', 'ivar', 'cvar'),
typerolename='class', rolename='obj',
can_collapse=True),
```
3. Required change: The rolename='obj' argument causes the field argument (variable name) to become a pending_xref. Remove or replace that argument so the variable name is emitted as addnodes.literal_strong instead. The typerolename='class' must be preserved so type annotations still cross-reference as class objects.
4. Test that must pass (added by the hidden test patch): tests/test_domain_py.py::test_info_field_list_var
The hidden test parses the RST:
```rst
.. py:class:: Class
:var int attr: blah blah
```
and asserts that the field list entry for attr is structured as:
```
(
[addnodes.literal_strong, 'attr'],
' (',
[pending_xref, addnodes.literal_emphasis, 'int'],
')',
' -- ',
'blah blah'
)
```
Crucially, attr must not be inside a pending_xref node.
5. All 34 pre-existing tests in tests/test_domain_py.py listed in the pass-to-pass set must continue to pass.
Parsing :var int attr: blah blah inside a .. py:class:: directive produces a node tree that wraps the variable name in a cross-reference:
pending_xref(attr) literal_emphasis(int) text('blah blah')Because pending_xref(attr) is resolved against the entire documentation tree, it can silently link attr to an unrelated class attribute that happens to share the name.
literal_strong(attr) pending_xref → literal_emphasis(int) text('blah blah')No cross-reference is produced for the variable name itself.
sphinx/domains/python.py (and tests/test_domain_py.py if you want to add the test locally, but the hidden grader will apply its own test patch regardless).typerolename='class' argument — type annotations must still resolve.'var', 'ivar', 'cvar' or the can_collapse=True setting.rolename parameter in TypedField / PyTypedField controls whether the field argument gets a cross-reference. Setting it to an empty string or omitting it entirely is the idiomatic Sphinx fix.docfields.py or PyTypedField internals are out of scope and may break other tests.Your submission is accepted if:
1. The hidden test tests/test_domain_py.py::test_info_field_list_var passes.
2. All 34 pass-to-pass tests in tests/test_domain_py.py continue to pass.
Leaderboards additionally rank accepted runs by estimated token usage, cost, and wall-clock time.
Container
not started
Visible tests
35
Hidden tests
0
Last run
Not run
Test 1
fail to passinfo field list var
Test 2
pass to passfunction signatures
Test 3
pass to passdomain py xrefs
Test 4
pass to passdomain py objects
Test 5
pass to passresolve xref for properties
Test 6
pass to passdomain py find obj
Test 7
pass to passget full qualified name
Test 8
pass to passparse annotation
Test 9
pass to passpyfunction signature
Test 10
pass to passpyfunction signature full
Test 11
pass to passpyfunction signature full py38
Test 12
pass to passpyfunction with number literals
Test 13
pass to passoptional pyfunction signature
Test 14
pass to passpyexception signature
Test 15
pass to passexceptions module is ignored
Test 16
pass to passpydata signature
Test 17
pass to passpydata signature old
Test 18
pass to passpyobject prefix
Test 19
pass to passpydata
Test 20
pass to passpyfunction
Test 21
pass to passpyclass options
Test 22
pass to passpymethod options
Test 23
pass to passpyclassmethod
Test 24
pass to passpystaticmethod
Test 25
pass to passpyattribute
Test 26
pass to passpydecorator signature
Test 27
pass to passpydecoratormethod signature
Test 28
pass to passcanonical
Test 29
pass to passinfo field list
Test 30
pass to passmodule index
Test 31
pass to passmodule index submodule
Test 32
pass to passmodule index not collapsed
Test 33
pass to passmodindex common prefix
Test 34
pass to passnoindexentry
Test 35
pass to passwarn missing reference
README.md
sphinx-doc/sphinx