#14694·sphinx

Python domain: only the first nested object inherits the class prefix inside a non-nestable directive body

Author: lpyu001Created Sep 18, 2026Updated Sep 18, 2026
Labelstype:bug

Describe the bug

When several .. data:: directives are nested in the body of a non-nestable Python directive such as .. attribute:: or .. method::, only the first one inherits the enclosing py:class prefix. Every following sibling is registered without it, so objects written side by side in one directive body end up with different fully qualified names and different HTML IDs.

How to Reproduce

Minimal method:

bash
$ mkdir sphinx-py-nesting && cd sphinx-py-nesting
$ cat > index.rst <<'EOF'
.. py:module:: mymod

.. py:attribute:: Widget.flags

   .. py:data:: FLAG_A

   .. py:data:: FLAG_B

   .. py:data:: FLAG_C
EOF
$ echo "" > conf.py
$ sphinx-build -M html . _build
$ python -m sphinx.ext.intersphinx _build/html/objects.inv

The three py:data directives are siblings in one directive body, but only the first one is registered under Widget:

py:data
    mymod.FLAG_B                     : index.html#mymod.FLAG_B
    mymod.FLAG_C                     : index.html#mymod.FLAG_C
    mymod.Widget.FLAG_A              : index.html#mymod.Widget.FLAG_A

The HTML ids differ in the same way:

bash
$ grep -o 'id="mymod[^"]*"' _build/html/index.html
id="mymod.Widget.flags"
id="mymod.Widget.FLAG_A"
id="mymod.FLAG_B"
id="mymod.FLAG_C"

The build emits no warning.

Replacing .. py:attribute:: Widget.flags with .. py:class:: Widget in the same index.rst names all three siblings consistently, which is the expected result:

py:data
    mymod.Widget.FLAG_A              : index.html#mymod.Widget.FLAG_A
    mymod.Widget.FLAG_B              : index.html#mymod.Widget.FLAG_B
    mymod.Widget.FLAG_C              : index.html#mymod.Widget.FLAG_C

Environment Information

Platform:              linux; (Linux-6.8.0-124-generic-x86_64-with-glibc2.39)
 Python version:        3.12.3 (main, Jan 22 2026, 20:57:42) [GCC 13.3.0])
 Python implementation: CPython
 Sphinx version:        8.2.3
 Docutils version:      0.21.2
 Jinja2 version:        3.1.6
 Pygments version:      2.21.0

Sphinx extensions

python
[]

Additional context

No response