Skip to content

Commit 637c32e

Browse files
Fix IndexErrors when the "Examples" is the last section in a docstring (#292)
* Fix case where Examples section is at the last Co-Authored-By: Justus Magin <14808389+keewis@users.noreply.github.com> * Prevent unnecessary empty lines Co-Authored-By: Justus Magin <14808389+keewis@users.noreply.github.com> --------- Co-authored-by: Justus Magin <14808389+keewis@users.noreply.github.com>
1 parent d2a34cb commit 637c32e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

jupyterlite_sphinx/_try_examples.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,14 @@ def insert_try_examples_directive(lines, **options):
391391
lines[right_index]
392392
):
393393
right_index += 1
394-
if "!! processed by numpydoc !!" in lines[right_index]:
394+
395+
# Check if we've reached the end of the docstring
396+
if right_index < len(lines) and "!! processed by numpydoc !!" in lines[right_index]:
395397
# Sometimes the .. appears on an earlier line than !! processed by numpydoc !!
396398
if not re.search(
397399
r"\.\.\s+\!\! processed by numpy doc \!\!", lines[right_index]
398400
):
399-
while lines[right_index].strip() != "..":
401+
while right_index > 0 and lines[right_index].strip() != "..":
400402
right_index -= 1
401403

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

413+
# Append the remainder of the docstring, if there is any
414+
if right_index < len(lines):
415+
new_lines += [""] + lines[right_index:]
416+
413417
return new_lines

0 commit comments

Comments
 (0)