Search code examples
pythonpython-sphinxrestructuredtext

unset or turn off current module for some attributes


I am writing some documentation, where I list the attributes that a duck-typed object must have to fulfill a particular interface. I tried doing something like this:

List of required attributes:

.. py:attribute:: x

   The x-coordinate, as a :py:class:`float`.

.. py:attribute:: y

   The y-coordinate, as a :py:class:`float`.

I am expecting to get a list of attributes with descriptions, but instead of plain names, the documentation ends up with names that include the prior .. py:currentmodule directive. So instead of just x, I end up with my.current.module.x.

Is there any way to do either of the following:

  1. Turn off .. py:currentmodule::. I attempted to set it to the empty string as well as ., but that just generated a warning and kept using the prior value.
  2. Configure generated attribute names not to include the module in the name (preferably in the link as well).

I would prefer option #2 because I actually have links to objects in the current module in the documentation, so it would be nice to make it clear that these attributes are not in the module without actually changing the current module.

At the moment, my workaround is to temporarily set .. py:currentmodule:: duck, and reinstate after that segment.

I have also tried to write the attribute docs as .. py:attribute:: ~x, .. py:attribute:: !x and .. py:attribute:: .x. All cases include the special character in the name, and the first two fail to generate anchors to the attributes.


Solution

  • You can set currentmodule to None before those objects:

    .. currentmodule:: None
    

    And, if later you have other objects for which you do want the current module to be prefixed, you can re-set the module as well:

    .. currentmodule:: your.module