I have this class:
class Class:
pass
The documentation generated by sphinx (in case it matters, I used the autodoc
extension) looks like this:
class package.Class
Bases: object
The inheritance from object
isn't useful information for the reader, and therefore I don't want it in my documentation. The output I'd like to see is this:
class package.Class
Is there a way to exclude object
from the list of base classes?
This is actually deeply embedded in the autodoc
source code, with no way to turn it off:
bases = [b.__module__ in ('__builtin__', 'builtins') and
u':class:`%s`' % b.__name__ or
u':class:`%s.%s`' % (b.__module__, b.__name__)
for b in self.object.__bases__]
self.add_line(u' ' + _(u'Bases: %s') % ', '.join(bases), sourcename)
object
isn't given any special treatment; there's no builtin way to exclude it from the list.
The best (automatic) solution I could find was to monkeypatch autodoc.
Adding this to conf.py
enables the desired behavior:
# ClassDocumenter.add_directive_header uses ClassDocumenter.add_line to
# write the class documentation.
# We'll monkeypatch the add_line method and intercept lines that begin
# with "Bases:".
# In order to minimize the risk of accidentally intercepting a wrong line,
# we'll apply this patch inside of the add_directive_header method.
from sphinx.ext.autodoc import ClassDocumenter, _
add_line = ClassDocumenter.add_line
line_to_delete = _(u'Bases: %s') % u':class:`object`'
def add_line_no_object_base(self, text, *args, **kwargs):
if text.strip() == line_to_delete:
return
add_line(self, text, *args, **kwargs)
add_directive_header = ClassDocumenter.add_directive_header
def add_directive_header_no_object_base(self, *args, **kwargs):
self.add_line = add_line_no_object_base.__get__(self)
result = add_directive_header(self, *args, **kwargs)
del self.add_line
return result
ClassDocumenter.add_directive_header = add_directive_header_no_object_base