1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Spinx autodoc - Skipping members with automethod set

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024 às 22:52.

  1. Stack

    Stack Membro Participativo

    I'm using sphinx and a few of the files have the directive .. automethod:: set. I need a way to have sphinx show all __init__ methods, without removing the .. automethod:: directive and without any warnings. I tried to look through stack, but didn't see anything like this. Here's a MWE:

    class A():
    ```
    documentation

    .. automethod:: __init__
    ```
    def __init__(self):
    ```
    documentation for A
    ```

    class B:
    ```
    documentation

    ```
    def __init__(self):
    ```
    documentation for B
    ```


    def skip_member(app, what, name, obj, skip, options):
    // What do I put here to skip the __init__ of class A, but *not* the __init__ of class B?
    if name == '__init__':
    return False
    return skip

    def setup(app):
    app.connect('autodoc-skip-member', skip_member)


    With this I get the following error:

    /home/xxx/sphinx-test/classFile.py:docstring of classFile.A.__init__:1: WARNING: duplicate object description of classFile.A.__init__, other instance in usage, use :no-index: for one of them


    I want to be able to get rid of this error message without removing the .. automethod:: directive. Is this even possible? An alternative that is also acceptable is to keep skip_member as is, but to alter MethodDocumenter (or something else?) to ignore the automethod directive if the name is __init__, but not sure the best way to do that.


    Why am I trying to do this? I'm working on a collaborative project so I can't remove the automethod directives, but I want an automated way to show all documentation including private/hidden methods. (So we would have 2 documentations: 1 for consumers and one for developers in essence) Removing the automethod directive would mean that the consumer version would now have more info than needed, so I don't want to remove those, but by not removing those, when I try and run the sphinx documentation, I'm getting a bunch of duplicate methods and so I need some way to suppress either the ones being included from automethod or from the skip_method way.

    Edit: Something that maybe wasn't clear in the above. I could technically use :no-index: to have the error not appear, but then the method gets duplicated (shows up twice in the docs).

    .. automethod:: __init__
    :no-index:


    When I try and look at the options variable in skip_member, I get a dictionary, but the only option showing up is members. In addition, the what is coming back as class which seems weird? I would have assumed it would come back as method since we're doing automethod?

    Continue reading...

Compartilhe esta Página