Render field lists in template-format docstrings - #33
Open
hakonhagland wants to merge 3 commits into
Open
hakonhagland wants to merge 3 commits into
hakonhagland wants to merge 3 commits into
Conversation
Docstrings coming from the template format were passed to docutils as a
single line, so reStructuredText field lists in them were never parsed.
The published API page showed the markup itself:
:param init: Whether to call MPI_Init() or not.
instead of a parameter table.
The cause is the separator in
for line in doc.split('\\n'): # Handle escaped newlines
In Python source '\\n' is a two-character string, a backslash followed
by an n. json.load has already turned the \n escapes in the JSON file
into real newline characters, so nothing in the docstring matches and
split returns one piece: the whole docstring, newlines and all, appended
to the ViewList as a single line. docutils parses line by line, so the
":param" never starts a line and is treated as ordinary text.
Split on a real newline instead, as the flat-format path a few lines
below already does. That path was unaffected, which is why the
opm-common page has always rendered its parameter tables correctly while
the opm-simulators page did not.
Both the constructor loop and the method loop had the same separator.
Builds a minimal Sphinx project from a small template-format docstrings file and asserts that a ":param" in the JSON becomes a parameter table rather than visible text, for both a method and a constructor since they go through separate loops. The existing tests/files/docstrings_simulators.json is in the older flat format, which uses the code path that was already correct, so it cannot exercise this. A small template-format file is added alongside it. Both tests fail on the previous commit's parent and pass on it.
The function printed the JSON path it was about to open, on every call, to the build's standard output. With two directives across three branch builds that is six bare absolute paths in the log, interleaved with sphinx-versioned's own output and with no indication of what they are. Removed rather than converted to a logger call, since the path is already determined by conf.py and visible there. If it is wanted as a diagnostic, sphinx.util.logging's logger.verbose() would put it behind sphinx-build -v instead of printing it unconditionally.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every
:param:and:type:on the generated simulators API page is printed as literal text instead of becoming a parameter table. On the current master build there are 45 occurrences of the raw string:paraminsimulators.html. A method renders like this:The
opm-commonpage is unaffected, which turns out to be the clue to the cause.Split docstrings on a real newline (commit 1)
process_template_docstringssplit each docstring withdoc.split('\\n'). In Python source'\\n'is a two-character string — a backslash followed by ann— not a newline.json.loadhas already converted the\nescapes in the JSON file into real newline characters by that point, so nothing in the docstring matches,splitreturns a single piece, and the whole docstring is appended to theViewListas one line.:param ...:in the middle of that single line never starts a line and is never recognized as a field list.'\n', which is what the flat-format path a few lines below already does. That is why only one of the two pages was affected:docstrings_common.jsonis in the flat format and goes down the correct path, whiledocstrings_simulators.jsonis in the template format and goes down this one.json.load— so it is removed rather than kept.Introduced in 069db36 ("Add template support to sphinx_ext_docstrings.py"), whose diff contains all three splits together — the class-level one correct, these two not.
Add a regression test (commit 2)
tests/test_sphinx_ext_docstrings.pybuilds a minimal Sphinx project in a temporary directory and asserts that a:param:in the JSON becomes a parameter table rather than visible text.tests/files/docstrings_simulators.jsonis in the older flat format, so it exercises the code path that was already correct and cannot catch this.docstrings_simulators_template.jsonis added alongside it — a minimal template-format file with one simulator, one constructor and one method.test_view_docs.pycontinues to pass.Remove a debug print (commit 3)
read_doc_stringsprinted the JSON path it was about to open on every call, straight to standard output. Across two directives and three branch builds that is six bare absolute paths in the build log, interleaved withsphinx-versioned's own output and unlabeled.conf.py. If it is wanted as a diagnostic,sphinx.util.logging'slogger.verbose()would put it behindsphinx-build -vinstead of printing unconditionally — happy to do that instead if reviewers prefer.Effect on the built documentation
:paramoccurrences insimulators.html: 45 → 0, with 57 real field-list blocks in their place.build succeeded, 45 warningseither way, and the warning list is identical apart from one line number that moves because the generated reStructuredText is now split into lines rather than being one long line.Inline literal start-string without end-string, three times. It is not introduced here — it is a three-backtick inline literal in thesetupMpidocstring inopm-simulators, appearing three times because one template entry expands into the three simulator classes. It is being fixed separately inopm-simulators; this change only makes the surrounding docstring parse, which is what lets Sphinx notice it at all.