Skip to content

Commit

Permalink
Fix issue with ABC-derived classes having their members ignored
Browse files Browse the repository at this point in the history
ABC-derived classes (i.e. those deriving from the `abc` and `collections.abc` modules) now include their members (as defined in `__dict__`) in generated `autosummary` RST. `__slots__` may still be used to override `__dict__`, but if an ABC-derived class has empty slots, its `__dict__` will be used instead.

Fixes #52
  • Loading branch information
jairideout committed Oct 31, 2018
1 parent d46a0c1 commit 2d2547a
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
13 changes: 12 additions & 1 deletion sphinx_automodapi/automodsumm.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class members that are inherited from a base class. This value can be
.. _sphinx.ext.inheritance_diagram: http://sphinx-doc.org/latest/ext/inheritance.html
"""

import abc
import inspect
import os
import re
Expand Down Expand Up @@ -551,7 +552,17 @@ def get_members_class(obj, typ, include_public=[],
if include_base:
names = dir(obj)
else:
if hasattr(obj, '__slots__'):
# Classes deriving from an ABC using the `abc` module will
# have empty `__slots__`, unless other slots were declared
# along the inheritance chain. If the ABC-derived class has
# empty slots, we'll use the class `__dict__` instead.
declares_slots = (
hasattr(obj, '__slots__') and
not (type(obj) is abc.ABCMeta and
len(obj.__slots__) == 0)
)

if declares_slots:
names = tuple(getattr(obj, '__slots__'))
else:
names = getattr(obj, '__dict__').keys()
Expand Down
2 changes: 2 additions & 0 deletions sphinx_automodapi/tests/cases/abstract_classes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This example is to make sure that classes derived from the `abc` module will
have their members listed (https://github.com/astropy/sphinx-automodapi/issues/52).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. automodapi:: sphinx_automodapi.tests.example_module.abstract_classes
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
SequenceSubclass
================

.. currentmodule:: sphinx_automodapi.tests.example_module.abstract_classes

.. autoclass:: SequenceSubclass
:show-inheritance:

.. rubric:: Attributes Summary

.. autosummary::

~SequenceSubclass.my_property

.. rubric:: Methods Summary

.. autosummary::

~SequenceSubclass.my_method

.. rubric:: Attributes Documentation

.. autoattribute:: my_property

.. rubric:: Methods Documentation

.. automethod:: my_method
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

sphinx_automodapi.tests.example_module.abstract_classes Module
--------------------------------------------------------------

.. automodule:: sphinx_automodapi.tests.example_module.abstract_classes

Classes
^^^^^^^

.. automodsumm:: sphinx_automodapi.tests.example_module.abstract_classes
:classes-only:
:toctree: api

Class Inheritance Diagram
^^^^^^^^^^^^^^^^^^^^^^^^^

.. automod-diagram:: sphinx_automodapi.tests.example_module.abstract_classes
:private-bases:
:parts: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. currentmodule:: sphinx_automodapi.tests.example_module.abstract_classes

.. autosummary::
:toctree: api

SequenceSubclass
37 changes: 37 additions & 0 deletions sphinx_automodapi/tests/example_module/abstract_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import collections.abc

__all__ = ['SequenceSubclass']


class SequenceSubclass(collections.abc.Sequence):
"""
Inherits from an ABC.
"""

def __init__(self):
self._items = []

def __len__(self):
"""
Must be defined for the collections.abc.Sequence base class.
"""
return len(self._items)

def __getitem__(self, key):
"""
Must be defined for the collections.abc.Sequence base class.
"""
return self._items[key]

def my_method(self, parameter):
"""
An example method.
"""
pass

@property
def my_property(self):
"""
An example property.
"""
return 42

0 comments on commit 2d2547a

Please sign in to comment.