Skip to content

Commit

Permalink
Merge pull request sphinx-doc#7395 from tk0miya/7219_indexentries_for…
Browse files Browse the repository at this point in the history
…_pyfunc

Fix sphinx-doc#7219: py:function directive generates incorrect index entry
  • Loading branch information
tk0miya committed Mar 29, 2020
2 parents 7887615 + fe9473f commit 70c61e4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Bugs fixed
argument lists, and more comprehensive error messages in some cases.
* C, C++, fix crash and wrong duplicate warnings related to anon symbols.
* #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
--------
Expand Down
21 changes: 16 additions & 5 deletions sphinx/domains/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,23 @@ def get_signature_prefix(self, sig: str) -> str:
def needs_arglist(self) -> bool:
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
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:
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):
Expand Down Expand Up @@ -915,8 +926,8 @@ def run(self) -> List[Node]:
# the platform and synopsis aren't printed; in fact, they are only
# used in the modindex currently
ret.append(target)
indextext = _('%s (module)') % modname
inode = addnodes.index(entries=[('single', indextext, node_id, '', None)])
indextext = '%s; %s' % (pairindextypes['module'], modname)
inode = addnodes.index(entries=[('pair', indextext, node_id, '', None)])
ret.append(inode)
return ret

Expand Down
15 changes: 13 additions & 2 deletions tests/test_domain_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ def test_pydata(app):

def test_pyfunction(app):
text = (".. py:function:: func1\n"
".. py:module:: example\n"
".. py:function:: func2\n"
" :async:\n")
domain = app.env.get_domain('py')
Expand All @@ -469,15 +470,25 @@ def test_pyfunction(app):
[desc, ([desc_signature, ([desc_name, "func1"],
[desc_parameterlist, ()])],
[desc_content, ()])],
nodes.target,
addnodes.index,
addnodes.index,
[desc, ([desc_signature, ([desc_annotation, "async "],
[desc_addname, "example."],
[desc_name, "func2"],
[desc_parameterlist, ()])],
[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 domain.objects['func1'] == ('index', 'func1', 'function')
assert 'func2' in domain.objects
assert domain.objects['func2'] == ('index', 'func2', 'function')
assert 'example.func2' in domain.objects
assert domain.objects['example.func2'] == ('index', 'example.func2', 'function')


def test_pymethod_options(app):
Expand Down

0 comments on commit 70c61e4

Please sign in to comment.