Skip to content

Commit aa9fab5

Browse files
authored
Merge pull request #9207 from tk0miya/9205_canonical_conflicts
Fix #9205: py domain: canonical option causes xref resolution error
2 parents d2c8cd3 + 4ab0dba commit aa9fab5

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Bugs fixed
2222
autosummary_generate
2323
* #8380: html search: tags for search result are broken
2424
* #9198: i18n: Babel emits errors when running compile_catalog
25+
* #9205: py domain: The :canonical: option causes "more than one target for
26+
cross-reference" warning
2527

2628
Testing
2729
--------

sphinx/domains/python.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,9 +1269,13 @@ def resolve_xref(self, env: BuildEnvironment, fromdocname: str, builder: Builder
12691269
if not matches:
12701270
return None
12711271
elif len(matches) > 1:
1272-
logger.warning(__('more than one target found for cross-reference %r: %s'),
1273-
target, ', '.join(match[0] for match in matches),
1274-
type='ref', subtype='python', location=node)
1272+
canonicals = [m for m in matches if not m[1].aliased]
1273+
if len(canonicals) == 1:
1274+
matches = canonicals
1275+
else:
1276+
logger.warning(__('more than one target found for cross-reference %r: %s'),
1277+
target, ', '.join(match[0] for match in matches),
1278+
type='ref', subtype='python', location=node)
12751279
name, obj = matches[0]
12761280

12771281
if obj[2] == 'module':
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
caninical
2+
=========
3+
4+
:py:class:`.Foo`
5+
6+
.. py:module:: canonical
7+
8+
.. py:class:: Foo
9+
:canonical: original.module.Foo

tests/roots/test-domain-py/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ test-domain-py
55

66
roles
77
module
8+
module_option
9+
abbr
10+
canonical

tests/test_domain_py.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,17 @@ def find_obj(modname, prefix, obj_name, obj_type, searchmode=0):
236236
('roles', 'NestedParentA.NestedChildA.subchild_1', 'method', False))])
237237

238238

239+
@pytest.mark.sphinx('html', testroot='domain-py', freshenv=True)
240+
def test_domain_py_canonical(app, status, warning):
241+
app.builder.build_all()
242+
243+
content = (app.outdir / 'canonical.html').read_text()
244+
assert ('<a class="reference internal" href="#canonical.Foo" title="canonical.Foo">'
245+
'<code class="xref py py-class docutils literal notranslate">'
246+
'<span class="pre">Foo</span></code></a>' in content)
247+
assert warning.getvalue() == ''
248+
249+
239250
def test_get_full_qualified_name():
240251
env = Mock(domaindata={})
241252
domain = PythonDomain(env)

0 commit comments

Comments
 (0)