Skip to content

Fix IndexErrors when the "Examples" is the last section in a docstring #292

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions jupyterlite_sphinx/_try_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,14 @@ def insert_try_examples_directive(lines, **options):
lines[right_index]
):
right_index += 1
if "!! processed by numpydoc !!" in lines[right_index]:

# Check if we've reached the end of the docstring
if right_index < len(lines) and "!! processed by numpydoc !!" in lines[right_index]:
# Sometimes the .. appears on an earlier line than !! processed by numpydoc !!
if not re.search(
r"\.\.\s+\!\! processed by numpy doc \!\!", lines[right_index]
):
while lines[right_index].strip() != "..":
while right_index > 0 and lines[right_index].strip() != "..":
right_index -= 1

# Add the ".. try_examples::" directive and indent the content of the Examples section
Expand All @@ -406,8 +408,10 @@ def insert_try_examples_directive(lines, **options):
+ [f" :{key}: {value}" for key, value in options.items()]
+ [""]
+ [" " + line for line in lines[left_index:right_index]]
+ [""]
+ lines[right_index:]
)

# Append the remainder of the docstring, if there is any
if right_index < len(lines):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make sure I understand correctly, this condition will be False in the case where the example section is last and there is no emptyline after it. The check keeps jupyterlite_sphinx from inserting an emptyline at the end in this case. If so, that sounds reasonable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right!

new_lines += [""] + lines[right_index:]

return new_lines
Loading