Skip to content

Commit

Permalink
Work around beartype + myst-parser issue
Browse files Browse the repository at this point in the history
adamtheturtle committed Jan 21, 2025
1 parent f9d1cc8 commit dfb2a91
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ dynamic = [
dependencies = [
"beartype>=0.18.5",
"docutils>=0.19",
"myst-parser>=4.0.0",
"sphinx>=7.3.5",
]
optional-dependencies.dev = [
@@ -51,7 +52,6 @@ optional-dependencies.dev = [
"interrogate==1.7.0",
"mypy[faster-cache]==1.14.1",
"mypy-strict-kwargs==2024.12.25",
"myst-parser==4.0.0",
"pre-commit==4.0.1",
"pyenchant==3.3.0rc1",
"pylint==3.3.3",
17 changes: 14 additions & 3 deletions src/sphinx_substitution_extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
from docutils.parsers.rst.roles import code_role
from docutils.parsers.rst.states import Inliner
from docutils.statemachine import StringList
from myst_parser.mocking import MockInliner
from sphinx import addnodes
from sphinx.application import Sphinx
from sphinx.directives.code import CodeBlock
@@ -101,21 +102,31 @@ def __call__( # pylint: disable=dangerous-default-value
rawtext: str,
text: str,
lineno: int,
inliner: Inliner,
inliner: Inliner | MockInliner,
# We allow mutable defaults as the Sphinx implementation requires it.
options: dict[Any, Any] = {}, # noqa: B006
content: list[str] = [], # noqa: B006
) -> tuple[list[Node], list[system_message]]:
"""
Replace placeholders with given variables.
"""
inliner_document = inliner.document
for name, value in inliner_document.substitution_defs.items():
for name, value in inliner.document.substitution_defs.items():
replacement = value.astext()
text = text.replace(f"|{name}|", replacement)
rawtext = text.replace(f"|{name}|", replacement)
rawtext = rawtext.replace(name, replacement)

# ``types-docutils`` says that ``code_role`` requires an ``Inliner``
# for ``inliner``.
#
# We can remove this when
# https://github.com/executablebooks/MyST-Parser/issues/1017
# is resolved by typing ``inliner`` as ``Inliner``.
if isinstance(inliner, MockInliner):
new_inliner = Inliner()
new_inliner.document = inliner.document
inliner = new_inliner

return code_role(
role=typ,
rawtext=rawtext,

0 comments on commit dfb2a91

Please sign in to comment.