How can I create documentation for the Python property definition tool using Sphinx?

I have a Python class something like this: docstrings, which should be converted to Sphinx documentation:

class Direction(object): """ A direction in which movement can be made. """ def __init__(self): self._name = None @property def name(self): """ The unique name of the direction. :return: The direction name :rtype: string """ return self._name @name.setter def name(self, value): """ Sets the direction name. :param string value: The direction name """ self._name = value 

Sphinx output looks something like this:

class Direction (name) Direction in which movement can be performed.

Name The unique name of the destination.

Returns: Direction Name

Return Type: String

which is fine as possible, but pay attention to the complete lack of information about the name installer.

Is there a way to get Sphinx to generate documentation for a property setting tool?

+8
python properties python-sphinx
source share
1 answer

Sphinx ignores docstrings in the property settings, so all documentation for the property should be in the @property method.

While Sphinx understands certain specific tags (for example :param ...: , it accepts any custom tags and displays them as labels for the text that follows them.

Therefore, something like the following will make the documentation in a reasonable way ( getter , setter and type can be changed to any other text if necessary).

 @property def name(self): """ The unique name of the direction. :getter: Returns this direction name :setter: Sets this direction name :type: string """ return self._name 

The generated documentation looks something like this:

class Direction (name) Direction in which movement can be performed.

Name The unique name of the destination.

Getter: Returns the name of this direction.

Setter: Sets the name of this direction.

Type: string

Thanks to @BrenBarm and @ABB for pointing me in the direction of this solution.

+10
source share

All Articles