From 02d6d26783b4bce5fb6712fe9fccf3e89f74ce1a Mon Sep 17 00:00:00 2001 From: Reinert Huseby Karlsen <2330659+rhkarls@users.noreply.github.com> Date: Fri, 17 Nov 2023 19:57:14 +0100 Subject: [PATCH] Remove comments from the lines of function definitions before generating docstring. Add two test cases. --- spyder/plugins/editor/extensions/docstring.py | 10 ++++ .../editor/extensions/tests/test_docstring.py | 57 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/spyder/plugins/editor/extensions/docstring.py b/spyder/plugins/editor/extensions/docstring.py index e0205e63ba0..95c9596a8e5 100644 --- a/spyder/plugins/editor/extensions/docstring.py +++ b/spyder/plugins/editor/extensions/docstring.py @@ -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"""(? 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