Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toc_object_entries uses language (Python) scoping instead of documentation scoping #12352

Open
jakobandersen opened this issue May 5, 2024 · 2 comments · May be fixed by #12367
Open

toc_object_entries uses language (Python) scoping instead of documentation scoping #12352

jakobandersen opened this issue May 5, 2024 · 2 comments · May be fixed by #12367

Comments

@jakobandersen
Copy link
Contributor

Describe the bug

The entries added by toc_object_entries for Python (and other domains?) seems to use the scoping of the language to nest the toc entries, instead of how they were declared in the document. E.g., if a nested class is declared after the class, then its entry is nested inside the class instead of appended.

How to Reproduce

E.g., with

.. py:currentmodule:: myMod
.. py:class:: MyClass

	.. py:method:: f()

.. py:class:: MyClass.MyNestedClass

I get the toc

- MyClass
  - MyClass.f()
  - MyClass.MyNestedClass

instead of

- MyClass
  - MyClass.f()
- MyClass.MyNestedClass

Environment Information

Tested with current master.

Sphinx extensions

No response

Additional context

Could the check at

if parents and parents in memo_parents:
be changed to check if the parent in the language is also the parent in the current toctree generation?

@electric-coder
Copy link

electric-coder commented May 8, 2024

Being a directive shouldn't currentmodule also have an empty line and one level of nesting? Like this:

.. py:currentmodule:: myMod

    .. py:class:: MyClass

        .. py:method:: f()

    .. py:class:: MyClass.MyNestedClass

To get the nested class I'd rely only on reST scoping rules so it seems the most robust and language agnostic solution would be to put f() and MyNestedClass on the same level:

.. py:currentmodule:: myMod

    .. py:class:: MyClass

        .. py:method:: f()

        .. py:class:: MyNestedClass

However I think you're right that:

seems to use the scoping of the language to nest the toc entries, instead of how they were declared in the document.

If you want to document the nested class flattened on the same level as the containing class then the TOC should have given the desired result:

- MyClass
  - MyClass.f()
- MyClass.MyNestedClass 

@jakobandersen
Copy link
Contributor Author

Being a directive shouldn't currentmodule also have an empty line and one level of nesting? Like this:

The currentmodule has no content, but simply sets the module. However, if you use module with content, then it's equivalent to the original example, and produces the wrong TOC as well.

To get the nested class I'd rely only on reST scoping rules so it seems the most robust and language agnostic solution would be to put f() and MyNestedClass on the same level:

Generally I agree that showing them nested is a good default. However, in the original use-case there are multiple nested classes and all involved classes are relatively large, making it desirable to separate them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants