Skip to content

Commit

Permalink
Fix #9364: autodoc: 1-element tuple on the defarg is wrongly rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Jun 20, 2021
1 parent 6918e69 commit b9158b9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions sphinx/pycode/ast.py
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/test_pycode_ast.py
Expand Up @@ -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)
Expand Down

0 comments on commit b9158b9

Please sign in to comment.