Add PyMethod class; a directive for python method description

This commit is contained in:
Takeshi KOMIYA
2019-04-07 21:09:57 +09:00
parent 70ed093b8b
commit dd6c79476f
2 changed files with 45 additions and 1 deletions

View File

@@ -523,6 +523,29 @@ class PyClassmember(PyObject):
return ''
class PyMethod(PyObject):
"""Description of a method."""
def needs_arglist(self):
# type: () -> bool
return True
def get_index_text(self, modname, name_cls):
# type: (str, Tuple[str, str]) -> str
name, cls = name_cls
try:
clsname, methname = name.rsplit('.', 1)
if modname and self.env.config.add_module_names:
clsname = '.'.join([modname, clsname])
except ValueError:
if modname:
return _('%s() (in module %s)') % (name, modname)
else:
return '%s()' % name
return _('%s() (%s method)') % (methname, clsname)
class PyDecoratorMixin:
"""
Mixin for decorator directives.
@@ -745,7 +768,7 @@ class PythonDomain(Domain):
'data': PyModulelevel,
'class': PyClasslike,
'exception': PyClasslike,
'method': PyClassmember,
'method': PyMethod,
'classmethod': PyClassmember,
'staticmethod': PyClassmember,
'attribute': PyClassmember,

View File

@@ -290,3 +290,24 @@ def test_pyobject_prefix(app):
desc)])]))
assert doctree[1][1][1].astext().strip() == 'say' # prefix is stripped
assert doctree[1][1][3].astext().strip() == 'FooBar.say' # not stripped
def test_pymethod(app):
text = (".. py:class:: Class\n"
"\n"
" .. py:method:: meth\n")
domain = app.env.get_domain('py')
doctree = restructuredtext.parse(app, text)
assert_node(doctree, (addnodes.index,
[desc, ([desc_signature, ([desc_annotation, "class "],
[desc_name, "Class"])],
[desc_content, (addnodes.index,
desc)])]))
assert_node(doctree[1][1][0], addnodes.index,
entries=[('single', 'meth() (Class method)', 'Class.meth', '', None)])
assert_node(doctree[1][1][1], ([desc_signature, ([desc_name, "meth"],
[desc_parameterlist, ()])],
[desc_content, ()]))
assert 'Class.meth' in domain.objects
assert domain.objects['Class.meth'] == ('index', 'method')