summaryrefslogtreecommitdiff
path: root/sphinx/pycode/parser.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-04-26 20:18:56 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-04-26 20:19:33 +0900
commitad4ea6aeb6a8e0e2f566312ce8c8f96e702f0b7d (patch)
treea2085f28933d43840716bc18508361f966e33fbf /sphinx/pycode/parser.py
parent56ed2991fdd2185f82d0e201f9e0c58427ed4e4d (diff)
downloadsphinx-git-ad4ea6aeb6a8e0e2f566312ce8c8f96e702f0b7d.tar.gz
refactor: pycode: Add VariableCommentPicker.get_qualname_for()
Diffstat (limited to 'sphinx/pycode/parser.py')
-rw-r--r--sphinx/pycode/parser.py44
1 files changed, 18 insertions, 26 deletions
diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py
index cb3cf0cc1..b2c350707 100644
--- a/sphinx/pycode/parser.py
+++ b/sphinx/pycode/parser.py
@@ -14,7 +14,7 @@ import sys
import tokenize
from token import NAME, NEWLINE, INDENT, DEDENT, NUMBER, OP, STRING
from tokenize import COMMENT, NL
-from typing import Any, Dict, List, Tuple
+from typing import Any, Dict, List, Optional, Tuple
from sphinx.pycode.ast import ast # for py37 or older
from sphinx.pycode.ast import parse, unparse
@@ -233,41 +233,33 @@ class VariableCommentPicker(ast.NodeVisitor):
self.deforders = {} # type: Dict[str, int]
super().__init__()
- def add_entry(self, name: str) -> None:
+ def get_qualname_for(self, name: str) -> Optional[List[str]]:
+ """Get qualified name for given object as a list of string."""
if self.current_function:
if self.current_classes and self.context[-1] == "__init__":
# store variable comments inside __init__ method of classes
- definition = self.context[:-1] + [name]
+ return self.context[:-1] + [name]
else:
- return
+ return None
else:
- definition = self.context + [name]
+ return self.context + [name]
- self.deforders[".".join(definition)] = next(self.counter)
+ def add_entry(self, name: str) -> None:
+ qualname = self.get_qualname_for(name)
+ if qualname:
+ self.deforders[".".join(qualname)] = next(self.counter)
def add_variable_comment(self, name: str, comment: str) -> None:
- if self.current_function:
- if self.current_classes and self.context[-1] == "__init__":
- # store variable comments inside __init__ method of classes
- context = ".".join(self.context[:-1])
- else:
- return
- else:
- context = ".".join(self.context)
-
- self.comments[(context, name)] = comment
+ qualname = self.get_qualname_for(name)
+ if qualname:
+ basename = ".".join(qualname[:-1])
+ self.comments[(basename, name)] = comment
def add_variable_annotation(self, name: str, annotation: ast.AST) -> None:
- if self.current_function:
- if self.current_classes and self.context[-1] == "__init__":
- # store variable comments inside __init__ method of classes
- context = ".".join(self.context[:-1])
- else:
- return
- else:
- context = ".".join(self.context)
-
- self.annotations[(context, name)] = unparse(annotation)
+ qualname = self.get_qualname_for(name)
+ if qualname:
+ basename = ".".join(qualname[:-1])
+ self.annotations[(basename, name)] = unparse(annotation)
def get_self(self) -> ast.arg:
"""Returns the name of first argument if in function."""