Skip to content

Commit 2ac430c

Browse files
authored
Fix autodoc event handler in sphinx.ext.napoleon (#14127)
1 parent 2502bfc commit 2ac430c

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

sphinx/ext/napoleon/docstring.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,13 @@ def __init__(
358358
self._what: _AutodocObjType | Literal['object'] = what
359359
self._name = name
360360
self._obj = obj
361-
self._opt = options
361+
if options:
362+
try:
363+
self._no_index = options.no_index or options.noindex
364+
except (AttributeError, TypeError):
365+
self._no_index = False
366+
else:
367+
self._no_index = False
362368
if isinstance(docstring, str):
363369
lines = docstring.splitlines()
364370
else:
@@ -877,9 +883,8 @@ def _parse_attributes_section(self, section: str) -> list[str]:
877883
lines.append(f':vartype {_name}: {_type}')
878884
else:
879885
lines.append('.. attribute:: ' + _name)
880-
if self._opt:
881-
if 'no-index' in self._opt or 'noindex' in self._opt:
882-
lines.append(' :no-index:')
886+
if self._no_index:
887+
lines.append(' :no-index:')
883888
lines.append('')
884889

885890
fields = self._format_field('', '', _desc)
@@ -945,9 +950,8 @@ def _parse_methods_section(self, section: str) -> list[str]:
945950
lines: list[str] = []
946951
for _name, _type, _desc in self._consume_fields(parse_type=False):
947952
lines.append(f'.. method:: {_name}')
948-
if self._opt:
949-
if 'no-index' in self._opt or 'noindex' in self._opt:
950-
lines.append(' :no-index:')
953+
if self._no_index:
954+
lines.append(' :no-index:')
951955
if _desc:
952956
lines.extend(['', *self._indent(_desc, 3)])
953957
lines.append('')

tests/test_ext_napoleon/test_ext_napoleon_docstring.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from inspect import cleandoc
99
from itertools import product
1010
from textwrap import dedent
11+
from types import SimpleNamespace
1112
from typing import TYPE_CHECKING
1213
from unittest import mock
1314

@@ -1238,7 +1239,7 @@ def test_custom_generic_sections(self):
12381239
actual = GoogleDocstring(docstring, test_config)
12391240
assert str(actual) == expected
12401241

1241-
def test_noindex(self):
1242+
def test_no_index(self):
12421243
docstring = """
12431244
Attributes:
12441245
arg
@@ -1267,7 +1268,7 @@ def test_noindex(self):
12671268
config=config,
12681269
app=None,
12691270
what='module',
1270-
options={'no-index': True},
1271+
options=SimpleNamespace(no_index=True),
12711272
)
12721273
assert str(actual) == expected
12731274

0 commit comments

Comments
 (0)