Manually set the module for an autodoc function

14 views
Skip to first unread message

Matt Dodge

unread,
May 15, 2019, 11:27:08 AM5/15/19
to sphinx-users
I posted this here on StackOverflow as well, thinking it might get some more meaningful eyeballs here though. I am looking to autodoc some functions in a Python module using `autofunction`. Let's say, for example, the functions are defined in `mylib/funcs/a.py` and that file looks something like this:
def myfunc(arg):
   
""" My function does something """
   
return

My intent is to have users call that function from the root of the library rather than at that file/module path. In other words mylib.myfunc rather than mylib.funcs.a.myfunc

However, for reasons outside of the scope of this question that I'd rather not get in to, when you do import mylib; mylib.myfunc or do from mylib import myfunc it doesn't point to the actual function where the docstring is. In other words, I cannot reference the actual myfunc in the mylib/__init__.py file.

I can get the function to auto doc like so:
..currentmodule:: mylib.funcs.a
..autofunction:: myfunc

However, the resulting documentation has a signature that looks like mylib.funcs.a.myfunc. I would like it to just be mylib.myfunc.

If I wasn't using autodoc I could do something like this in my sphinx docs:
.. function:: myfunc(arg)
   
:module: mylib

So I suppose my question is this: Is there a way to "alias" or change the label of a module when using autodoc on a function? I have looked at the preprocessing events for processing a docstring and the signature but those don't seem to allow you to change how the module is displayed.

I have also tried setting __module__ in the a.py file and in a few other places, based on the source code and the get_real_modname method in autodoc, but it doesn't seem to have any effect.
Reply all
Reply to author
Forward
0 new messages