Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle exception from autodoc for classes with __slots__ #124

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions sphinx_autosummary_accessors/documenters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sphinx.ext.autodoc import AttributeDocumenter, Documenter, MethodDocumenter
from sphinx.ext.autodoc.importer import import_module
from sphinx.util import inspect


class AccessorLevelDocumenter(Documenter):
Expand Down Expand Up @@ -87,15 +88,44 @@ class AccessorAttributeDocumenter(AccessorLevelDocumenter, AttributeDocumenter):
priority = 0.6


class AccessorMethodDocumenter(AccessorLevelDocumenter, MethodDocumenter):
class SlottedParentMethodDocumenter(MethodDocumenter):
def import_object(self, raiseerror: bool = False) -> bool:
"""Copy pasted from MethodDocumenter to handle parent with __slots__"""
ret = super(MethodDocumenter, self).import_object(raiseerror)
if not ret:
return ret

# handle case when parent is instance with __slots__
# otherwise autodoc errors, because it expects parents to have __dict__
if hasattr(self.parent, "__slots__") and not isinstance(self.parent, type):
self.parent = self.parent.__class__

# to distinguish classmethod/staticmethod
obj = self.parent.__dict__.get(self.object_name)

if obj is None:
obj = self.object

if inspect.isclassmethod(obj) or inspect.isstaticmethod(
obj, cls=self.parent, name=self.object_name
):
# document class and static members before ordinary ones
self.member_order = self.member_order - 1

return ret


class AccessorMethodDocumenter(AccessorLevelDocumenter, SlottedParentMethodDocumenter):
objtype = "accessormethod"
directivetype = "method"

# lower than MethodDocumenter so this is not chosen for normal methods
priority = 0.6


class AccessorCallableDocumenter(AccessorLevelDocumenter, MethodDocumenter):
class AccessorCallableDocumenter(
AccessorLevelDocumenter, SlottedParentMethodDocumenter
):
"""
This documenter lets us removes .__call__ from the method signature for
callable accessors like Series.plot
Expand Down
Loading