Skip to content

Preserve the enclosing class prefix for sibling objects nested in a… - #14695

Open
lpyu001 wants to merge 1 commit into
sphinx-doc:masterfrom
lpyu001:fix/14694-python-domain-class-prefix
Open

lpyu001 wants to merge 1 commit into
sphinx-doc:masterfrom
lpyu001:fix/14694-python-domain-class-prefix

Conversation

@lpyu001

@lpyu001 lpyu001 commented Sep 18, 2026 •

Copy link
Copy Markdown

Purpose

PyObject.before_content sets py:class from an explicit dotted prefix but only pushes onto the py:classes stack when the directive is nestable. PyObject.after_content restores py:class from that stack unconditionally, outside the allow_nesting guard. For .. py:attribute:: Widget.flags the stack is empty, so the first object nested in its body clears py:class and
the rest of the body loses its class context.

With an empty extensions list, three .. py:data:: directives in that body are registered as mymod.Widget.FLAG_A, mymod.FLAG_B and mymod.FLAG_C, and the HTML ids differ the same way. Cross-references also stop resolving
after the first nested object, with no warning unless -n is used. Autodoc hits the same path, since .. automethod:: mymod.Widget.frob emits a Widget.-prefixed signature.

The fix saves py:class in before_content and restores it in after_content, instead of restoring the stack top. It applies to nestable directives too: in the body of .. py:method:: Other.meth() inside .. py:class:: Outer, an object after a nested .. py:class:: is currently registered under Outer rather than Other — the same defect with a wrong prefix instead of a missing one. The py:classes stack is otherwise unchanged.

This restores the pre-1.6 semantics: before b0875d6, after_cont py:classonlyif self.clsname_set`, so a nested directive never parent's context. That rewrite covered the nestable case only.

Documents relying on the current behaviour will see object names a change for siblings after the first nested object. Adds a regressi a CHANGES.rst entry.

References

AI Disclosure

Claude Code (Claude Opus 5) was used to prepare this pull request. reproduced the behaviour on 8.2.3 and master, ran the test suites patched and unpatched checkout, found the pre-1.6 implementation, identified the nested-class case behind the nestable branch change this description, the CHANGES.rst entry and the regression test.to sphinx/domains/python/_object.py is mine.

🤖 Generated with Claude Code

This branch has not been deployed

No deployments
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.

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

1 participant