Skip to content

Commit

Permalink
Fix sphinx-doc#8372: autodoc: autoclass directive became slower than …
Browse files Browse the repository at this point in the history
…Sphinx-3.2

* The result of ModuleAnalyzer.parse() is not cached
* autodoc tries to search overloaded constructor methods to the root
  class even if a definition found
  • Loading branch information
tk0miya committed Nov 8, 2020
1 parent 1193d83 commit e3eec9f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -16,6 +16,7 @@ Features added
Bugs fixed
----------

* #8372: autodoc: autoclass directive became slower than Sphinx-3.2
* #8364: C, properly initialize attributes in empty symbols.

Testing
Expand Down
3 changes: 3 additions & 0 deletions sphinx/ext/autodoc/__init__.py
Expand Up @@ -1503,6 +1503,9 @@ def get_overloaded_signatures(self) -> List[Signature]:
qualname = '.'.join([cls.__qualname__, self._signature_method_name])
if qualname in analyzer.overloads:
return analyzer.overloads.get(qualname)
elif qualname in analyzer.tagorder:
# the constructor is defined in the class, but not overrided.
return []
except PycodeError:
pass

Expand Down
13 changes: 7 additions & 6 deletions sphinx/pycode/__init__.py
Expand Up @@ -150,9 +150,13 @@ def __init__(self, source: IO, modname: str, srcname: str, decoded: bool = False
self.overloads = None # type: Dict[str, List[Signature]]
self.tagorder = None # type: Dict[str, int]
self.tags = None # type: Dict[str, Tuple[str, int, int]]
self._parsed = False

def parse(self) -> None:
"""Parse the source code."""
if self._parsed:
return None

try:
parser = Parser(self.code, self._encoding)
parser.parse()
Expand All @@ -169,21 +173,18 @@ def parse(self) -> None:
self.overloads = parser.overloads
self.tags = parser.definitions
self.tagorder = parser.deforders
self._parsed = True
except Exception as exc:
raise PycodeError('parsing %r failed: %r' % (self.srcname, exc)) from exc

def find_attr_docs(self) -> Dict[Tuple[str, str], List[str]]:
"""Find class and module-level attributes and their documentation."""
if self.attr_docs is None:
self.parse()

self.parse()
return self.attr_docs

def find_tags(self) -> Dict[str, Tuple[str, int, int]]:
"""Find class, function and method definitions and their location."""
if self.tags is None:
self.parse()

self.parse()
return self.tags

@property
Expand Down

0 comments on commit e3eec9f

Please sign in to comment.