refactor: pycode: Use OrderedDict to store variable comments

It is worthy to keep the order of analyzer.attr_docs to generate
document in reproducible. So this uses OrderedDict explicitly to do
that. It also helps python3.5 environment.
This commit is contained in:
Takeshi KOMIYA 2020-07-19 16:00:52 +09:00
parent 68c732e97c
commit b6bf2b88ce
2 changed files with 4 additions and 2 deletions

View File

@ -11,6 +11,7 @@
import re
import tokenize
import warnings
from collections import OrderedDict
from importlib import import_module
from inspect import Signature
from io import StringIO
@ -156,7 +157,7 @@ class ModuleAnalyzer:
parser = Parser(self.code, self._encoding)
parser.parse()
self.attr_docs = {}
self.attr_docs = OrderedDict()
for (scope, comment) in parser.comments.items():
if comment:
self.attr_docs[scope] = comment.splitlines() + ['']

View File

@ -12,6 +12,7 @@ import itertools
import re
import sys
import tokenize
from collections import OrderedDict
from inspect import Signature
from token import NAME, NEWLINE, INDENT, DEDENT, NUMBER, OP, STRING
from tokenize import COMMENT, NL
@ -228,7 +229,7 @@ class VariableCommentPicker(ast.NodeVisitor):
self.context = [] # type: List[str]
self.current_classes = [] # type: List[str]
self.current_function = None # type: ast.FunctionDef
self.comments = {} # type: Dict[Tuple[str, str], str]
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]