Skip to content

Commit 6d15153

Browse files
Add tilde prefix support for the :py:deco: role (#13545)
Co-authored-by: Adam Turner <[email protected]>
1 parent 4dbdf80 commit 6d15153

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Bugs fixed
3131

3232
* #13369: Correctly parse and cross-reference unpacked type annotations.
3333
Patch by Alicia Garcia-Raboso.
34+
* #13528: Add tilde ``~`` prefix support for :rst:role:`py:deco`.
35+
Patch by Shengyu Zhang and Adam Turner.
3436

3537
Testing
3638
-------

sphinx/domains/python/__init__.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from collections.abc import Iterable, Iterator, Sequence, Set
3030
from typing import Any, ClassVar
3131

32-
from docutils.nodes import Element, Node, TextElement
32+
from docutils.nodes import Element, Node
3333

3434
from sphinx.addnodes import desc_signature, pending_xref
3535
from sphinx.application import Sphinx
@@ -594,23 +594,17 @@ def process_link(
594594

595595

596596
class _PyDecoXRefRole(PyXRefRole):
597-
def __init__(
597+
def process_link(
598598
self,
599-
fix_parens: bool = False,
600-
lowercase: bool = False,
601-
nodeclass: type[Element] | None = None,
602-
innernodeclass: type[TextElement] | None = None,
603-
warn_dangling: bool = False,
604-
) -> None:
605-
super().__init__(
606-
fix_parens=True,
607-
lowercase=lowercase,
608-
nodeclass=nodeclass,
609-
innernodeclass=innernodeclass,
610-
warn_dangling=warn_dangling,
599+
env: BuildEnvironment,
600+
refnode: Element,
601+
has_explicit_title: bool,
602+
title: str,
603+
target: str,
604+
) -> tuple[str, str]:
605+
title, target = super().process_link(
606+
env, refnode, has_explicit_title, title, target
611607
)
612-
613-
def update_title_and_target(self, title: str, target: str) -> tuple[str, str]:
614608
return f'@{title}', target
615609

616610

tests/test_domains/test_domain_py.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,3 +1791,18 @@ def test_pep_695_and_pep_696_whitespaces_in_default(app, tp_list, tptext):
17911791
text = f'.. py:function:: f{tp_list}() -> Annotated[T, Qux[int]()]'
17921792
doctree = restructuredtext.parse(app, text)
17931793
assert doctree.astext() == f'\n\nf{tptext}() -> Annotated[T, Qux[int]()]\n\n'
1794+
1795+
1796+
def test_deco_role(app):
1797+
text = """\
1798+
.. py:decorator:: foo.bar
1799+
:no-contents-entry:
1800+
:no-index-entry:
1801+
:no-typesetting:
1802+
"""
1803+
1804+
doctree = restructuredtext.parse(app, text + '\n:py:deco:`foo.bar`')
1805+
assert doctree.astext() == '\n\n\n\n@foo.bar'
1806+
1807+
doctree = restructuredtext.parse(app, text + '\n:py:deco:`~foo.bar`')
1808+
assert doctree.astext() == '\n\n\n\n@bar'

0 commit comments

Comments
 (0)