From b6bf2b88cef6c2b1517d7948c61ca86abb20fd77 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 19 Jul 2020 16:00:52 +0900 Subject: [PATCH] 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. --- sphinx/pycode/__init__.py | 3 ++- sphinx/pycode/parser.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/sphinx/pycode/__init__.py b/sphinx/pycode/__init__.py index 29e53178f..4fef4a394 100644 --- a/sphinx/pycode/__init__.py +++ b/sphinx/pycode/__init__.py @@ -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() + [''] diff --git a/sphinx/pycode/parser.py b/sphinx/pycode/parser.py index d2e8fb976..be9bfc96d 100644 --- a/sphinx/pycode/parser.py +++ b/sphinx/pycode/parser.py @@ -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]