Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Remove comments from the function definition lines before generating docstrings #21536

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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