You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Have one clear API for calling Sphinx without unintended side-effects (deprecate SphinxTestApp?) #12130
Currenty, there is no clearly documented way to "properly" use Sphinx programatically.
By "properly", I mean in a way that could be called multiple times within the same Python interpretor, without any unexpected polluting of the global state, and thus act exactly the same as if each call had been made in a separate Python interpretor
At present, there are diverging ways to do this, for the main CLI and for test cases:
Its not really clear from first appearance, but these actually almost achieve the same thing:
docutils_namespace, on exit, resets the directives/roles lookups and additional node visitors
SphinxTestApp.cleanup resets the directives/roles lookups and additional node visitors
However, there are also clear differences:
patch_docutils temporarily patches docutils in a number of ways, that do not appear to be addressed by SphinxTestApp, in essence meaning you are not strictly testing the behaviour of Sphinx in production when using SphinxTestApp
SphinxTestApp.cleanup does addition global state cleaning, restoring: sys.path, sphinx.locale.translators, and sphinx.pycode.ModuleAnalyzer.cache
I feel this divergence is unecessary, and ideally there would be one clear way to call sphinx as an API, perhaps something like:
An additional note (as I just mentioned in #12089 (comment))
is that it is not currently possible to temporarily turn-off colored logging output.
This is annoying for if you want to assert the content of the warning stream
changed the title [-]Have a clear API for calling `Sphinx` without side-effects (deprecate `SphinxTestApp`?)[/-][+]Have one clear API for calling `Sphinx` without side-effects (deprecate `SphinxTestApp`?)[/+]on Mar 18, 2024
changed the title [-]Have one clear API for calling `Sphinx` without side-effects (deprecate `SphinxTestApp`?)[/-][+]Have one clear API for calling `Sphinx` without unintended side-effects (deprecate `SphinxTestApp`?)[/+]on Mar 18, 2024
I'll add something that I'm interested in: multiple calls to build, passing in filenames.
Use case: a high-performance reloading server that is very different to the sphinx-autobuild approach. Currently, calling build repeatedly on the same sphinx.application.Sphinx instance has globals etc. as mentioned in this ticket.
In the context of testing, it'd also be nice if Sphinx exposed a pytest fixture for this.
I recently discovered that https://www.sphinx-doc.org/en/master/extdev/testing.html exists and even attempted using it. One of the problems it has is that it's totally undocumented, and another is that the fixture names aren't made for use externally. A third-party project would make use of it by injecting app and redefining root_dir — both names are so generic that it's impossible to guess what these are w/o prior knowledge.
One of the problems it has is that it's totally undocumented, and another is that the fixture names aren't made for use externally. A third-party project would make use of it by injecting app and redefining root_dir — both names are so generic that it's impossible to guess what these are w/o prior knowledge.
I started documenting it somewhere. I can try cherry-picking the documentation only if it could help though
Reacted by 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко)
Activity
[-]Have a clear API for calling `Sphinx` without side-effects (deprecate `SphinxTestApp`?)[/-][+]Have one clear API for calling `Sphinx` without side-effects (deprecate `SphinxTestApp`?)[/+][-]Have one clear API for calling `Sphinx` without side-effects (deprecate `SphinxTestApp`?)[/-][+]Have one clear API for calling `Sphinx` without unintended side-effects (deprecate `SphinxTestApp`?)[/+]pauleveritt commentedon Jun 6, 2024
I'll add something that I'm interested in: multiple calls to
build, passing infilenames.Use case: a high-performance reloading server that is very different to the
sphinx-autobuildapproach. Currently, callingbuildrepeatedly on the samesphinx.application.Sphinxinstance has globals etc. as mentioned in this ticket.webknjaz commentedon Jun 26, 2024
In the context of testing, it'd also be nice if Sphinx exposed a pytest fixture for this.
webknjaz commentedon Feb 26, 2025
I recently discovered that https://www.sphinx-doc.org/en/master/extdev/testing.html exists and even attempted using it. One of the problems it has is that it's totally undocumented, and another is that the fixture names aren't made for use externally. A third-party project would make use of it by injecting
appand redefiningroot_dir— both names are so generic that it's impossible to guess what these are w/o prior knowledge.picnixz commentedon Feb 28, 2025
I started documenting it somewhere. I can try cherry-picking the documentation only if it could help though
webknjaz commentedon Feb 28, 2025
@picnixz well, the docs would help. But I'd want the names to make sense outside the Sphinx upstream too...