diff options
Diffstat (limited to 'sphinx/pycode/ast.py')
-rw-r--r-- | sphinx/pycode/ast.py | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/sphinx/pycode/ast.py b/sphinx/pycode/ast.py index 9bafff11c..9734cb476 100644 --- a/sphinx/pycode/ast.py +++ b/sphinx/pycode/ast.py @@ -10,6 +10,7 @@ import sys from typing import Dict, List, Type, Optional +from typing import overload if sys.version_info > (3, 8): import ast @@ -58,6 +59,16 @@ def parse(code: str, mode: str = 'exec') -> "ast.AST": return ast.parse(code, mode=mode) +@overload +def unparse(node: None) -> None: + ... + + +@overload +def unparse(node: ast.AST) -> str: + ... + + def unparse(node: Optional[ast.AST]) -> Optional[str]: """Unparse an AST to string.""" if node is None: @@ -144,6 +155,12 @@ class _UnparseVisitor(ast.NodeVisitor): ["%s=%s" % (k.arg, self.visit(k.value)) for k in node.keywords]) return "%s(%s)" % (self.visit(node.func), ", ".join(args)) + def visit_Constant(self, node: ast.Constant) -> str: # type: ignore + if node.value is Ellipsis: + return "..." + else: + return repr(node.value) + def visit_Dict(self, node: ast.Dict) -> str: keys = (self.visit(k) for k in node.keys) values = (self.visit(v) for v in node.values) @@ -177,13 +194,6 @@ class _UnparseVisitor(ast.NodeVisitor): else: return "()" - if sys.version_info >= (3, 6): - def visit_Constant(self, node: ast.Constant) -> str: - if node.value is Ellipsis: - return "..." - else: - return repr(node.value) - if sys.version_info < (3, 8): # these ast nodes were deprecated in python 3.8 def visit_Bytes(self, node: ast.Bytes) -> str: |