summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2011-01-08 10:07:42 +0100
committerGeorg Brandl <georg@python.org>2011-01-08 10:07:42 +0100
commit53e6c23f52e9c0f76a4eb04d883a051f5e2dd0e0 (patch)
treec7b9a5945f8dcec6ed7df9c938e97f3f5c22297e
parentf6c9add394b8a6c91215fbd9b5d73752a109ae0a (diff)
downloadsphinx-53e6c23f52e9c0f76a4eb04d883a051f5e2dd0e0.tar.gz
#478: Added :rst:dir:`py:decorator` directive to describe decorators.
-rw-r--r--CHANGES2
-rw-r--r--doc/domains.rst39
-rw-r--r--sphinx/domains/python.py54
3 files changed, 85 insertions, 10 deletions
diff --git a/CHANGES b/CHANGES
index 9d7d8473..26c63f8b 100644
--- a/CHANGES
+++ b/CHANGES
@@ -70,6 +70,8 @@ Release 1.1 (in development)
* #259: HTML table rows now have even/odd CSS classes to enable
"Zebra styling".
+* #478: Added :rst:dir:`py:decorator` directive to describe decorators.
+
* #367: Added automatic exclusion of hidden members in inheritance
diagrams, and an option to selectively enable it.
diff --git a/doc/domains.rst b/doc/domains.rst
index 6e6a9b26..4cd77b9e 100644
--- a/doc/domains.rst
+++ b/doc/domains.rst
@@ -231,6 +231,45 @@ The following directives are provided for module and class contents:
.. versionadded:: 0.6
+.. rst:directive:: .. 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
+
+ .. code-block:: python
+
+ 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 :rst:role:`py:func` role.
+
+.. rst:directive:: .. py:decoratormethod:: name
+ .. py:decoratormethod:: name(signature)
+
+ Same as :rst:dir:`py:decorator`, but for decorators that are methods.
+
+ Refer to a decorator method using the :rst:role:`py:meth` role.
+
.. _signatures:
diff --git a/sphinx/domains/python.py b/sphinx/domains/python.py
index aa3375df..0dbd883c 100644
--- a/sphinx/domains/python.py
+++ b/sphinx/domains/python.py
@@ -357,6 +357,38 @@ class PyClassmember(PyObject):
self.clsname_set = True
+class PyDecoratorMixin(object):
+ """
+ Mixin for decorator directives.
+ """
+ def handle_signature(self, sig, signode):
+ ret = super(PyDecoratorMixin, self).handle_signature(sig, signode)
+ signode.insert(0, addnodes.desc_addname('@', '@'))
+ return ret
+
+ def needs_arglist(self):
+ return False
+
+
+class PyDecoratorFunction(PyDecoratorMixin, PyModulelevel):
+ """
+ Directive to mark functions meant to be used as decorators.
+ """
+ def run(self):
+ # a decorator function is a function after all
+ self.name = 'py:function'
+ return PyModulelevel.run(self)
+
+
+class PyDecoratorMethod(PyDecoratorMixin, PyClassmember):
+ """
+ Directive to mark methods meant to be used as decorators.
+ """
+ def run(self):
+ self.name = 'py:method'
+ return PyClassmember.run(self)
+
+
class PyModule(Directive):
"""
Directive to mark description of a new module.
@@ -534,16 +566,18 @@ class PythonDomain(Domain):
}
directives = {
- 'function': PyModulelevel,
- 'data': PyModulelevel,
- 'class': PyClasslike,
- 'exception': PyClasslike,
- 'method': PyClassmember,
- 'classmethod': PyClassmember,
- 'staticmethod': PyClassmember,
- 'attribute': PyClassmember,
- 'module': PyModule,
- 'currentmodule': PyCurrentModule,
+ 'function': PyModulelevel,
+ 'data': PyModulelevel,
+ 'class': PyClasslike,
+ 'exception': PyClasslike,
+ 'method': PyClassmember,
+ 'classmethod': PyClassmember,
+ 'staticmethod': PyClassmember,
+ 'attribute': PyClassmember,
+ 'module': PyModule,
+ 'currentmodule': PyCurrentModule,
+ 'decorator': PyDecoratorFunction,
+ 'decoratormethod': PyDecoratorMethod,
}
roles = {
'data': PyXRefRole(),