Fix RST field list absorbing trailing blocks into last field (#86) - #113
Open
apoorvdarshan wants to merge 1 commit into
Open
Fix RST field list absorbing trailing blocks into last field (#86)#113apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
In the ReST parser, the field-splitting regex only broke a field at the next `:field:` line or end of input. A trailing block after the field list (for example an `Example` section following `:rtype: dict`) has no leading `:`, so it was absorbed into the preceding field's value, leaving type_name as e.g. "dict\nExample:\n>>> ...". Terminate a field at a blank line followed by unindented, non-field content, which ends the RST field list. Indented continuation lines and unindented continuation lines without a blank separator are unaffected. Fixesrr-#86
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 freeto 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.
Fixes#86
Root cause
In the ReST parser (
docstring_parser/rest.py), the field list is split into individual fields with:A field only ends at the next line starting with
:or at the end of input. Any trailing block that follows the field list but has no leading:(e.g. anExamplesection after:rtype: dict) therefore gets pulled into the value of the last field. The last:rtype:/:param:type then swallows everything after it.Reproduction
Wrong output (before the fix):
Corrected output (after the fix):
Fix
Terminate a field not only at the next
:field:or end of input, but also at a blank line followed by unindented, non-field content — which is where an RST field list ends:r"(^:.*?)(?=^:|\n[ \t]*\n(?=[^ \t\n:])|\Z)"The new alternative
\n[ \t]*\n(?=[^ \t\n:])matches a blank line followed by a line that starts at the base indentation and is not another field, so the trailing block is no longer absorbed. This is deliberately narrow:Tests
Added
test_returns_does_not_absorb_trailing_blockindocstring_parser/tests/test_rest.py, which asserts that a:rtype: dictfollowed by a trailingExampleblock yieldstype_name == "dict". The test fails on the unmodified code (the type name includes the whole Example block) and passes with the fix.The full existing test suite passes (255 passed).
black,isort, andpylint(repo config) are clean on the changed files.Disclosure: prepared with AI assistance; reviewed and verified locally.