Skip to content

Commit

Permalink
section name checker
Browse files Browse the repository at this point in the history
  • Loading branch information
he11owthere authored Dec 1, 2023
1 parent 33cd752 commit 60cd1c8
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ivy_lint/formatters/docs_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from ivy_lint.formatters import BaseFormatter, BaseDocstringFormatter

class DocstringFormatter(BaseDocstringFormatter):
def validate_section_name(self, section_name, VALID_SECTION_NAMES):
if section_name not in VALID_SECTION_NAMES:
raise ValueError(f"Invalid section name: {section_name}. Valid section names are {VALID_SECTION_NAMES}")

def format_docstring(self, doc):
"""Formats a single docstring."""
# Rename "Functional Examples" to "Examples" and format it without the extra newline
Expand All @@ -14,6 +18,10 @@ def format_docstring(self, doc):
# Ensure newline and correct indentation after "Examples" when it's already there
doc = re.sub(r'(\s*)Examples\n\1--------\s*\n+([^\n])', r'\1Examples\n\1--------\n\2', doc)

VALID_SECTION_NAMES = ["Args", "Arguments", "Attention", "Attributes", "Caution", "Danger", "Error", "Example", "Examples", "Hint", "Important",
"Keyword Args", "Keyword Arguments", "Methods", "Note", "Notes", "Other Parameters", "Parameters", "Return", "Returns", "Raise",
"Raises", "References", "See Also", "Tip", "Todo", "Warning", "Warnings", "Warn", "Warns", "Yield", "Yields"]

# Identify code blocks
lines = doc.split('\n')
is_codeblock = False
Expand All @@ -25,6 +33,9 @@ def format_docstring(self, doc):

for idx, line in enumerate(lines):
stripped_line = line.strip()
if stripped_line.startswith('-') and stripped_line.endswith('-'):
section_title = prev_line
validate_section_name(section_title, VALID_SECTION_NAMES)
if not is_codeblock and stripped_line.startswith('>>>'):
is_codeblock = True
codeblock_start_lines.add(idx)
Expand All @@ -45,6 +56,7 @@ def format_docstring(self, doc):
is_codeblock_cont = True
if not stripped_line.startswith(('>>>', '...')):
lines_to_modify.add(idx)
prev_line = stripped_line

# Add blank lines before code blocks
formatted_lines = []
Expand Down

0 comments on commit 60cd1c8

Please sign in to comment.