Sphinx Domains

New in version 1.0.

What is a Domain?

Originally, Sphinx was conceived for a single project, the documentation of the Python language. Shortly afterwards, it was made available for everyone as a documentation tool, but the documentation of Python modules remained deeply built in – the most fundamental directives, like function, were designed for Python objects. Since Sphinx has become somewhat popular, interest developed in using it for many different purposes: C/C++ projects, JavaScript, or even reStructuredText markup (like in this documentation).


A domain is a collection of markup (reStructuredText directives and roles) to describe and link to objects belonging together, e.g. elements of a programming language. Directive and role names in a domain have names like domain:name, e.g. py:function. Domains can also provide custom indices (like the Python Module Index).


This section describes what the domains that come with Sphinx provide. The domain API is documented as well, in the section Domain API.

Basic Markup

Most domains provide a number of object description directives, used to describe specific objects provided by modules. Each directive requires one or more signatures to provide basic information about what is being described, and the content should be the description. The basic version makes entries in the general index; if no index entry is desired, you can give the directive option flag :noindex:. An example using a Python domain directive:

.. py:function:: spam(eggs)

   Spam or ham the foo.







Default Domain


.. default-domain:: name




.. function:: pyfunc()

   Describes a Python function.

Reference to :func:`pyfunc`.

Cross-referencing syntax

For cross-reference roles provided by domains, the same facilities exist as for general cross-references. See 交叉索引的语法.

In short:


The Python Domain


.. py:module:: name






.. py:currentmodule:: name

This directive tells Sphinx that the classes, functions etc. documented from here are in the given module (like py:module), but it will not create index entries, an entry in the Global Module Index, or a link target for py:mod. This is helpful in situations where documentation for things in a module is spread over multiple files or sections – one location has the py:module directive, the others only py:currentmodule.

The following directives are provided for module and class contents:

.. py:data:: name


.. py:exception:: name


.. py:function:: name(signature)

Describes a module-level function. The signature should include the parameters, enclosing optional parameters in brackets. Default values can be given if it enhances clarity; see Python Signatures. For example:

.. py:function:: Timer.repeat([repeat=3[, number=1000000]])



.. py:class:: name[(signature)]

Describes a class. The signature can include parentheses with parameters which will be shown as the constructor arguments. See also Python Signatures.

Methods and attributes belonging to the class should be placed in this directive’s body. If they are placed outside, the supplied name should contain the class name so that cross-references still work. Example:

.. py:class:: Foo
   .. py:method:: quux()

-- or --

.. py:class:: Bar

.. py:method:: Bar.quux()

The first way is the preferred one.

.. py:attribute:: name


.. py:method:: name(signature)

Describes an object method. The parameters should not include the self parameter. The description should include similar information to that described for function. See also Python Signatures.

.. py:staticmethod:: name(signature)

Like py:method, but indicates that the method is a static method.

New in version 0.4.

.. py:classmethod:: name(signature)

Like py:method, but indicates that the method is a class method.

New in version 0.6.

.. py:decorator:: name
.. py:decorator:: name(signature)

Describes a decorator function. The signature should not represent the signature of the actual function, but the usage as a decorator. For example, given the functions

def removename(func):
    func.__name__ = ''
    return func

def setnewname(name):
    def decorator(func):
        func.__name__ = name
        return func
    return decorator

the descriptions should look like this:

.. py:decorator:: removename

   Remove name of the decorated function.

.. py:decorator:: setnewname(name)

   Set name of the decorated function to *name*.

There is no py:deco role to link to a decorator that is marked up with this directive; rather, use the py:func role.

.. py:decoratormethod:: name
.. py:decoratormethod:: name(signature)

Same as py:decorator, but for decorators that are methods.

Refer to a decorator method using the py:meth role.

Python Signatures



It is customary to put the opening bracket before the comma. In addition to this “nested” bracket style, a “flat” style can also be used, due to the fact that most optional parameters can be given independently:




Info field lists

New in version 0.4.


  • type: Type of a parameter.
  • var, ivar, cvar: Description of a variable.
  • returns, return: Description of the return value.
  • rtype: Return type.



   Format the exception with a traceback.

   :param etype: exception type
   :param value: exception value
   :param tb: traceback object
   :param limit: maximum number of stack frames to show
   :type limit: integer or None
   :rtype: list of strings

This will render like this:


Format the exception with a traceback.

  • etype – exception type
  • value – exception value
  • tb – traceback object
  • limit (integer or None) – maximum number of stack frames to show
Return type:

list of strings


:param integer limit: maximum number of stack frames to show

Cross-referencing Python objects







Reference a module-level variable.




Reference a class; a dotted name may be used.




Reference a data attribute of an object.


Reference an exception. A dotted name may be used.



New in version 0.4.


Normally, names in these roles are searched first without any further qualification, then with the current module name prepended, then with the current module and class name (if any) prepended. If you prefix the name with a dot, this order is reversed. For example, in the documentation of Python’s codecs module, :py:func:`open` always refers to the built-in function, while :py:func:`.open` refers to codecs.open().




The C Domain


.. c:function:: type name(signature)





.. c:member:: type name

Describes a C struct member. Example signature:

.. c:member:: PyObject* PyTypeObject.tp_bases


.. c:macro:: name


.. c:type:: name


.. c:var:: type name


.. c:var:: PyObject* PyClass_Type

Cross-referencing C constructs



Reference a C-language variable.


Reference a C-language function. Should include trailing parentheses.


Reference a “simple” C macro, as defined above.


Reference a C-language type.

The C++ Domain


The following directives are available:

.. cpp:class:: signatures
.. cpp:function:: signatures
.. cpp:member:: signatures
.. cpp:type:: signatures

Describe a C++ object. Full signature specification is supported – give the signature as you would in the declaration. Here some examples:


   Describes a method with parameters and types.

.. cpp:function:: bool namespaced::theclass::method(arg1, arg2)

   Describes a method without types.

.. cpp:function:: const T &array<T>::operator[]() const

   Describes the constant indexing operator of a templated array.

.. cpp:function:: operator bool() const

   Describe a casting operator here.


   Describe a constexpr function here.



.. cpp:type:: theclass::const_iterator

Will be rendered like this:


Describes a method with parameters and types.


Describes a method without types.


Describes the constant indexing operator of a templated array.

operator bool() const

Describe a casting operator here.


Describe a constexpr function here.

std::string theclass::name
type theclass::const_iterator
.. cpp:namespace:: namespace

Select the current C++ namespace for the following objects.

These roles link to the given object types:


Reference a C++ object. You can give the full signature (and need to, for overloaded functions.)


Sphinx’ syntax to give references a custom title can interfere with linking to template classes, if nothing follows the closing angle bracket, i.e. if the link looks like this: :cpp:class:`MyClass<T>`. This is interpreted as a link to T with a title of MyClass. In this case, please escape the opening angle bracket with a backslash, like this: :cpp:class:`MyClass\<T>`.

Note on References

It is currently impossible to link to a specific version of an overloaded method. Currently the C++ domain is the first domain that has basic support for overloaded methods and until there is more data for comparison we don’t want to select a bad syntax to reference a specific overload. Currently Sphinx will link to the first overloaded version of the method / function.

The Standard Domain

The so-called “standard” domain collects all markup that doesn’t warrant a domain of its own. Its directives and roles are not prefixed with a domain name.

The standard domain is also where custom object descriptions, added using the add_object_type() API, are placed.




.. option:: -m <module>, --module <module>

   Run a module as a script.

The directive will create a cross-reference target named after the first option, referencable by option (in the example case, you’d use something like :option:`-m`).

.. envvar:: name

Describes an environment variable that the documented code or program uses or defines. Referencable by envvar.

.. program:: name

Like py:currentmodule, this directive produces no output. Instead, it serves to notify Sphinx that all following option directives document options for the program called name.

If you use program, you have to qualify the references in your option roles by the program name, so if you have the following situation

.. program:: rm

.. option:: -r

   Work recursively.

.. program:: svn

.. option:: -r revision

   Specify the revision to work upon.

then :option:`rm -r` would refer to the first option, while :option:`svn -r` would refer to the second one.

The program name may contain spaces (in case you want to document subcommands like svn add and svn commit separately).

New in version 0.5.


.. describe:: text
.. object:: text


.. describe:: PAPER

   You can set this variable to select a paper size.

The JavaScript Domain

The JavaScript domain (name js) provides the following directives:

.. js:function:: name(signature)

Describes a JavaScript function or method. If you want to describe arguments as optional use square brackets as documented for Python signatures.



   :param callback: Get's called with the object.
   :param errback:
       text so we need multiple lines
   :throws SomeError: For whatever reason in that case.
   :returns: Something

This is rendered as:

$.getJSON(href, callback[, errback])
  • href (string) – An URI to the location of the resource.
  • callback – Get’s called with the object.
  • errback – Get’s called in case the request fails. And a lot of other text so we need multiple lines.

SomeError – For whatever reason in that case.



.. js:class:: name

Describes a constructor that creates an object. This is basically like a function but will show up with a class prefix:


   :param string name: The name of the animal
   :param number age: an optional age for the animal

This is rendered as:

class MyAnimal(name[, age])
  • name (string) – The name of the animal
  • age (number) – an optional age for the animal
.. js:data:: name

Describes a global variable or constant.

.. js:attribute:: object.name

Describes the attribute name of object.

These roles are provided to refer to the described objects:


The reStructuredText domain

The reStructuredText domain (name rst) provides the following directives:

.. rst:directive:: name

Describes a reST directive. The name can be a single directive name or actual directive syntax (.. prefix and :: suffix) with arguments that will be rendered differently. For example:

.. rst:directive:: foo

   Foo description.

.. rst:directive:: .. bar:: baz

   Bar description.

will be rendered as:

.. foo::

Foo description.

.. bar:: baz

Bar description.

.. rst:role:: name

Describes a reST role. For example:

.. rst:role:: foo

   Foo description.

will be rendered as:


Foo description.

These roles are provided to refer to the described objects:


More domains
