From 1b04ddbc9acfcc8666d0fae23906949b3468b790 Mon Sep 17 00:00:00 2001 From: James Addison Date: Thu, 4 Jul 2024 15:28:41 +0100 Subject: [PATCH] Type-hints: update to pass mypy checks with types-docutils update from 2024-07-04. * Reporter.get_source_and_line is added at runtime by the ``RSTState.runtime_init`` method. * Pass the RST parser state's ``Inliner`` when calling class roles (could this reflect an interface change, or is it a bug?). * Add a RST-parser-context generic type argument to ``RSTState`` type-hints. --- sphinx/ext/autodoc/directive.py | 2 +- sphinx/ext/inheritance_diagram.py | 4 ++-- sphinx/util/nodes.py | 5 ++++- sphinx/util/parsing.py | 9 ++++++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/sphinx/ext/autodoc/directive.py b/sphinx/ext/autodoc/directive.py index 7741dcbdf0d..9b0bf66a02c 100644 --- a/sphinx/ext/autodoc/directive.py +++ b/sphinx/ext/autodoc/directive.py @@ -112,7 +112,7 @@ def run(self) -> list[Node]: reporter = self.state.document.reporter try: - source, lineno = reporter.get_source_and_line( + source, lineno = reporter.get_source_and_line( # type: ignore[attr-defined] self.lineno) except AttributeError: source, lineno = (None, None) diff --git a/sphinx/ext/inheritance_diagram.py b/sphinx/ext/inheritance_diagram.py index b9e51378099..7a47003d7f9 100644 --- a/sphinx/ext/inheritance_diagram.py +++ b/sphinx/ext/inheritance_diagram.py @@ -378,7 +378,7 @@ def run(self) -> list[Node]: aliases=self.config.inheritance_alias, top_classes=node['top-classes']) except InheritanceException as err: - return [node.document.reporter.warning(err, line=self.lineno)] # type: ignore[union-attr] + return [node.document.reporter.warning(err, line=self.lineno)] # Create xref nodes for each target of the graph's image map and # add them to the doc tree so that Sphinx can resolve the @@ -386,7 +386,7 @@ def run(self) -> list[Node]: # removed from the doctree after we're done with them. for name in graph.get_all_class_names(): refnodes, x = class_role( # type: ignore[call-arg,misc] - 'class', ':class:`%s`' % name, name, 0, self.state) + 'class', ':class:`%s`' % name, name, 0, self.state.inliner) node.extend(refnodes) # Store the graph object so we can use it to generate the # dot file later diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py index 9f3e827c827..2e39ed33fcc 100644 --- a/sphinx/util/nodes.py +++ b/sphinx/util/nodes.py @@ -325,7 +325,10 @@ def traverse_translatable_index( yield node, entries -def nested_parse_with_titles(state: RSTState, content: StringList, node: Node, +_RSTContext = TypeVar("_RSTContext") + + +def nested_parse_with_titles(state: RSTState[_RSTContext], content: StringList, node: Node, content_offset: int = 0) -> str: """Version of state.nested_parse() that allows titles and does not require titles to have the same decoration as the calling document. diff --git a/sphinx/util/parsing.py b/sphinx/util/parsing.py index a8f937f8fe1..4318f12bf39 100644 --- a/sphinx/util/parsing.py +++ b/sphinx/util/parsing.py @@ -3,7 +3,7 @@ from __future__ import annotations import contextlib -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, TypeVar from docutils.nodes import Element, Node from docutils.statemachine import StringList, string2lines @@ -14,8 +14,11 @@ from docutils.parsers.rst.states import RSTState +_RSTContext = TypeVar("_RSTContext") + + def nested_parse_to_nodes( - state: RSTState, + state: RSTState[_RSTContext], text: str | StringList, *, source: str = '', @@ -67,7 +70,7 @@ def nested_parse_to_nodes( @contextlib.contextmanager -def _fresh_title_style_context(state: RSTState) -> Iterator[None]: +def _fresh_title_style_context(state: RSTState[_RSTContext]) -> Iterator[None]: # hack around title style bookkeeping memo = state.memo surrounding_title_styles: list[str | tuple[str, str]] = memo.title_styles