Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sphinx - autodata shows str.__doc__

I'm trying to document my python code with Sphinx, but I found a problem documenting some data instantiated with exec; I have a table with names and values that I need to instantiate.

So in my code I wrote something like:

my_vars = [{'name': 'var1', 'value': 'first'},
           {'name': 'var2', 'value': 'second'}]

for var in my_vars:
    exec("{var[name]} = '{var[value]}'".format(var=var))

The problem is with Sphinx: since I'd like to maintain just the source code I used autodata, the corrisponding lines from my .rst file are:

.. autodata:: mymodule.var1

.. autodata:: mymodule.var2

that when built gave me this:

mymodule.var1 = 'first'
    str(string[, encoding[, errors]]) -> str

    Create a new string object from the given encoded string.
    encoding defaults to the current default string encoding.
    errors can be ‘strict’, ‘replace’ or ‘ignore’ and defaults to ‘strict’.

mymodule.var2 = 'second'
    str(string[, encoding[, errors]]) -> str

    Create a new string object from the given encoded string.
    encoding defaults to the current default string encoding.
    errors can be ‘strict’, ‘replace’ or ‘ignore’ and defaults to ‘strict’.

I think autodata goes looking into var1.__doc__ for a doc string and there found str.__doc__ (that is the message shown before).

I really don't know what to do and I'm searching for a way of not showing that ugly doc string (but still maintaining mymodule.var1 = 'first').

Or maybe even better a way to show my own doc, like: var1 is this. (but I wouldn't know where to put it).

like image 486
Rik Poggi Avatar asked Jan 05 '12 23:01

Rik Poggi


People also ask

How does Sphinx Autodoc work?

autodoc provides several directives that are versions of the usual py:module , py:class and so forth. On parsing time, they import the corresponding module and extract the docstring of the given objects, inserting them into the page source under a suitable py:module , py:class etc. directive.

What is Sphinx-Apidoc?

sphinx-apidoc is a tool for automatic generation of Sphinx sources that, using the autodoc extension, document a whole package in the style of other automatic API documentation tools. MODULE_PATH is the path to a Python package to document, and OUTPUT_PATH is the directory where the generated sources are placed.


2 Answers

My suggestion is this: document the variables in the module docstring instead of trying to get something usable from autodata.

mymodule.py:

""" 
This module is...

Module variables:

* var1: var1 doc
* var2: var2 doc
"""

my_vars = [{'name': 'var1', 'value': 'first'},
           {'name': 'var2', 'value': 'second'}]

for var in my_vars:
    exec("{var[name]} = '{var[value]}'".format(var=var))

...
... 

You could also use info fields:

"""

:var var1: var1 doc
:var var2: var2 doc
"""

This works, sort of, but the output is not as nicely formatted as info fields used to document class variables or function parameters.


Update: following up on comments about str subclassing. Does this work for you?

from collections import UserString   

my_vars = [{'name': 'var1', 'value': 'first', "doc": "var1 docstring"},
           {'name': 'var2', 'value': 'second', "doc": "var2 docstring"}]

for var in my_vars:
    code = """\
{0} = UserString('{1}')
{0}.__doc__ = '{2}'""".format(var["name"], var["value"], var["doc"])
    exec(code)
like image 79
mzjn Avatar answered Oct 12 '22 13:10

mzjn


I realized how can you fix it. Write smth like this:

x = 55
"""
x is varibble lala
"""

Use automodule directive and Sphinx will make docs for you.

like image 33
Stan Avatar answered Oct 12 '22 13:10

Stan