Skip to content

Commit 18bb8bc

Browse files
Implement :no-index-entry: for autodoc (sphinx-doc#12233)
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
1 parent 7801540 commit 18bb8bc

5 files changed

Lines changed: 79 additions & 2 deletions

File tree

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ Features added
7676
* #13272: The Python and JavaScript module directives now support
7777
the ``:no-index-entry:`` option.
7878
Patch by Adam Turner.
79+
* #12233: autodoc: Allow directives to use ``:no-index-entry:``
80+
and include the ``:no-index:`` and ``:no-index-entry:`` options within
81+
:confval:`autodoc_default_options`.
82+
Patch by Jonny Saunders and Adam Turner.
7983

8084
Bugs fixed
8185
----------

doc/usage/extensions/autodoc.rst

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,15 @@ Automatically document modules
326326
327327
.. versionadded:: 0.4
328328
329+
.. rst:directive:option:: no-index-entry
330+
:type:
331+
332+
Do not generate an index entry for the documented module
333+
or any auto-documented members.
334+
Unlike ``:no-index:``, cross-references are still created.
335+
336+
.. versionadded:: 8.2
337+
329338
.. rst:directive:option:: platform: platforms
330339
:type: comma separated list
331340
@@ -578,6 +587,15 @@ Automatically document classes or exceptions
578587
579588
.. versionadded:: 0.4
580589
590+
.. rst:directive:option:: no-index-entry
591+
:type:
592+
593+
Do not generate an index entry for the documented class
594+
or any auto-documented members.
595+
Unlike ``:no-index:``, cross-references are still created.
596+
597+
.. versionadded:: 8.2
598+
581599
.. rst:directive:option:: class-doc-from
582600
:type: class, init, or both
583601
@@ -854,6 +872,14 @@ Automatically document function-like objects
854872
855873
.. versionadded:: 0.4
856874
875+
.. rst:directive:option:: no-index-entry
876+
:type:
877+
878+
Do not generate an index entry for the documented function.
879+
Unlike ``:no-index:``, cross-references are still created.
880+
881+
.. versionadded:: 8.2
882+
857883
858884
Automatically document attributes or data
859885
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -900,11 +926,18 @@ Automatically document attributes or data
900926
.. rst:directive:option:: no-index
901927
:type:
902928
903-
Do not generate an index entry for the documented class
904-
or any auto-documented members.
929+
Do not generate an index entry for the documented variable or constant.
905930
906931
.. versionadded:: 0.4
907932
933+
.. rst:directive:option:: no-index-entry
934+
:type:
935+
936+
Do not generate an index entry for the documented variable or constant.
937+
Unlike ``:no-index:``, cross-references are still created.
938+
939+
.. versionadded:: 8.2
940+
908941
.. rst:directive:option:: annotation: value
909942
:type: string
910943
@@ -1039,6 +1072,8 @@ There are also config values that you can set:
10391072
* ``'show-inheritance'``: See :rst:dir:`autoclass:show-inheritance`.
10401073
* ``'class-doc-from'``: See :rst:dir:`autoclass:class-doc-from`.
10411074
* ``'no-value'``: See :rst:dir:`autodata:no-value`.
1075+
* ``'no-index'``: See :rst:dir:`automodule:no-index`.
1076+
* ``'no-index-entry'``: See :rst:dir:`automodule:no-index-entry`.
10421077

10431078
.. versionadded:: 1.8
10441079

sphinx/ext/autodoc/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ class Documenter:
365365

366366
option_spec: ClassVar[OptionSpec] = {
367367
'no-index': bool_option,
368+
'no-index-entry': bool_option,
368369
'noindex': bool_option,
369370
}
370371

@@ -605,6 +606,8 @@ def add_directive_header(self, sig: str) -> None:
605606

606607
if self.options.no_index or self.options.noindex:
607608
self.add_line(' :no-index:', sourcename)
609+
if self.options.no_index_entry:
610+
self.add_line(' :no-index-entry:', sourcename)
608611
if self.objpath:
609612
# Be explicit about the module, this is necessary since .. class::
610613
# etc. don't support a prepended module name
@@ -1110,6 +1113,7 @@ class ModuleDocumenter(Documenter):
11101113
'members': members_option,
11111114
'undoc-members': bool_option,
11121115
'no-index': bool_option,
1116+
'no-index-entry': bool_option,
11131117
'inherited-members': inherited_members_option,
11141118
'show-inheritance': bool_option,
11151119
'synopsis': identity,
@@ -1197,6 +1201,8 @@ def add_directive_header(self, sig: str) -> None:
11971201
self.add_line(' :platform: ' + self.options.platform, sourcename)
11981202
if self.options.deprecated:
11991203
self.add_line(' :deprecated:', sourcename)
1204+
if self.options.no_index_entry:
1205+
self.add_line(' :no-index-entry:', sourcename)
12001206

12011207
def get_module_members(self) -> dict[str, ObjectMember]:
12021208
"""Get members of target module."""
@@ -1625,6 +1631,7 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
16251631
'members': members_option,
16261632
'undoc-members': bool_option,
16271633
'no-index': bool_option,
1634+
'no-index-entry': bool_option,
16281635
'inherited-members': inherited_members_option,
16291636
'show-inheritance': bool_option,
16301637
'member-order': member_order_option,

sphinx/ext/autodoc/directive.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
AUTODOC_DEFAULT_OPTIONS = [
3131
'members',
3232
'undoc-members',
33+
'no-index',
34+
'no-index-entry',
3335
'inherited-members',
3436
'show-inheritance',
3537
'private-members',

tests/test_extensions/test_ext_autodoc.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,3 +3180,32 @@ def function_rst(name, sig):
31803180
*function_rst('bar', 'x: typing.Literal[1234]'),
31813181
*function_rst('foo', 'x: typing.Literal[target.literal.MyEnum.a]'),
31823182
]
3183+
3184+
3185+
@pytest.mark.sphinx('html', testroot='ext-autodoc')
3186+
def test_no_index_entry(app):
3187+
# modules can use no-index-entry
3188+
options = {'no-index-entry': None}
3189+
actual = do_autodoc(app, 'module', 'target.module', options)
3190+
assert ' :no-index-entry:' in list(actual)
3191+
3192+
# classes can use no-index-entry
3193+
actual = do_autodoc(app, 'class', 'target.classes.Foo', options)
3194+
assert ' :no-index-entry:' in list(actual)
3195+
3196+
# functions can use no-index-entry
3197+
actual = do_autodoc(app, 'function', 'target.functions.func', options)
3198+
assert ' :no-index-entry:' in list(actual)
3199+
3200+
# modules respect no-index-entry in autodoc_default_options
3201+
app.config.autodoc_default_options = {'no-index-entry': True}
3202+
actual = do_autodoc(app, 'module', 'target.module')
3203+
assert ' :no-index-entry:' in list(actual)
3204+
3205+
# classes respect config-level no-index-entry
3206+
actual = do_autodoc(app, 'class', 'target.classes.Foo')
3207+
assert ' :no-index-entry:' in list(actual)
3208+
3209+
# functions respect config-level no-index-entry
3210+
actual = do_autodoc(app, 'function', 'target.functions.func')
3211+
assert ' :no-index-entry:' in list(actual)

0 commit comments

Comments
 (0)