J. Chopard
J. Chopard

Reputation: 21

How to make sphinx.ext.viewcode work while not using automodule

Bonjour,

I have a python module 'example_doc' with 'MyObj' in it that I'm trying to document using sphinx. When I use 'autodoc' and the autoclass directive:

.. autoclass:: testdoc.example_doc.MyObj

It generates the correct page with object description and since I'm using the 'viewcode' sphinx extension, I get a link to the source code on the side.

However when I try to reproduce the result by using the class directive directly:

.. class:: testdoc.example_doc.MyObj

I don't get the link to the source code.

edit: I'm using the latest build of sphinx for conda, i.e. sphinx 1.3.5 and I just have 'sphinx.ext.autodoc' and 'sphinx.ext.viewcode' as extensions in my conf.py.

Code used:

Source link
===========

autodoc below

.. autoclass:: testdoc.example_doc.MyObj

class below

.. class:: testdoc.example_doc.MyObj

Result:

sphinx doc html for viewcode test

Upvotes: 2

Views: 2653

Answers (1)

RoadrunnerWMC
RoadrunnerWMC

Reputation: 693

I had the same problem. After doing a bit of debugging within the viewcode extension to figure out what was different between using autodoc and declaring things manually, I discovered that you can make it work by declaring the module separately, before all of the classes.

That is, instead of doing this:

Page title
==========

Introductory text goes here

.. class:: testdoc.example_doc.MyObj

    Bla bla bla, example documentation for the first class

.. class:: testdoc.example_doc.MySecondObj

    Bla bla bla, example documentation for the second class

Do this:

Page title
==========

Introductory text goes here

.. module:: testdoc.example_doc

.. class:: MyObj

    Bla bla bla, example documentation for the first class

.. class:: MySecondObj

    Bla bla bla, example documentation for the second class

I'm not sure if this is a bug or intended behavior, but either way, explicitly declaring the module made the [source] links appear for me.

Upvotes: 1

Related Questions