Properties can have types documented with :type:

This commit is contained in:
Ashley Whetter 2017-09-06 15:29:41 -07:00
parent cb0220de1a
commit 8797ff51b3
3 changed files with 46 additions and 13 deletions

View File

@ -553,7 +553,10 @@ class GoogleDocstring(UnicodeMixin):
def _parse_attribute_docstring(self):
# type: () -> List[unicode]
_type, _desc = self._consume_inline_attribute()
return self._format_field('', _type, _desc)
lines = self._format_field('', '', _desc)
if _type:
lines.extend(['', ':type: %s' % _type])
return lines
def _parse_attributes_section(self, section):
# type: (unicode) -> List[unicode]
@ -566,8 +569,11 @@ class GoogleDocstring(UnicodeMixin):
lines.append(':vartype %s: %s' % (_name, _type))
else:
lines.extend(['.. attribute:: ' + _name, ''])
fields = self._format_field('', _type, _desc)
fields = self._format_field('', '', _desc)
lines.extend(self._indent(fields, 3))
if _type:
lines.append('')
lines.extend(self._indent([':type: %s' % _type], 3))
lines.append('')
if self._config.napoleon_use_ivar:
lines.append('')

View File

@ -288,6 +288,12 @@ class DocFieldTransformer(object):
fieldtype, fieldarg = fieldname.astext(), ''
typedesc, is_typefield = typemap.get(fieldtype, (None, None))
# collect the content, trying not to keep unnecessary paragraphs
if _is_single_paragraph(fieldbody):
content = fieldbody.children[0].children
else:
content = fieldbody.children
# sort out unknown fields
if typedesc is None or typedesc.has_arg != bool(fieldarg):
# either the field name is unknown, or the argument doesn't
@ -297,16 +303,27 @@ class DocFieldTransformer(object):
new_fieldname += ' ' + fieldarg
fieldname[0] = nodes.Text(new_fieldname)
entries.append(field)
# but if this has a type then we can at least link it
if typedesc and is_typefield and content:
target = content[0].astext()
xrefs = typedesc.make_xrefs(
typedesc.typerolename,
self.directive.domain,
target,
contnode=content[0],
)
if _is_single_paragraph(fieldbody):
fieldbody.children[0].clear()
fieldbody.children[0].extend(xrefs)
else:
fieldbody.clear()
fieldbody.extend(xrefs)
continue
typename = typedesc.name
# collect the content, trying not to keep unnecessary paragraphs
if _is_single_paragraph(fieldbody):
content = fieldbody.children[0].children
else:
content = fieldbody.children
# if the field specifies a type, put it in the types collection
if is_typefield:
# filter out only inline nodes; others will result in invalid

View File

@ -58,15 +58,21 @@ Sample namedtuple subclass
.. attribute:: attr1
*Arbitrary type* -- Quick description of attr1
Quick description of attr1
:type: Arbitrary type
.. attribute:: attr2
*Another arbitrary type* -- Quick description of attr2
Quick description of attr2
:type: Another arbitrary type
.. attribute:: attr3
*Type* -- Adds a newline after the type
Adds a newline after the type
:type: Type
"""
self.assertEqual(expected, actual)
@ -323,7 +329,9 @@ Attributes:
expected = """\
.. attribute:: in_attr
:class:`numpy.ndarray` -- super-dooper attribute
super-dooper attribute
:type: :class:`numpy.ndarray`
"""
self.assertEqual(expected, actual)
@ -336,7 +344,9 @@ Attributes:
expected = """\
.. attribute:: in_attr
*numpy.ndarray* -- super-dooper attribute
super-dooper attribute
:type: numpy.ndarray
"""
self.assertEqual(expected, actual)