Skip to content

Commit 59e9553

Browse files
authored
Use a protocol for RoleFunction (#12513)
1 parent e63e2f3 commit 59e9553

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

sphinx/ext/inheritance_diagram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def run(self) -> list[Node]:
385385
# references to real URLs later. These nodes will eventually be
386386
# removed from the doctree after we're done with them.
387387
for name in graph.get_all_class_names():
388-
refnodes, x = class_role( # type: ignore[call-arg,misc]
388+
refnodes, x = class_role( # type: ignore[misc]
389389
'class', ':class:`%s`' % name, name, 0, self.state.inliner)
390390
node.extend(refnodes)
391391
# Store the graph object so we can use it to generate the

sphinx/util/typing.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
if TYPE_CHECKING:
2626
from collections.abc import Mapping
27-
from typing import Final, Literal
27+
from typing import Final, Literal, Protocol
2828

2929
from docutils.parsers.rst.states import RSTState as _RSTStateGeneric
3030
from typing_extensions import TypeAlias, TypeIs
@@ -95,10 +95,25 @@ def is_invalid_builtin_class(obj: Any) -> bool:
9595
PathMatcher = Callable[[str], bool]
9696

9797
# common role functions
98-
RoleFunction = Callable[
99-
[str, str, str, int, Inliner, dict[str, Any], Sequence[str]],
100-
tuple[list[nodes.Node], list[nodes.system_message]],
101-
]
98+
if TYPE_CHECKING:
99+
class RoleFunction(Protocol):
100+
def __call__(
101+
self,
102+
name: str,
103+
rawtext: str,
104+
text: str,
105+
lineno: int,
106+
inliner: Inliner,
107+
/,
108+
options: dict[str, Any] | None = None,
109+
content: Sequence[str] = (),
110+
) -> tuple[list[nodes.Node], list[nodes.system_message]]:
111+
...
112+
else:
113+
RoleFunction = Callable[
114+
[str, str, str, int, Inliner, dict[str, Any], Sequence[str]],
115+
tuple[list[nodes.Node], list[nodes.system_message]],
116+
]
102117

103118
# A option spec for directive
104119
OptionSpec = dict[str, Callable[[str], Any]]

0 commit comments

Comments
 (0)