Skip to content

Commit f578eed

Browse files
authored
Fix incorrect index treatment of the :canonical: option in the py:type directive (#13926)
1 parent 8dd7a23 commit f578eed

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Features added
7070
Bugs fixed
7171
----------
7272

73+
* #13926: multiple py:type directives for the same canonical type no
74+
longer result in spurious duplicate object description warnings.
75+
Patch by Jeremy Maitin-Shepard.
7376
* #1327: LaTeX: tables using longtable raise error if
7477
:rst:dir:`tabularcolumns` specifies automatic widths
7578
(``L``, ``R``, ``C``, or ``J``).

sphinx/domains/python/_object.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,17 @@ def add_target_and_index(
424424
domain = self.env.domains.python_domain
425425
domain.note_object(fullname, self.objtype, node_id, location=signode)
426426

427-
canonical_name = self.options.get('canonical')
428-
if canonical_name:
429-
domain.note_object(
430-
canonical_name, self.objtype, node_id, aliased=True, location=signode
431-
)
427+
if self.objtype != 'type':
428+
# py:type directive uses `canonical` option for a different meaning
429+
canonical_name = self.options.get('canonical')
430+
if canonical_name:
431+
domain.note_object(
432+
canonical_name,
433+
self.objtype,
434+
node_id,
435+
aliased=True,
436+
location=signode,
437+
)
432438

433439
if 'no-index-entry' not in self.options:
434440
if index_text := self.get_index_text(mod_name, name_cls): # type: ignore[arg-type]

tests/test_domains/test_domain_py.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,3 +1806,16 @@ def test_deco_role(app):
18061806

18071807
doctree = restructuredtext.parse(app, text + '\n:py:deco:`~foo.bar`')
18081808
assert doctree.astext() == '\n\n\n\n@bar'
1809+
1810+
1811+
def test_pytype_canonical(app):
1812+
text = """\
1813+
.. py:type:: A
1814+
:canonical: int
1815+
1816+
.. py:type:: B
1817+
:canonical: int
1818+
"""
1819+
1820+
doctree = restructuredtext.parse(app, text)
1821+
assert not app.warning.getvalue()

0 commit comments

Comments
 (0)