Skip to content

Commit 87e0fa2

Browse files
authored
🐛 Fixes for Sphinx v7 (#22)
Accounts for change in `srcdir` type in sphinx-doc/sphinx#11526, and now removes `translation_progress` attribute of the doctree by default (added in sphinx 7.1).
1 parent b96eb29 commit 87e0fa2

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ repos:
4141
args: [--config-file=pyproject.toml]
4242
additional_dependencies:
4343
- types-docutils
44-
- sphinx~=4.1
44+
- sphinx~=7.0
4545
- pytest

src/sphinx_pytest/plugin.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from docutils import nodes
1010
from docutils.core import Publisher
1111
import pytest
12+
from sphinx import version_info as sphinx_version_info
1213
from sphinx.environment import BuildEnvironment
13-
from sphinx.testing.path import path
1414
from sphinx.testing.util import SphinxTestApp
1515

1616
from .builders import DoctreeBuilder
@@ -90,12 +90,22 @@ def doctrees(self) -> dict[str, nodes.document] | Doctrees:
9090
except AttributeError:
9191
return Doctrees(self.env)
9292

93-
def pformat(self, docname: str = "index") -> str:
93+
def pformat(
94+
self, docname: str = "index", pop_doc_attrs=("translation_progress",)
95+
) -> str:
9496
"""Return an indented pseudo-XML representation.
9597
9698
The src directory is replaced with <src>, for reproducibility.
99+
100+
:param pop_doc_attrs: Remove these attributes of the doctree node,
101+
before converting to text.
102+
By default, ``translation_progress`` is removed for compatibility
103+
(added in sphinx 7.1).
97104
"""
98-
text = self.doctrees[docname].pformat()
105+
doctree = self.doctrees[docname].deepcopy()
106+
for attr_name in pop_doc_attrs:
107+
doctree.attributes.pop(attr_name, None)
108+
text = doctree.pformat()
99109
return text.replace(str(self._app.srcdir) + os.sep, "<src>/").rstrip()
100110

101111
def get_resolved_doctree(self, docname: str = "index") -> nodes.document:
@@ -109,9 +119,22 @@ def get_resolved_doctree(self, docname: str = "index") -> nodes.document:
109119
# https://github.com/sphinx-doc/sphinx/blob/05a898ecb4ff8e654a053a1ba5131715a4514812/sphinx/environment/__init__.py#L538
110120
return doctree
111121

112-
def get_resolved_pformat(self, docname: str = "index") -> str:
113-
"""Return the pformat of the doctree after post-transforms."""
114-
text = self.get_resolved_doctree(docname).pformat()
122+
def get_resolved_pformat(
123+
self, docname: str = "index", pop_doc_attrs=("translation_progress",)
124+
) -> str:
125+
"""Return an indented pseudo-XML representation, after post-transforms.
126+
127+
The src directory is replaced with <src>, for reproducibility.
128+
129+
:param pop_doc_attrs: Remove these attributes of the doctree node,
130+
before converting to text.
131+
By default, ``translation_progress`` is removed for compatibility
132+
(added in sphinx 7.1).
133+
"""
134+
doctree = self.get_resolved_doctree(docname)
135+
for attr_name in pop_doc_attrs:
136+
doctree.attributes.pop(attr_name, None)
137+
text = doctree.pformat()
115138
return text.replace(str(self._app.srcdir) + os.sep, "<src>/").rstrip()
116139

117140

@@ -141,9 +164,17 @@ def __call__(
141164
self.srcdir.joinpath(filename).parent.mkdir(parents=True, exist_ok=True)
142165
self.srcdir.joinpath(filename).write_text(content, encoding="utf8")
143166

167+
srcdir: Any
168+
if sphinx_version_info >= (7, 2):
169+
srcdir = self.srcdir
170+
else:
171+
from sphinx.testing.path import path
172+
173+
srcdir = path(str(self.srcdir))
174+
144175
return AppWrapper(
145176
self._app_cls(
146-
srcdir=path(str(self.srcdir)),
177+
srcdir=srcdir,
147178
buildername=self.buildername,
148179
confoverrides=self._confoverrides,
149180
**kwargs,

0 commit comments

Comments
 (0)