Skip to content

Add tilde prefix support for :py:deco: role #13545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Bugs fixed

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

Testing
-------
26 changes: 10 additions & 16 deletions sphinx/domains/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from collections.abc import Iterable, Iterator, Sequence, Set
from typing import Any, ClassVar

from docutils.nodes import Element, Node, TextElement
from docutils.nodes import Element, Node

from sphinx.addnodes import desc_signature, pending_xref
from sphinx.application import Sphinx
Expand Down Expand Up @@ -594,23 +594,17 @@ def process_link(


class _PyDecoXRefRole(PyXRefRole):
def __init__(
def process_link(
self,
fix_parens: bool = False,
lowercase: bool = False,
nodeclass: type[Element] | None = None,
innernodeclass: type[TextElement] | None = None,
warn_dangling: bool = False,
) -> None:
super().__init__(
fix_parens=True,
lowercase=lowercase,
nodeclass=nodeclass,
innernodeclass=innernodeclass,
warn_dangling=warn_dangling,
env: BuildEnvironment,
refnode: Element,
has_explicit_title: bool,
title: str,
target: str,
) -> tuple[str, str]:
title, target = super().process_link(
env, refnode, has_explicit_title, title, target
)

def update_title_and_target(self, title: str, target: str) -> tuple[str, str]:
return f'@{title}', target


Expand Down
15 changes: 15 additions & 0 deletions tests/test_domains/test_domain_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -1791,3 +1791,18 @@ def test_pep_695_and_pep_696_whitespaces_in_default(app, tp_list, tptext):
text = f'.. py:function:: f{tp_list}() -> Annotated[T, Qux[int]()]'
doctree = restructuredtext.parse(app, text)
assert doctree.astext() == f'\n\nf{tptext}() -> Annotated[T, Qux[int]()]\n\n'


def test_deco_role(app):
text = """\
.. py:decorator:: foo.bar
:no-contents-entry:
:no-index-entry:
:no-typesetting:
"""

doctree = restructuredtext.parse(app, text + '\n:py:deco:`foo.bar`')
assert doctree.astext() == '\n\n\n\[email protected]'

doctree = restructuredtext.parse(app, text + '\n:py:deco:`~foo.bar`')
assert doctree.astext() == '\n\n\n\n@bar'
Loading