Skip to content

Commit

Permalink
Merge pull request #9207 from tk0miya/9205_canonical_conflicts
Browse files Browse the repository at this point in the history
Fix #9205: py domain: canonical option causes xref resolution error
  • Loading branch information
tk0miya authored May 10, 2021
2 parents d2c8cd3 + 4ab0dba commit aa9fab5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Bugs fixed
autosummary_generate
* #8380: html search: tags for search result are broken
* #9198: i18n: Babel emits errors when running compile_catalog
* #9205: py domain: The :canonical: option causes "more than one target for
cross-reference" warning

Testing
--------
Expand Down
10 changes: 7 additions & 3 deletions sphinx/domains/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,9 +1269,13 @@ def resolve_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder
if not matches:
return None
elif len(matches) > 1:
logger.warning(__('more than one target found for cross-reference %r: %s'),
target, ', '.join(match[0] for match in matches),
type='ref', subtype='python', location=node)
canonicals = [m for m in matches if not m[1].aliased]
if len(canonicals) == 1:
matches = canonicals
else:
logger.warning(__('more than one target found for cross-reference %r: %s'),
target, ', '.join(match[0] for match in matches),
type='ref', subtype='python', location=node)
name, obj = matches[0]

if obj[2] == 'module':
Expand Down
9 changes: 9 additions & 0 deletions tests/roots/test-domain-py/canonical.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
caninical
=========

:py:class:`.Foo`

.. py:module:: canonical
.. py:class:: Foo
:canonical: original.module.Foo
3 changes: 3 additions & 0 deletions tests/roots/test-domain-py/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ test-domain-py

roles
module
module_option
abbr
canonical
11 changes: 11 additions & 0 deletions tests/test_domain_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ def find_obj(modname, prefix, obj_name, obj_type, searchmode=0):
('roles', 'NestedParentA.NestedChildA.subchild_1', 'method', False))])


@pytest.mark.sphinx('html', testroot='domain-py', freshenv=True)
def test_domain_py_canonical(app, status, warning):
app.builder.build_all()

content = (app.outdir / 'canonical.html').read_text()
assert ('<a class="reference internal" href="#canonical.Foo" title="canonical.Foo">'
'<code class="xref py py-class docutils literal notranslate">'
'<span class="pre">Foo</span></code></a>' in content)
assert warning.getvalue() == ''


def test_get_full_qualified_name():
env = Mock(domaindata={})
domain = PythonDomain(env)
Expand Down

0 comments on commit aa9fab5

Please sign in to comment.