Add remove_old
configuration to sphinx.ext.apidoc
#13668
+19
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Makes the option available to be configured when using apidoc as an extension instead of as a command-line tool.
Purpose
The command-line version of apidoc allows to specify the
--remove-old
option. When passed, the value is true.With the extension version of apidoc,
sphinx.ext.apidoc
,remove_old
defaults toTrue
, but isn't configurable.sphinx/sphinx/ext/apidoc/_shared.py
Lines 53 to 57 in 0767742
This means that when refactoring an old project to use the apidoc extension (in conf.py) instead of a customized Makefile with multiple handwritten calls to sphinx-apidoc, whilst wanting to keep the same generated output structure is impossible. The extension deletes all other files in the target directory (specified with
'destination'
's value ofapidoc_modules
configuration option). It deletes all other handwritten files, and even deletes the index.rst, which causes other failures in the build process.I understand that most people would want to generate these files in a subfolder of their source directory, but for legacy reasons, some contents and hand-written overrides are all in the same folder, and the pages name should be kept the same for our use-case.
Having the
remove_old
configuration value ported to the extension's config, even if not used by our project's makefile with the cli-based version, is the less invasive way for sphinx to allow our use case, and prevent the unexpected deletion of files when building (at least we have git to restore committed files). The unexpected part of the behaviour difference when migrating is caused by the default value that is different. It isFalse
for the cli (opt-in), butTrue
(non-configurable) when used as an extension.With this PR, the config value added is then properly used at the very end of
_run_apidoc_module()
, that guards calling_remove_old_files()
or not (line 89):sphinx/sphinx/ext/apidoc/_extension.py
Lines 63 to 91 in 0767742
Since I'm new to this code base (and never wrote an extension), I would request to take a special look at the very-end of
main()
insphinx/ext/apidoc/_cli.py
, to make sure the default value is still the same after this change. I'm still not sure, even after having applied the changes locally.sphinx/sphinx/ext/apidoc/_cli.py
Lines 255 to 279 in 0767742
However, I ran tox on my repo of this fork once I applied the same changes for this PR, and the existing tests passed. There is one test that is found when searching for "remove_old" or "remove-old", but it didn't catch the behaviour change between the cli and extension usage, so...
sphinx/tests/test_extensions/test_ext_apidoc.py
Lines 755 to 776 in 0767742
References
Note:
I will need to update the CHANGES.rst entry with the pull-request number once the PR is filed, and the PR number is assigned.Changed