diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-05-06 20:08:42 +0300 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2020-05-07 05:10:32 +0300 |
commit | 2cfa3370e7edd6bb7c40ba46bc70903de5a890db (patch) | |
tree | 46e7d058d3f7f3650c01bcc0fd36856e36505471 /sphinx/pycode/ast.py | |
parent | 11ff9207e6f161c121ff58aad534688f416ae431 (diff) |
refactor: Sort visitor methods of _UnparseVisitor
Diffstat (limited to 'sphinx/pycode/ast.py')
-rw-r--r-- | sphinx/pycode/ast.py | 124 |
1 files changed, 62 insertions, 62 deletions
diff --git a/sphinx/pycode/ast.py b/sphinx/pycode/ast.py index 3fe0a7fcb..e6f1d2b35 100644 --- a/sphinx/pycode/ast.py +++ b/sphinx/pycode/ast.py @@ -81,6 +81,54 @@ class _UnparseVisitor(ast.NodeVisitor): else: return node.arg + def _visit_arg_with_default(self, arg: ast.arg, default: Optional[ast.AST]) -> str: + """Unparse a single argument to a string.""" + name = self.visit(arg) + if default: + if arg.annotation: + name += " = %s" % self.visit(default) + else: + name += "=%s" % self.visit(default) + return name + + def visit_arguments(self, node: ast.arguments) -> str: + defaults = list(node.defaults) + positionals = len(node.args) + posonlyargs = 0 + if hasattr(node, "posonlyargs"): # for py38+ + posonlyargs += len(node.posonlyargs) # type:ignore + positionals += posonlyargs + for _ in range(len(defaults), positionals): + defaults.insert(0, None) + + kw_defaults = list(node.kw_defaults) + for _ in range(len(kw_defaults), len(node.kwonlyargs)): + kw_defaults.insert(0, None) + + args = [] # type: List[str] + if hasattr(node, "posonlyargs"): # for py38+ + for i, arg in enumerate(node.posonlyargs): # type: ignore + args.append(self._visit_arg_with_default(arg, defaults[i])) + + if node.posonlyargs: # type: ignore + args.append('/') + + for i, arg in enumerate(node.args): + args.append(self._visit_arg_with_default(arg, defaults[i + posonlyargs])) + + if node.vararg: + args.append("*" + self.visit(node.vararg)) + + if node.kwonlyargs and not node.vararg: + args.append('*') + for i, arg in enumerate(node.kwonlyargs): + args.append(self._visit_arg_with_default(arg, kw_defaults[i])) + + if node.kwarg: + args.append("**" + self.visit(node.kwarg)) + + return ", ".join(args) + def visit_Attribute(self, node: ast.Attribute) -> str: return "%s.%s" % (self.visit(node.value), node.attr) @@ -114,23 +162,6 @@ class _UnparseVisitor(ast.NodeVisitor): def visit_Name(self, node: ast.Name) -> str: return node.id - if sys.version_info < (3, 8): - # these ast nodes were deprecated in python 3.8 - def visit_Num(self, node: ast.Num) -> str: - return repr(node.n) - - def visit_Str(self, node: ast.Str) -> str: - return repr(node.s) - - def visit_Bytes(self, node: ast.Bytes) -> str: - return repr(node.s) - - def visit_NameConstant(self, node: ast.NameConstant) -> str: - return repr(node.value) - - def visit_Ellipsis(self, node: ast.Ellipsis) -> str: - return "..." - def visit_Set(self, node: ast.Set) -> str: return "{" + ", ".join(self.visit(e) for e in node.elts) + "}" @@ -153,53 +184,22 @@ class _UnparseVisitor(ast.NodeVisitor): else: return repr(node.value) - def generic_visit(self, node): - raise NotImplementedError('Unable to parse %s object' % type(node).__name__) - - def _visit_arg_with_default(self, arg: ast.arg, default: Optional[ast.AST]) -> str: - """Unparse a single argument to a string.""" - name = self.visit(arg) - if default: - if arg.annotation: - name += " = %s" % self.visit(default) - else: - name += "=%s" % self.visit(default) - return name - - def visit_arguments(self, node: ast.arguments) -> str: - defaults = list(node.defaults) - positionals = len(node.args) - posonlyargs = 0 - if hasattr(node, "posonlyargs"): # for py38+ - posonlyargs += len(node.posonlyargs) # type:ignore - positionals += posonlyargs - for _ in range(len(defaults), positionals): - defaults.insert(0, None) - - kw_defaults = list(node.kw_defaults) - for _ in range(len(kw_defaults), len(node.kwonlyargs)): - kw_defaults.insert(0, None) - - args = [] # type: List[str] - if hasattr(node, "posonlyargs"): # for py38+ - for i, arg in enumerate(node.posonlyargs): # type: ignore - args.append(self._visit_arg_with_default(arg, defaults[i])) - - if node.posonlyargs: # type: ignore - args.append('/') + if sys.version_info < (3, 8): + # these ast nodes were deprecated in python 3.8 + def visit_Bytes(self, node: ast.Bytes) -> str: + return repr(node.s) - for i, arg in enumerate(node.args): - args.append(self._visit_arg_with_default(arg, defaults[i + posonlyargs])) + def visit_Ellipsis(self, node: ast.Ellipsis) -> str: + return "..." - if node.vararg: - args.append("*" + self.visit(node.vararg)) + def visit_NameConstant(self, node: ast.NameConstant) -> str: + return repr(node.value) - if node.kwonlyargs and not node.vararg: - args.append('*') - for i, arg in enumerate(node.kwonlyargs): - args.append(self._visit_arg_with_default(arg, kw_defaults[i])) + def visit_Num(self, node: ast.Num) -> str: + return repr(node.n) - if node.kwarg: - args.append("**" + self.visit(node.kwarg)) + def visit_Str(self, node: ast.Str) -> str: + return repr(node.s) - return ", ".join(args) + def generic_visit(self, node): + raise NotImplementedError('Unable to parse %s object' % type(node).__name__) |