diff options
Diffstat (limited to 'sphinx/pycode/parser.py')
-rw-r--r-- | sphinx/pycode/parser.py | 69 |
1 files changed, 33 insertions, 36 deletions
diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py index dca59acd4..fa249d8c5 100644 --- a/sphinx/pycode/parser.py +++ b/sphinx/pycode/parser.py @@ -10,7 +10,6 @@ import inspect import itertools import re -import sys import tokenize from collections import OrderedDict from inspect import Signature @@ -26,12 +25,6 @@ indent_re = re.compile('^\\s*$') emptyline_re = re.compile('^\\s*(#.*)?$') -if sys.version_info >= (3, 6): - ASSIGN_NODES = (ast.Assign, ast.AnnAssign) -else: - ASSIGN_NODES = (ast.Assign) - - def filter_whitespace(code: str) -> str: return code.replace('\f', ' ') # replace FF (form feed) with whitespace @@ -94,7 +87,10 @@ def dedent_docstring(s: str) -> str: dummy.__doc__ = s docstring = inspect.getdoc(dummy) - return docstring.lstrip("\r\n").rstrip("\r\n") + if docstring: + return docstring.lstrip("\r\n").rstrip("\r\n") + else: + return "" class Token: @@ -133,8 +129,8 @@ class TokenProcessor: lines = iter(buffers) self.buffers = buffers self.tokens = tokenize.generate_tokens(lambda: next(lines)) - self.current = None # type: Token - self.previous = None # type: Token + self.current: Token = None + self.previous: Token = None def get_line(self, lineno: int) -> str: """Returns specified line.""" @@ -182,7 +178,7 @@ class AfterCommentParser(TokenProcessor): def __init__(self, lines: List[str]) -> None: super().__init__(lines) - self.comment = None # type: str + self.comment: str = None def fetch_rvalue(self) -> List[Token]: """Fetch right-hand value of assignment.""" @@ -225,18 +221,18 @@ class VariableCommentPicker(ast.NodeVisitor): self.counter = itertools.count() self.buffers = buffers self.encoding = encoding - self.context = [] # type: List[str] - self.current_classes = [] # type: List[str] - self.current_function = None # type: ast.FunctionDef - self.comments = OrderedDict() # type: Dict[Tuple[str, str], str] - self.annotations = {} # type: Dict[Tuple[str, str], str] - self.previous = None # type: ast.AST - self.deforders = {} # type: Dict[str, int] - self.finals = [] # type: List[str] - self.overloads = {} # type: Dict[str, List[Signature]] - self.typing = None # type: str - self.typing_final = None # type: str - self.typing_overload = None # type: str + self.context: List[str] = [] + self.current_classes: List[str] = [] + self.current_function: ast.FunctionDef = None + self.comments: Dict[Tuple[str, str], str] = OrderedDict() + self.annotations: Dict[Tuple[str, str], str] = {} + self.previous: ast.AST = None + self.deforders: Dict[str, int] = {} + self.finals: List[str] = [] + self.overloads: Dict[str, List[Signature]] = {} + self.typing: str = None + self.typing_final: str = None + self.typing_overload: str = None super().__init__() def get_qualname_for(self, name: str) -> Optional[List[str]]: @@ -354,7 +350,7 @@ class VariableCommentPicker(ast.NodeVisitor): """Handles Assign node and pick up a variable comment.""" try: targets = get_assign_targets(node) - varnames = sum([get_lvar_names(t, self=self.get_self()) for t in targets], []) # type: List[str] # NOQA + varnames: List[str] = sum([get_lvar_names(t, self=self.get_self()) for t in targets], []) # NOQA current_line = self.get_line(node.lineno) except TypeError: return # this assignment is not new definition! @@ -398,13 +394,14 @@ class VariableCommentPicker(ast.NodeVisitor): for varname in varnames: self.add_entry(varname) - def visit_AnnAssign(self, node: ast.AST) -> None: # Note: ast.AnnAssign not found in py35 + def visit_AnnAssign(self, node: ast.AnnAssign) -> None: """Handles AnnAssign node and pick up a variable comment.""" self.visit_Assign(node) # type: ignore def visit_Expr(self, node: ast.Expr) -> None: """Handles Expr node and pick up a comment if string.""" - if (isinstance(self.previous, ASSIGN_NODES) and isinstance(node.value, ast.Str)): + if (isinstance(self.previous, (ast.Assign, ast.AnnAssign)) and + isinstance(node.value, ast.Str)): try: targets = get_assign_targets(self.previous) varnames = get_lvar_names(targets[0], self.get_self()) @@ -469,10 +466,10 @@ class DefinitionFinder(TokenProcessor): def __init__(self, lines: List[str]) -> None: super().__init__(lines) - self.decorator = None # type: Token - self.context = [] # type: List[str] - self.indents = [] # type: List - self.definitions = {} # type: Dict[str, Tuple[str, int, int]] + self.decorator: Token = None + self.context: List[str] = [] + self.indents: List = [] + self.definitions: Dict[str, Tuple[str, int, int]] = {} def add_definition(self, name: str, entry: Tuple[str, int, int]) -> None: """Add a location of definition.""" @@ -546,12 +543,12 @@ class Parser: def __init__(self, code: str, encoding: str = 'utf-8') -> None: self.code = filter_whitespace(code) self.encoding = encoding - self.annotations = {} # type: Dict[Tuple[str, str], str] - self.comments = {} # type: Dict[Tuple[str, str], str] - self.deforders = {} # type: Dict[str, int] - self.definitions = {} # type: Dict[str, Tuple[str, int, int]] - self.finals = [] # type: List[str] - self.overloads = {} # type: Dict[str, List[Signature]] + self.annotations: Dict[Tuple[str, str], str] = {} + self.comments: Dict[Tuple[str, str], str] = {} + self.deforders: Dict[str, int] = {} + self.definitions: Dict[str, Tuple[str, int, int]] = {} + self.finals: List[str] = [] + self.overloads: Dict[str, List[Signature]] = {} def parse(self) -> None: """Parse the source code.""" |