Skip to content

Commit

Permalink
Merge pull request #21536 from rhkarls/docstring_gen_comment_fix
Browse files Browse the repository at this point in the history
PR: Remove comments from the function definition lines before generating docstrings
  • Loading branch information
ccordoba12 authored Nov 17, 2023
2 parents d4df044 + 02d6d26 commit c749154
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spyder/plugins/editor/extensions/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ def is_in_scope_backward(text):
text.replace(r"\"", "").replace(r"\'", "")[::-1])


def remove_comments(text):
"""
Remove code comments from text while ignoring hash symbols (#) inside
quotes, which can be part of function arguments.
"""
return re.sub(pattern=r"""(?<!['"])(#.*)""", repl="", string=text)


class DocstringWriterExtension(object):
"""Class for insert docstring template automatically."""

Expand Down Expand Up @@ -194,6 +202,7 @@ def get_function_definition_from_first_line(self):

for __ in range(min(remain_lines, 20)):
cur_text = to_text_string(cursor.block().text()).rstrip()
cur_text = remove_comments(cur_text)

if is_first_line:
if not is_start_of_function(cur_text):
Expand Down Expand Up @@ -239,6 +248,7 @@ def get_function_definition_from_below_last_line(self):

cursor.movePosition(QTextCursor.PreviousBlock)
prev_text = to_text_string(cursor.block().text()).rstrip()
prev_text = remove_comments(prev_text)

if is_first_line:
if not self.is_end_of_function_definition(
Expand Down
57 changes: 57 additions & 0 deletions spyder/plugins/editor/extensions/tests/test_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,60 @@ def test_docstring_line_break(editor_auto_docstring, text, expected):
editor.writer_docstring.write_docstring_for_shortcut()

assert editor.toPlainText() == expected


@pytest.mark.parametrize(
'text, expected',
[
(''' def test(v: str = "#"): # comment, with '#' and "#"
''',
''' def test(v: str = "#"): # comment, with '#' and "#"
"""\n \n
Parameters
----------
v : str, optional
DESCRIPTION. The default is "#".
Returns
-------
None.
"""
'''),
(''' def test(v1: str = "#", # comment, with '#' and "#"
v2: str = '#') -> str:
''',
''' def test(v1: str = "#", # comment, with '#' and "#"
v2: str = '#') -> str:
"""\n \n
Parameters
----------
v1 : str, optional
DESCRIPTION. The default is "#".
v2 : str, optional
DESCRIPTION. The default is '#'.
Returns
-------
str
DESCRIPTION.
"""
'''),
])
def test_docstring_comments(editor_auto_docstring, text, expected):
"""
Test auto docstring with comments on lines of function definition.
"""
CONF.set('editor', 'docstring_type', 'Numpydoc')
editor = editor_auto_docstring
editor.set_text(text)

cursor = editor.textCursor()
cursor.movePosition(QTextCursor.NextBlock)
cursor.setPosition(QTextCursor.End, QTextCursor.MoveAnchor)
editor.setTextCursor(cursor)

editor.writer_docstring.write_docstring_for_shortcut()

assert editor.toPlainText() == expected

0 comments on commit c749154

Please sign in to comment.