Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sphinx remove dataclass fields from autodoc

As this post describes, autodoc will eagerly add the class variables to the documentation even if napoleon adds the fields as documentation in:

from dataclasses import dataclass
@dataclass
class Foo():
    """Some class

    Attributes:
        a: foo
        b: bar
    """
    a: str
    b: int

    def c(self) -> int:
        """Here's a doc'd function
        """
        return 3

I want to explicitly tell autodoc to not document any class variables (which is also instance variables in the case of dataclasses) - I only want autodoc to show declared functions for a given class and let napoleon handle the class/instance variables for all classes it finds. Is this possible without :exclude-members: for every class (which is a huge hassle)?

I already tried:

autodoc_default_options = {
    'members':          True,
    'undoc-members':    False,
}

in my conf.py and this in the .rst file:

.. automodule:: some.module
    :members:
    :show-inheritance:

Which should hide undocumented members but they still show up:

enter image description here

like image 612
OneRaynyDay Avatar asked Jul 09 '26 06:07

OneRaynyDay


1 Answers

Removing :undoc-members: directive from the corresponding module did the job for me. This does not have the perfect granularity control but if you dataclasses are in the same module or if you don't care about not documenting other stuff without docstrings, this should do the job.

like image 127
canbooo Avatar answered Jul 10 '26 19:07

canbooo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!