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]