3

I'm currently using Sphinx (first time using it) to build the docs for my modules and I have some classes which have some class variables that are initialized with a default value.

For ex:

class ConfigSettings(object):
    """Class that manages a config file
    """    
    #: Contains the path to the config file (root dir of module)
    path = Path(util.getAbsCurrentPath('configfile.ini'))

When building the docs the variable is evaluated and it prints the full path to the file which I don't want (for security reasons). Is there a way to not display the variable value but maybe only the annotation using Sphinx?

I tried various combinations of .. autoclass: and .. autodata: but none of them worked so far...

This is what I currently have in my build file:

Config module
----------------------------

.. automodule:: lib.config
   :members:
   :undoc-members:
   :show-inheritance:
bad_coder
  • 11,289
  • 20
  • 44
  • 72
DarkImage
  • 63
  • 1
  • 8
  • `:noindex:` option in the second `.. autoclass:` was used only to prevent Sphinx from issuing a warning from documenting the YourClass twice with `.. autoclass:`, because that would cause a duplicate entry in the general index. – bad_coder May 09 '20 at 22:16

2 Answers2

2

You can workaround the problem by not evaluating the path at import time. I think the best way to do that is using a classproperty.

For example:

class ConfigSettings(object):
    @classproperty
    def path(cls):
        return Path(util.getAbsCurrentPath('configfile.ini'))
shx2
  • 61,779
  • 13
  • 130
  • 153
2

The easiest way with Sphinx directives is using annotation or excluding the member.

Unless there is a strict need to stop variables from self-initializing on module import, it's incorrect to change your Python source because of the way you want to present it. If your Python source code is correct, then adapt your .rst file to customize presentation.

your_module.py

from pathlib import Path


class YourClass:

    #: This comment is documented with the member.
    path = Path('your_path', 'configfile.ini')

your_module.rst (shows 2 possible ways).

your_module
===========

.. automodule:: your_module
    :exclude-members: YourClass

    .. autoclass:: YourClass
        :exclude-members: path

        In this example you use an annotation while excluding from autoclass.

        .. autoattribute:: path
            :annotation: ='write your path here'

    .. autoclass:: YourClass
        :noindex:
        :exclude-members: path

        In this example you simply exclude from autoclass.

The result:

enter image description here

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • 2
    this is indeed what i was looking for thank you !! with my tries i got very close to your solution but i was keeping getting warning for duplicates references simply because i was not excluding the class from the module!! – DarkImage May 09 '20 at 22:37