Skip to content

Commit

Permalink
Type-hints: update to pass mypy checks with types-docutils update fro…
Browse files Browse the repository at this point in the history
…m 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.
  • Loading branch information
jayaddison committed Jul 4, 2024
1 parent b23ac4f commit 1b04ddb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion sphinx/ext/autodoc/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions sphinx/ext/inheritance_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,15 +378,15 @@ 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
# references to real URLs later. These nodes will eventually be
# 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
Expand Down
5 changes: 4 additions & 1 deletion sphinx/util/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions sphinx/util/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = '<generated text>',
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1b04ddb

Please sign in to comment.