Skip to content

Possible data loss with sphinx.util.parsing.nested_parse_to_nodes() #13845

Closed

Description

@gmilde

Describe the bug

The utility function nested_parse_to_nodes() provides the boolean parameter keep_title_context".
If this parameter is True, parsing is done by docutils.parsers.rst.state.RSTState.nested_parse() with the document-wide "title style hierarchy".

Recent testing revealed that this method can loose complete sections without warning when the rST input contains sections with a title style that matches a lower section level than the current section level.

The new section algorithm introduced in 0.22 does no longer drop "lower" sections. However, after the nested parsing is complete, the calling parser continues where it left of, messing the order of elements in the doctree (cf. https://sourceforge.net/p/docutils/code/10205/).

The most sensible simple solution for Docutils is to always use a new "title style context" with nested parsing, which overrides the "keep_title_context" argument.
It seems Sphinx itself never uses nested_parse_to_nodes() with keep_title_context=True.

If there is a convincing use case for keep_title_context=True, we might try to find a save implementation. In this case, Sphinx should define and test the desired behaviour when a nested section title uses the style of a lower section level than the section containing the directive using nested_parse_to_nodes().
Otherwise I suggest to emit a warning like "nested parsing always uses a separate title style context in Docutils >= 0.23."

How to Reproduce

Try old_nested_parsing.py with:

Docutils 0.21 for the legacy section parsing algorithm,

Docutils commit r10204 for the messed order with the new section parsing algorithm (a unit test demonstrating the issue was added in commit r10205),

Docutils commit r10206 for the fix using a new title style context with every nested parsing.

Activity

  1. added
    docutils
    on Aug 19, 2025
  2. AA-Turner commented on Aug 19, 2025

    @AA-Turner
    Member

    Thanks @gmilde! I'm on holiday for the next week but will try and investigate.

    A

  3. jfbu commented on Aug 21, 2025

    @jfbu
    Contributor

    Our own testing with Docutils HEAD now fails since r10204. The r10205, r10206 and subsequent commits to Docutils did not modify that.

    The failing test (and all others) tests/test_directives/test_directive_only.py::test_sectioning passes at r10203 of Docutils.

    Note that the LaTeX/CI failure is a duplicate and a collateral to the fact that LaTeX/CI now uses Docutils HEAD.

    @gmilde It seems the revision numbers were shifted by one in your OP, so I edited them. Please revert if I erred.

  4. gmilde commented on Aug 21, 2025

    @gmilde
    ContributorAuthor

    FAILED tests/test_directives/test_directive_only.py::test_sectioning - AssertionError: Section out of place: '1.6.2. Subsection'
    assert '1.6.1.1.' == '1.6.2.'

    This is the "only" directive failing if it tries to re-attach/move sections with a higher level up in the document tree (by some "dark magic", so failure was kind of expected -- this is why I did not yet make a bugfix release with these changes).

    See #13861 for possible fixes.

  5. jfbu commented on Sep 6, 2025

    @jfbu
    Contributor

    Update: as @gmilde mentions at #13861 (comment) our ``only directive'' test passes again since [r10226] rST parser: restore backwards compatibility ....

    (to confirm I relaunched the failed jobs at current tip of master eaebbec)

  6. locked as resolved and limited conversation to collaborators on Dec 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docutilstype:bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions