Skip to content

Commit 774f8cc

Browse files
authored
Add the new py:deco cross-reference role (#13292)
1 parent d486e80 commit 774f8cc

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ Features added
9898
Patch by Bénédikt Tran and Adam Turner.
9999
* #13163: Always print the full context when Sphinx encounters an internal error.
100100
Patch by Kevin Deldycke and Adam Turner.
101+
* #13105: Introduce the :rst:role:`py:deco` role to cross-reference decorator
102+
functions and methods in the Python domain.
103+
Patch by Adam Turner.
101104

102105
Bugs fixed
103106
----------

doc/usage/domains/python.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,7 @@ The following directives are provided for module and class contents:
562562

563563
(as opposed to ``.. py:decorator:: removename(func)``.)
564564

565-
There is no ``py:deco`` role to link to a decorator that is marked up with
566-
this directive; rather, use the :rst:role:`py:func` role.
565+
Refer to a decorator function using the :rst:role:`py:deco` role.
567566

568567
.. rst:directive:option:: single-line-parameter-list
569568
:type: no value
@@ -589,7 +588,7 @@ The following directives are provided for module and class contents:
589588
590589
Same as :rst:dir:`py:decorator`, but for decorators that are methods.
591590

592-
Refer to a decorator method using the :rst:role:`py:meth` role.
591+
Refer to a decorator method using the :rst:role:`py:deco` role.
593592

594593
.. _annotation expression: https://typing.readthedocs.io/en/latest/spec/annotations.html#type-and-annotation-expressions
595594

@@ -769,6 +768,17 @@ a matching identifier is found:
769768
automatically by Sphinx if the :confval:`add_function_parentheses` config
770769
value is ``True`` (the default).
771770

771+
.. rst:role:: py:deco
772+
773+
Reference a Python decorator; dotted names may be used.
774+
The rendered output will be prepended with an at-sign (``@``),
775+
for example: ``:py:deco:`removename``` produces :py:deco:`removename`.
776+
777+
.. py:decorator:: removename
778+
:no-contents-entry:
779+
:no-index-entry:
780+
:no-typesetting:
781+
772782
.. rst:role:: py:data
773783
774784
Reference a module-level variable.

sphinx/domains/python/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from collections.abc import Iterable, Iterator, Sequence, Set
2929
from typing import Any, ClassVar
3030

31-
from docutils.nodes import Element, Node
31+
from docutils.nodes import Element, Node, TextElement
3232

3333
from sphinx.addnodes import desc_signature, pending_xref
3434
from sphinx.application import Sphinx
@@ -592,6 +592,27 @@ def process_link(
592592
return title, target
593593

594594

595+
class _PyDecoXRefRole(PyXRefRole):
596+
def __init__(
597+
self,
598+
fix_parens: bool = False,
599+
lowercase: bool = False,
600+
nodeclass: type[Element] | None = None,
601+
innernodeclass: type[TextElement] | None = None,
602+
warn_dangling: bool = False,
603+
) -> None:
604+
super().__init__(
605+
fix_parens=True,
606+
lowercase=lowercase,
607+
nodeclass=nodeclass,
608+
innernodeclass=innernodeclass,
609+
warn_dangling=warn_dangling,
610+
)
611+
612+
def update_title_and_target(self, title: str, target: str) -> tuple[str, str]:
613+
return f'@{title}', target
614+
615+
595616
def filter_meta_fields(
596617
app: Sphinx, domain: str, objtype: str, content: Element
597618
) -> None:
@@ -748,6 +769,7 @@ class PythonDomain(Domain):
748769
'data': PyXRefRole(),
749770
'exc': PyXRefRole(),
750771
'func': PyXRefRole(fix_parens=True),
772+
'deco': _PyDecoXRefRole(),
751773
'class': PyXRefRole(),
752774
'const': PyXRefRole(),
753775
'attr': PyXRefRole(),

0 commit comments

Comments
 (0)