From b9158b96d261eb6126dbd035aa72c621ddea91e8 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Mon, 21 Jun 2021 02:43:55 +0900 Subject: [PATCH] Fix #9364: autodoc: 1-element tuple on the defarg is wrongly rendered --- CHANGES | 2 ++ sphinx/pycode/ast.py | 8 +++++--- tests/test_pycode_ast.py | 3 ++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index a0f1bab262..e48dcace95 100644 --- a/CHANGES +++ b/CHANGES @@ -65,6 +65,8 @@ Bugs fixed * #9250: autodoc: The inherited method not having docstring is wrongly parsed * #9283: autodoc: autoattribute directive failed to generate document for an attribute not having any comment +* #9364: autodoc: single element tuple on the default argument value is wrongly + rendered * #9317: html: Pushing left key causes visiting the next page at the first page * #9270: html theme : pyramid theme generates incorrect logo links * #9217: manpage: The name of manpage directory that is generated by diff --git a/sphinx/pycode/ast.py b/sphinx/pycode/ast.py index f541ec0a90..23da575bde 100644 --- a/sphinx/pycode/ast.py +++ b/sphinx/pycode/ast.py @@ -213,10 +213,12 @@ def visit_UnaryOp(self, node: ast.UnaryOp) -> str: return "%s %s" % (self.visit(node.op), self.visit(node.operand)) def visit_Tuple(self, node: ast.Tuple) -> str: - if node.elts: - return "(" + ", ".join(self.visit(e) for e in node.elts) + ")" - else: + if len(node.elts) == 0: return "()" + elif len(node.elts) == 1: + return "(%s,)" % self.visit(node.elts[0]) + else: + return "(" + ", ".join(self.visit(e) for e in node.elts) + ")" if sys.version_info < (3, 8): # these ast nodes were deprecated in python 3.8 diff --git a/tests/test_pycode_ast.py b/tests/test_pycode_ast.py index e800623517..6ae7050da3 100644 --- a/tests/test_pycode_ast.py +++ b/tests/test_pycode_ast.py @@ -53,8 +53,9 @@ ("+ a", "+ a"), # UAdd ("- 1", "- 1"), # UnaryOp ("- a", "- a"), # USub - ("(1, 2, 3)", "(1, 2, 3)"), # Tuple + ("(1, 2, 3)", "(1, 2, 3)"), # Tuple ("()", "()"), # Tuple (empty) + ("(1,)", "(1,)"), # Tuple (single item) ]) def test_unparse(source, expected): module = ast.parse(source)