Skip to content

Fix numpy docstring args being misparsed as section headers (fixes #439)#686

Open
UditDewan wants to merge 1 commit into
google:masterfrom
UditDewan:fix-numpy-docstring-arg-section-title
Open

Fix numpy docstring args being misparsed as section headers (fixes #439)#686
UditDewan wants to merge 1 commit into
google:masterfrom
UditDewan:fix-numpy-docstring-arg-section-title

Conversation

@UditDewan

Copy link
Copy Markdown

Fixes #439

Problem

With a numpy-style docstring, an argument whose name happens to match a section title keyword (argument, arg, param, parameter, key, ...) breaks help text alignment:

def run(executable, argument):
    """Run.

    Parameters
    ----------
    executable :
            Executable to run
    argument :
            Positional argument to the executable

    """

--help previously showed both descriptions under EXECUTABLE and nothing under ARGUMENT:

POSITIONAL ARGUMENTS
    EXECUTABLE
        Executable to run

        Positional argument to the executable
    ARGUMENT

Cause

Inside the numpy Parameters section, the line argument : sits at the same indentation as the section header, so _google_section_permitted allows it to start a new section, and _google_section matches argument against the Args section titles. The parser switches to a Google-style Args section, so the argument is never recorded and the following description line is appended to the previous argument.

Fix

Numpy parameter lines have whitespace before the colon (argument : int), while Google section headers do not (Args:). When the parser is inside a numpy-format section, a line whose colon is preceded by whitespace is now treated as a parameter definition rather than a new section header.

With the fix, --help shows:

POSITIONAL ARGUMENTS
    EXECUTABLE
        Executable to run
    ARGUMENT
        Positional argument to the executable

Added a regression test using the example from the issue. All existing docstring and helptext tests pass.

In a numpy-style Parameters section, an argument whose name matches a
section title keyword (e.g. "argument", "param", "key") was
misinterpreted as the start of a new Google-style Args section. The
argument was then dropped from the parsed docstring and its description
was appended to the previous argument, producing misaligned help text.

Numpy parameter lines have whitespace before the colon ("argument :
int"), while Google section headers do not ("Args:"), so treat such
lines inside a numpy section as parameters rather than section headers.

Fixes google#439
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.

Misaligned help text with positional arguments and numpydoc

1 participant