Merge pull request #7395 from tk0miya/7219_indexentries_for_pyfunc

Fix #7219: py:function directive generates incorrect index entry
This commit is contained in:
Takeshi KOMIYA 2020-03-29 20:04:29 +09:00 committed by GitHub
commit 70c61e44c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 7 deletions

View File

@ -24,6 +24,8 @@ Bugs fixed
argument lists, and more comprehensive error messages in some cases. argument lists, and more comprehensive error messages in some cases.
* C, C++, fix crash and wrong duplicate warnings related to anon symbols. * C, C++, fix crash and wrong duplicate warnings related to anon symbols.
* #6477: Escape first "!" in a cross reference linking no longer possible * #6477: Escape first "!" in a cross reference linking no longer possible
* #7219: py domain: The index entry generated by ``py:function`` directive is
different with one from ``index`` directive with "builtin" type
Testing Testing
-------- --------

View File

@ -551,12 +551,23 @@ class PyFunction(PyObject):
def needs_arglist(self) -> bool: def needs_arglist(self) -> bool:
return True return True
def get_index_text(self, modname: str, name_cls: Tuple[str, str]) -> str: def add_target_and_index(self, name_cls: Tuple[str, str], sig: str,
signode: desc_signature) -> None:
super().add_target_and_index(name_cls, sig, signode)
modname = self.options.get('module', self.env.ref_context.get('py:module'))
node_id = signode['ids'][0]
name, cls = name_cls name, cls = name_cls
if modname: if modname:
return _('%s() (in module %s)') % (name, modname) text = _('%s() (in module %s)') % (name, modname)
self.indexnode['entries'].append(('single', text, node_id, '', None))
else: else:
return _('%s() (built-in function)') % name text = '%s; %s()' % (pairindextypes['builtin'], name)
self.indexnode['entries'].append(('pair', text, node_id, '', None))
def get_index_text(self, modname: str, name_cls: Tuple[str, str]) -> str:
# add index in own add_target_and_index() instead.
return None
class PyDecoratorFunction(PyFunction): class PyDecoratorFunction(PyFunction):
@ -915,8 +926,8 @@ class PyModule(SphinxDirective):
# the platform and synopsis aren't printed; in fact, they are only # the platform and synopsis aren't printed; in fact, they are only
# used in the modindex currently # used in the modindex currently
ret.append(target) ret.append(target)
indextext = _('%s (module)') % modname indextext = '%s; %s' % (pairindextypes['module'], modname)
inode = addnodes.index(entries=[('single', indextext, node_id, '', None)]) inode = addnodes.index(entries=[('pair', indextext, node_id, '', None)])
ret.append(inode) ret.append(inode)
return ret return ret

View File

@ -461,6 +461,7 @@ def test_pydata(app):
def test_pyfunction(app): def test_pyfunction(app):
text = (".. py:function:: func1\n" text = (".. py:function:: func1\n"
".. py:module:: example\n"
".. py:function:: func2\n" ".. py:function:: func2\n"
" :async:\n") " :async:\n")
domain = app.env.get_domain('py') domain = app.env.get_domain('py')
@ -469,15 +470,25 @@ def test_pyfunction(app):
[desc, ([desc_signature, ([desc_name, "func1"], [desc, ([desc_signature, ([desc_name, "func1"],
[desc_parameterlist, ()])], [desc_parameterlist, ()])],
[desc_content, ()])], [desc_content, ()])],
nodes.target,
addnodes.index,
addnodes.index, addnodes.index,
[desc, ([desc_signature, ([desc_annotation, "async "], [desc, ([desc_signature, ([desc_annotation, "async "],
[desc_addname, "example."],
[desc_name, "func2"], [desc_name, "func2"],
[desc_parameterlist, ()])], [desc_parameterlist, ()])],
[desc_content, ()])])) [desc_content, ()])]))
assert_node(doctree[0], addnodes.index,
entries=[('pair', 'built-in function; func1()', 'func1', '', None)])
assert_node(doctree[3], addnodes.index,
entries=[('pair', 'module; example', 'module-example', '', None)])
assert_node(doctree[4], addnodes.index,
entries=[('single', 'func2() (in module example)', 'example.func2', '', None)])
assert 'func1' in domain.objects assert 'func1' in domain.objects
assert domain.objects['func1'] == ('index', 'func1', 'function') assert domain.objects['func1'] == ('index', 'func1', 'function')
assert 'func2' in domain.objects assert 'example.func2' in domain.objects
assert domain.objects['func2'] == ('index', 'func2', 'function') assert domain.objects['example.func2'] == ('index', 'example.func2', 'function')
def test_pymethod_options(app): def test_pymethod_options(app):