autodoc: refactor __slots__ operations using getslots()

This commit is contained in:
Takeshi KOMIYA 2020-11-10 02:49:45 +09:00
parent bbb0ea5f12
commit 11d0a7b75b
2 changed files with 11 additions and 10 deletions

View File

@ -2270,8 +2270,8 @@ class SlotsAttributeDocumenter(AttributeDocumenter):
% self.__class__.__name__, % self.__class__.__name__,
RemovedInSphinx50Warning, stacklevel=2) RemovedInSphinx50Warning, stacklevel=2)
name = self.objpath[-1] name = self.objpath[-1]
__slots__ = safe_getattr(self.parent, '__slots__', []) __slots__ = inspect.getslots(self.parent)
if isinstance(__slots__, dict) and isinstance(__slots__.get(name), str): if __slots__ and isinstance(__slots__.get(name, None), str):
docstring = prepare_docstring(__slots__[name]) docstring = prepare_docstring(__slots__[name])
return [docstring] return [docstring]
else: else:

View File

@ -16,7 +16,7 @@ from typing import Any, Callable, Dict, List, Mapping, NamedTuple, Optional, Tup
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
from sphinx.pycode import ModuleAnalyzer from sphinx.pycode import ModuleAnalyzer
from sphinx.util import logging from sphinx.util import logging
from sphinx.util.inspect import isclass, isenumclass, safe_getattr from sphinx.util.inspect import getslots, isclass, isenumclass, safe_getattr
if False: if False:
# For type annotation # For type annotation
@ -203,14 +203,15 @@ def get_object_members(subject: Any, objpath: List[str], attrgetter: Callable,
members[name] = Attribute(name, True, value) members[name] = Attribute(name, True, value)
# members in __slots__ # members in __slots__
if isclass(subject) and getattr(subject, '__slots__', None) is not None: try:
__slots__ = getslots(subject)
if __slots__:
from sphinx.ext.autodoc import SLOTSATTR from sphinx.ext.autodoc import SLOTSATTR
slots = subject.__slots__ for name in __slots__:
if isinstance(slots, str):
slots = [slots]
for name in slots:
members[name] = Attribute(name, True, SLOTSATTR) members[name] = Attribute(name, True, SLOTSATTR)
except (TypeError, ValueError):
pass
# other members # other members
for name in dir(subject): for name in dir(subject):