summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sphinx/pycode/ast.py2
-rw-r--r--tests/test_pycode_ast.py8
2 files changed, 6 insertions, 4 deletions
diff --git a/sphinx/pycode/ast.py b/sphinx/pycode/ast.py
index 755116475..2a896a7c3 100644
--- a/sphinx/pycode/ast.py
+++ b/sphinx/pycode/ast.py
@@ -202,6 +202,8 @@ class _UnparseVisitor(ast.NodeVisitor):
return "%s[%s]" % (self.visit(node.value), self.visit(node.slice))
def visit_UnaryOp(self, node: ast.UnaryOp) -> str:
+ if not isinstance(node.op, ast.Not):
+ return "%s%s" % (self.visit(node.op), self.visit(node.operand))
return "%s %s" % (self.visit(node.op), self.visit(node.operand))
def visit_Tuple(self, node: ast.Tuple) -> str:
diff --git a/tests/test_pycode_ast.py b/tests/test_pycode_ast.py
index 6143105eb..2d33a5629 100644
--- a/tests/test_pycode_ast.py
+++ b/tests/test_pycode_ast.py
@@ -25,7 +25,7 @@ from sphinx.pycode import ast
("...", "..."), # Ellipsis
("a // b", "a // b"), # FloorDiv
("Tuple[int, int]", "Tuple[int, int]"), # Index, Subscript
- ("~ 1", "~ 1"), # Invert
+ ("~1", "~1"), # Invert
("lambda x, y: x + y",
"lambda x, y: ..."), # Lambda
("[1, 2, 3]", "[1, 2, 3]"), # List
@@ -42,9 +42,9 @@ from sphinx.pycode import ast
("{1, 2, 3}", "{1, 2, 3}"), # Set
("a - b", "a - b"), # Sub
("'str'", "'str'"), # Str
- ("+ a", "+ a"), # UAdd
- ("- 1", "- 1"), # UnaryOp
- ("- a", "- a"), # USub
+ ("+a", "+a"), # UAdd
+ ("-1", "-1"), # UnaryOp
+ ("-a", "-a"), # USub
("(1, 2, 3)", "(1, 2, 3)"), # Tuple
("()", "()"), # Tuple (empty)
("(1,)", "(1,)"), # Tuple (single item)