Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 7, 2025
1 parent 6c51656 commit 437f0b2
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions utils/remove_docstring_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,36 @@
from ast import NodeVisitor


def process_node(node: ast.FunctionDef | ast.ClassDef) -> tuple[int, int] | None:
"""If node contains a docstring example, return start and end lines."""
if (
node.body
and isinstance(expr := node.body[0], ast.Expr)
and isinstance(value := expr.value, ast.Constant)
and isinstance(docstring := value.value, str)
and "Examples:" in docstring
and value.end_lineno is not None
):
docstring_lines = [line.strip() for line in docstring.splitlines()]
examples_line_start = value.lineno + docstring_lines.index("Examples:")
# lineno is 1-indexed so we subtract 1.
return (examples_line_start - 1, value.end_lineno - 1)
return None


class Visitor(NodeVisitor):
def __init__(self, file: str) -> None:
self.file = file
self.to_remove: list[tuple[int, int]] = []

def visit_FunctionDef(self, node: ast.FunctionDef) -> None: # noqa: N802
if (
node.body
and isinstance(expr := node.body[0], ast.Expr)
and isinstance(value := expr.value, ast.Constant)
and isinstance(docstring := value.value, str)
and "Examples:" in docstring
and value.end_lineno is not None
):
docstring_lines = [line.strip() for line in docstring.splitlines()]
examples_line_start = value.lineno + docstring_lines.index("Examples:")
# lineno is 1-indexed so we subtract 1.
self.to_remove.append((examples_line_start - 1, value.end_lineno - 1))

if removal := process_node(node):
self.to_remove.append(removal)
self.generic_visit(node)

def visit_ClassDef(self, node: ast.ClassDef) -> None: # noqa: N802
if (
node.body
and isinstance(expr := node.body[0], ast.Expr)
and isinstance(value := expr.value, ast.Constant)
and isinstance(docstring := value.value, str)
and "Examples:" in docstring
and value.end_lineno is not None
):
docstring_lines = [line.strip() for line in docstring.splitlines()]
examples_line_start = value.lineno + docstring_lines.index("Examples:")
# lineno is 1-indexed so we subtract 1.
self.to_remove.append((examples_line_start, value.end_lineno))

if removal := process_node(node):
self.to_remove.append(removal)
self.generic_visit(node)


Expand Down

0 comments on commit 437f0b2

Please sign in to comment.