Closes #1623: Return types specified with `:rtype:` are now turned into links if possible.

This commit is contained in:
Georg Brandl committed 2014-11-12 11:32:21 +01:00
1 parent 09d2c399d3
commit 6c194ad56f
6 files changed
+49 -24

No files matched your search

+2
View File
@@ -123,6 +123,8 @@ Features added
to Jeppe Pihl. to Jeppe Pihl.
* PR#299: add various options to sphinx-quickstart. Quiet mode option * PR#299: add various options to sphinx-quickstart. Quiet mode option
``--quiet`` will skips wizard mode. Thanks to WAKAYAMA shirou. ``--quiet`` will skips wizard mode. Thanks to WAKAYAMA shirou.
* #1623: Return types specified with ``:rtype:`` are now turned into links if
possible.
Bugs fixed Bugs fixed
---------- ----------
+2 -2
View File
@@ -310,12 +310,12 @@ are recognized and formatted nicely:
* ``param``, ``parameter``, ``arg``, ``argument``, ``key``, ``keyword``: * ``param``, ``parameter``, ``arg``, ``argument``, ``key``, ``keyword``:
Description of a parameter. Description of a parameter.
* ``type``: Type of a parameter. * ``type``: Type of a parameter. Creates a link if possible.
* ``raises``, ``raise``, ``except``, ``exception``: That (and when) a specific * ``raises``, ``raise``, ``except``, ``exception``: That (and when) a specific
exception is raised. exception is raised.
* ``var``, ``ivar``, ``cvar``: Description of a variable. * ``var``, ``ivar``, ``cvar``: Description of a variable.
* ``returns``, ``return``: Description of the return value. * ``returns``, ``return``: Description of the return value.
* ``rtype``: Return type. * ``rtype``: Return type. Creates a link if possible.
The field names must consist of one of these keywords and an argument (except The field names must consist of one of these keywords and an argument (except
for ``returns`` and ``rtype``, which do not need an argument). This is best for ``returns`` and ``rtype``, which do not need an argument). This is best
+24 -15
View File
@@ -84,13 +84,14 @@ def _pseudo_parse_arglist(signode, arglist):
# This override allows our inline type specifiers to behave like :class: link # This override allows our inline type specifiers to behave like :class: link
# when it comes to handling "." and "~" prefixes. # when it comes to handling "." and "~" prefixes.
class PyTypedField(TypedField): class PyXrefMixin(object):
def make_xref(self, rolename, domain, target, innernode=nodes.emphasis): def make_xref(self, rolename, domain, target, innernode=nodes.emphasis,
result = super(PyTypedField, self).make_xref(rolename, domain, target, contnode=None):
innernode) result = super(PyXrefMixin, self).make_xref(rolename, domain, target,
innernode, contnode)
result['refspecific'] = True
if target.startswith('.'): if target.startswith('.'):
result['reftarget'] = target[1:] result['reftarget'] = target[1:]
result['refspecific'] = True
result[0][0] = nodes.Text(target[1:]) result[0][0] = nodes.Text(target[1:])
if target.startswith('~'): if target.startswith('~'):
result['reftarget'] = target[1:] result['reftarget'] = target[1:]
@@ -99,6 +100,14 @@ class PyTypedField(TypedField):
return result return result
class PyField(PyXrefMixin, Field):
pass
class PyTypedField(PyXrefMixin, TypedField):
pass
class PyObject(ObjectDescription): class PyObject(ObjectDescription):
""" """
Description of a general Python object. Description of a general Python object.
@@ -111,21 +120,21 @@ class PyObject(ObjectDescription):
doc_field_types = [ doc_field_types = [
PyTypedField('parameter', label=l_('Parameters'), PyTypedField('parameter', label=l_('Parameters'),
names=('param', 'parameter', 'arg', 'argument', names=('param', 'parameter', 'arg', 'argument',
'keyword', 'kwarg', 'kwparam'), 'keyword', 'kwarg', 'kwparam'),
typerolename='obj', typenames=('paramtype', 'type'), typerolename='obj', typenames=('paramtype', 'type'),
can_collapse=True), can_collapse=True),
TypedField('variable', label=l_('Variables'), rolename='obj', PyTypedField('variable', label=l_('Variables'), rolename='obj',
names=('var', 'ivar', 'cvar'), names=('var', 'ivar', 'cvar'),
typerolename='obj', typenames=('vartype',), typerolename='obj', typenames=('vartype',),
can_collapse=True), can_collapse=True),
GroupedField('exceptions', label=l_('Raises'), rolename='exc', GroupedField('exceptions', label=l_('Raises'), rolename='exc',
names=('raises', 'raise', 'exception', 'except'), names=('raises', 'raise', 'exception', 'except'),
can_collapse=True), can_collapse=True),
Field('returnvalue', label=l_('Returns'), has_arg=False, Field('returnvalue', label=l_('Returns'), has_arg=False,
names=('returns', 'return')), names=('returns', 'return')),
Field('returntype', label=l_('Return type'), has_arg=False, PyField('returntype', label=l_('Return type'), has_arg=False,
names=('rtype',)), names=('rtype',), bodyrolename='obj'),
] ]
def get_signature_prefix(self, sig): def get_signature_prefix(self, sig):
+17 -6
View File
@@ -29,11 +29,13 @@ def _is_single_paragraph(node):
class Field(object): class Field(object):
""" """A doc field that is never grouped. It can have an argument or not, the
A doc field that is never grouped. It can have an argument or not, the
argument can be linked using a specified *rolename*. Field should be used argument can be linked using a specified *rolename*. Field should be used
for doc fields that usually don't occur more than once. for doc fields that usually don't occur more than once.
The body can be linked using a specified *bodyrolename* if the content is
just a single inline or text node.
Example:: Example::
:returns: description of the return value :returns: description of the return value
@@ -42,19 +44,22 @@ class Field(object):
is_grouped = False is_grouped = False
is_typed = False is_typed = False
def __init__(self, name, names=(), label=None, has_arg=True, rolename=None): def __init__(self, name, names=(), label=None, has_arg=True, rolename=None,
bodyrolename=None):
self.name = name self.name = name
self.names = names self.names = names
self.label = label self.label = label
self.has_arg = has_arg self.has_arg = has_arg
self.rolename = rolename self.rolename = rolename
self.bodyrolename = bodyrolename
def make_xref(self, rolename, domain, target, innernode=nodes.emphasis): def make_xref(self, rolename, domain, target, innernode=nodes.emphasis,
contnode=None):
if not rolename: if not rolename:
return innernode(target, target) return contnode or innernode(target, target)
refnode = addnodes.pending_xref('', refdomain=domain, refexplicit=False, refnode = addnodes.pending_xref('', refdomain=domain, refexplicit=False,
reftype=rolename, reftarget=target) reftype=rolename, reftarget=target)
refnode += innernode(target, target) refnode += contnode or innernode(target, target)
return refnode return refnode
def make_entry(self, fieldarg, content): def make_entry(self, fieldarg, content):
@@ -67,6 +72,12 @@ class Field(object):
fieldname += nodes.Text(' ') fieldname += nodes.Text(' ')
fieldname += self.make_xref(self.rolename, domain, fieldname += self.make_xref(self.rolename, domain,
fieldarg, nodes.Text) fieldarg, nodes.Text)
if len(content) == 1 and (
isinstance(content[0], nodes.Text) or
(isinstance(content[0], nodes.inline) and len(content[0]) == 1
and isinstance(content[0][0], nodes.Text))):
content = [self.make_xref(self.bodyrolename, domain,
content[0].astext(), contnode=content[0])]
fieldbody = nodes.field_body('', nodes.paragraph('', '', *content)) fieldbody = nodes.field_body('', nodes.paragraph('', '', *content))
return nodes.field('', fieldname, fieldbody) return nodes.field('', fieldname, fieldbody)
+1 -1
View File
@@ -92,7 +92,7 @@ Referring to :func:`nothing <>`.
:type isdst: * some complex :type isdst: * some complex
* expression * expression
:returns: a new :class:`Time` instance :returns: a new :class:`Time` instance
:rtype: :class:`Time` :rtype: Time
:raises ValueError: if the values are out of range :raises ValueError: if the values are out of range
:ivar int hour: like *hour* :ivar int hour: like *hour*
:ivar minute: like *minute* :ivar minute: like *minute*
+3
View File
@@ -200,6 +200,9 @@ HTML_XPATH = {
(".//dl[@class='userdesc']", ''), (".//dl[@class='userdesc']", ''),
(".//dt[@id='userdesc-myobj']", ''), (".//dt[@id='userdesc-myobj']", ''),
(".//a[@href='#userdesc-myobj'][@class='reference internal']", ''), (".//a[@href='#userdesc-myobj'][@class='reference internal']", ''),
# docfields
(".//a[@class='reference internal'][@href='#TimeInt']/em", 'TimeInt'),
(".//a[@class='reference internal'][@href='#Time']", 'Time'),
# C references # C references
(".//span[@class='pre']", 'CFunction()'), (".//span[@class='pre']", 'CFunction()'),
(".//a[@href='#c.Sphinx_DoSomething']", ''), (".//a[@href='#c.Sphinx_DoSomething']", ''),