Skip to content

Commit

Permalink
Add monkey patch to fix source file/line number info for object descr…
Browse files Browse the repository at this point in the history
…iptions

This addresses the issue described at
sphinx-doc/sphinx#11147.
  • Loading branch information
jbms committed Jan 22, 2023
1 parent 42d5d86 commit 9c6a2c8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions sphinx_immaterial/__init__.py
Expand Up @@ -16,6 +16,7 @@

from . import html_translator_mixin
from .apidoc import apidoc_formatting
from .apidoc import fix_sphinx_issue_11147 # pylint: disable=unused-import
from . import nav_adapt
from . import sections

Expand Down
29 changes: 29 additions & 0 deletions sphinx_immaterial/apidoc/fix_sphinx_issue_11147.py
@@ -0,0 +1,29 @@
"""Monkey patches a fix for https://github.com/sphinx-doc/sphinx/pull/11147."""

import inspect

import sphinx.util.nodes
import sphinx.util.docutils


def _monkey_patch_nested_parse_with_titles():
orig_nested_parse_with_titles = sphinx.util.nodes.nested_parse_with_titles

if list(inspect.signature(orig_nested_parse_with_titles).parameters.keys()) != [
"state",
"content",
"node",
]:
# Assume https://github.com/sphinx-doc/sphinx/pull/11147 has been merged.
return

# Note that this is different from the fix in
# https://github.com/sphinx-doc/sphinx/pull/11147, since it is not practical
# to monkey patch all callers to pass in the `content_offset`. Instead, we
# use `switch_source_input`, which makes it unnecessary to pass in
# `content_offset`.
def nested_parse_with_titles(state, content, node):
with sphinx.util.docutils.switch_source_input(state, content):
return orig_nested_parse_with_titles(state, content, node)

sphinx.util.nodes.nested_parse_with_titles = nested_parse_with_titles

0 comments on commit 9c6a2c8

Please sign in to comment.