Skip to content

Commit

Permalink
🎨
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 7, 2025
1 parent b168801 commit 7036414
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions utils/remove_docstring_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None: # noqa: N802
and "Examples:" in docstring
and value.end_lineno is not None
):
examples_line_start = value.lineno + [
line.strip() for line in docstring.splitlines()
].index("Examples:")
examples_line_end = value.end_lineno
self.to_remove.append((examples_line_start, examples_line_end))
stripped_lines = [line.strip() for line in docstring.splitlines()]
examples_start = value.lineno + stripped_lines.index("Examples:")
# lineno is 1-indexed, so we subtract 1 to have 0-indexed numbers.
self.to_remove.append((examples_start - 1, value.end_lineno - 1))

self.generic_visit(node)

Expand All @@ -57,11 +56,8 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None: # noqa: N802
visitor.visit(tree)
if visitor.to_remove:
lines = content.splitlines()
for examples_start, examples_end in sorted(
visitor.to_remove, key=lambda x: -x[0]
):
# Remove from "Examples:" until the line before the docstring
# ends (we don't want to remove the final triple-backquote!).
del lines[examples_start : examples_end - 1]
removals = sorted(visitor.to_remove, key=lambda x: -x[0])
for examples_start, examples_end in removals:
del lines[examples_start:examples_end]
with open(file, "w") as fd:
fd.write("\n".join(lines))

0 comments on commit 7036414

Please sign in to comment.