Change name of method for documentation

16 views
Skip to first unread message

CaptFugu

unread,
Jun 4, 2020, 4:02:29 AM6/4/20
to sphinx-users
Suppose I have the following setup:

class ExternalPackageClass:
   
def __call__(self, *args, **kwargs):
       
return self.forward(*args, **kwargs)

   
def forward(self, *args, **kwargs):
       
raise NotImplementedError


class MyClass(ExternalPackageClass):
   
def forward(self, input):
        r
"""Prints the input"""
       
print(input)

Can I configure autodoc somehow to document the forward() method as __call__()? Otherwise I always need to include the information somewhere that forward() is invoked if MyClass is called.

Komiya Takeshi

unread,
Jun 4, 2020, 9:48:15 AM6/4/20
to sphinx...@googlegroups.com
Hi,

The autodoc extension scans the target module and generates
documentation. No way to imitate it. As a workaround, you can insert
your content manually:

.. autoclass:: yourmodule.MyClass

.. py:method:: __call__

Prints the input.
This text is added to the document.

Note: You should not use `:member:` option to the `autoclass` or
`automodule` directives.
Then they'll create a document for MyClass.forward() automatically.


Thanks,
Takeshi KOMIYA

2020年6月4日(木) 17:02 'CaptFugu' via sphinx-users <sphinx...@googlegroups.com>:
> --
> You received this message because you are subscribed to the Google Groups "sphinx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/9df0564f-f823-499c-a7e5-6d0efee594e9%40googlegroups.com.

CaptFugu

unread,
Jun 4, 2020, 10:31:03 AM6/4/20
to sphinx-users
Thats too bad, but thanks for the clarification.

Would this be possible if I wrote a custom extension for it? It should do the following:

  1. Detect if the docstring belongs to a class that inherits from ExternalPackageClass. If yes, proceed with step 2. Otherwise continue normally
  2. Check if it is the docstring of the __call__ method. If yes, copy the docstring from forward and use it.

> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx...@googlegroups.com.

Komiya Takeshi

unread,
Jun 4, 2020, 11:49:34 AM6/4/20
to sphinx...@googlegroups.com
Yes, you can do it. But there is no simple hook to do that. So you
need to understand how autodoc works. (And AFAIK there are no good
examples and documentations to extend autodoc...)

2020年6月4日(木) 23:31 'CaptFugu' via sphinx-users <sphinx...@googlegroups.com>:
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/64fe6193-a268-42ef-8c28-7f023eebf1ab%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages