diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2018-04-04 23:12:26 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2018-04-08 23:51:01 +0900 |
commit | 245e7d7bab6cccc29ae4ef405c8cc81c2845b3a0 (patch) | |
tree | 7dd540e60a0a72c92f2bb3c2c6907edae52f3318 /sphinx/pycode/parser.py | |
parent | 7e1707000feb43b5ea0d1dffe0c5cb1b9dfc268e (diff) | |
download | sphinx-git-245e7d7bab6cccc29ae4ef405c8cc81c2845b3a0.tar.gz |
Fix #4812: autodoc ignores type annotated variables
Diffstat (limited to 'sphinx/pycode/parser.py')
-rw-r--r-- | sphinx/pycode/parser.py | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py index f15da664a..deba48b1c 100644 --- a/sphinx/pycode/parser.py +++ b/sphinx/pycode/parser.py @@ -12,6 +12,7 @@ import ast import inspect import itertools import re +import sys import tokenize from token import NAME, NEWLINE, INDENT, DEDENT, NUMBER, OP, STRING from tokenize import COMMENT, NL @@ -27,6 +28,21 @@ indent_re = re.compile(u'^\\s*$') emptyline_re = re.compile(u'^\\s*(#.*)?$') +if sys.version_info >= (3, 6): + ASSIGN_NODES = (ast.Assign, ast.AnnAssign) +else: + ASSIGN_NODES = (ast.Assign) + + +def get_assign_targets(node): + # type: (ast.AST) -> List[ast.expr] + """Get list of targets from Assign and AnnAssign node.""" + if isinstance(node, ast.Assign): + return node.targets + else: + return [node.target] # type: ignore + + def get_lvar_names(node, self=None): # type: (ast.AST, ast.expr) -> List[unicode] """Convert assignment-AST to variable names. @@ -284,7 +300,8 @@ class VariableCommentPicker(ast.NodeVisitor): # type: (ast.Assign) -> None """Handles Assign node and pick up a variable comment.""" try: - varnames = sum([get_lvar_names(t, self=self.get_self()) for t in node.targets], []) + targets = get_assign_targets(node) + varnames = sum([get_lvar_names(t, self=self.get_self()) for t in targets], []) current_line = self.get_line(node.lineno) except TypeError: return # this assignment is not new definition! @@ -320,12 +337,18 @@ class VariableCommentPicker(ast.NodeVisitor): for varname in varnames: self.add_entry(varname) + def visit_AnnAssign(self, node): + # type: (ast.AST) -> None + """Handles AnnAssign node and pick up a variable comment.""" + self.visit_Assign(node) # type: ignore + def visit_Expr(self, node): # type: (ast.Expr) -> None """Handles Expr node and pick up a comment if string.""" - if (isinstance(self.previous, ast.Assign) and isinstance(node.value, ast.Str)): + if (isinstance(self.previous, ASSIGN_NODES) and isinstance(node.value, ast.Str)): try: - varnames = get_lvar_names(self.previous.targets[0], self.get_self()) + targets = get_assign_targets(self.previous) + varnames = get_lvar_names(targets[0], self.get_self()) for varname in varnames: if isinstance(node.value.s, text_type): docstring = node.value.s |