Skip to content

Bug, Unhandled case when a section is empty #160

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

Closed
jspaezp opened this issue Sep 9, 2022 · 2 comments · Fixed by #163 · May be fixed by #162
Closed

Bug, Unhandled case when a section is empty #160

jspaezp opened this issue Sep 9, 2022 · 2 comments · Fixed by #163 · May be fixed by #162
Labels
bug Something isn't working

Comments

@jspaezp
Copy link
Contributor

jspaezp commented Sep 9, 2022

Issue

# >>> Call Args:  ['tests/data/format/numpydoc/numpydoc_parameter_spacing.py', '--style', 'numpydoc', '--style', 'pep257']

This call fails with an index error because section[0] has length 0, therefore section[0][0] returns an index error.

Fix

Modifying:

# Check that indent on first line of section didn't get weird
first_section = True
for section in new_sections.values():
if first_section:
section[0] = section[0].lstrip()
first_section = False
elif not section[0][0].isspace():
section[0] = f"{' ' * indent_length:s}{section[0]:s}"

To:

 # Check that indent on first line of section didn't get weird 
 first_section = True 
 for section in new_sections.values(): 
     if first_section: 
         section[0] = section[0].lstrip() 
         first_section = False 
     elif len(section[0]) > 0 and not section[0][0].isspace(): # <<<<<< this is the fix
         section[0] = f"{' ' * indent_length:s}{section[0]:s}" 

Related:

Breaking call:

# >>> Call Args:  ['tests/data/format/summary_splitter/regression_151.py', '--style', 'numpydoc', '--linewrap-full-docstring']

Fix:

summary_lines = summary.splitlines()
new_summary = "\n".join(
textwrap.wrap(
summary_lines[0],
width=line_length,
initial_indent=" " * (indent_length + quotes_length),
subsequent_indent=" " * indent_length,
replace_whitespace=True,
)
)[indent_length + quotes_length :]

        summary_lines = summary.splitlines()

        # Here is the fix, checks if the summary line is empty before going though
        if len(summary_lines) == 0:
            summary_lines = [""]

        new_summary = "\n".join(
            textwrap.wrap(
                summary_lines[0],
                width=line_length,
                initial_indent=" " * (indent_length + quotes_length),
                subsequent_indent=" " * indent_length,
                replace_whitespace=True,
            )
        )[indent_length + quotes_length :]


@Pierre-Sassoulas Pierre-Sassoulas added the bug Something isn't working label Sep 9, 2022
@DanielNoord
Copy link
Owner

Sounds good! Could you open a PR with the proposed fix and a test?

@DanielNoord
Copy link
Owner

Reopening as the test in #162 shows this can still reproduce in some edge cases.

I can't immediately think of a fix for #162 as it also needs to raise some sort of user warning like we saw in #278

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants