diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2021-03-27 00:37:36 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2021-03-27 00:41:15 +0900 |
commit | b9f0582f06a8fe1b042333b9e8eb3cd575f1db2c (patch) | |
tree | 79681383ead054763a6c71aa257bd5ce82245635 /sphinx/pycode/parser.py | |
parent | d27bdce2d7e1b2435eebc1f7cad0d7c4c56c910e (diff) | |
download | sphinx-git-b9f0582f06a8fe1b042333b9e8eb3cd575f1db2c.tar.gz |
refactor: Use PEP-526 based variable annotation (sphinx.pycode)
Diffstat (limited to 'sphinx/pycode/parser.py')
-rw-r--r-- | sphinx/pycode/parser.py | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py index d157c7c1c..fa249d8c5 100644 --- a/sphinx/pycode/parser.py +++ b/sphinx/pycode/parser.py @@ -129,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.""" @@ -178,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.""" @@ -221,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]]: @@ -350,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! @@ -466,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.""" @@ -543,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.""" |