Skip to content

Commit

Permalink
small test
Browse files Browse the repository at this point in the history
  • Loading branch information
he11owthere authored Dec 1, 2023
1 parent 287b257 commit 306b148
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions ivy_lint/formatters/docs_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ 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}")
return False
return True

def format_docstring(self, doc):
"""Formats a single docstring."""
Expand All @@ -19,29 +21,34 @@ def format_docstring(self, doc):
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"]
"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
codeblock_start_lines = set() # This will store indices of lines which start a code block
lines_to_modify = set() # This will store the indices of indented lines not containing "..."
incorrect_sections = set()
prev_line = ""
is_codeblock_cont = False
lb = 0
rb = 0

for idx, line in enumerate(lines):
stripped_line = line.strip()

if stripped_line.startswith('-') and stripped_line.endswith('-'):
section_title = prev_line
self.validate_section_name(section_title, VALID_SECTION_NAMES)
if not self.validate_section_name(section_title, VALID_SECTION_NAMES):
incorrect_sections.add(idx)

if not is_codeblock and stripped_line.startswith('>>>'):
is_codeblock = True
codeblock_start_lines.add(idx)
elif is_codeblock and not is_codeblock_cont and (not stripped_line or (not stripped_line.startswith(('>>>', '...')))):
is_codeblock = False

if is_codeblock:
if stripped_line.startswith(('>>>')):
lb = rb = 0
Expand Down Expand Up @@ -77,6 +84,8 @@ def format_docstring(self, doc):
indentation = len(formatted_lines[-2]) - len(formatted_lines[-2].lstrip())
formatted_lines[-1] = (indentation * ' ') + '...' + line[indentation:]
continue
if idx in incorrect_sections:
formatted_lines[-1] = "INCORRECT"
formatted_lines.append(line)

return '\n'.join(formatted_lines)
Expand Down

0 comments on commit 306b148

Please sign in to comment.