Skip to content

Commit baffcf0

Browse files
authored
autodoc: Deprecate mapping interface for options (#14140)
1 parent 61f9b70 commit baffcf0

File tree

3 files changed

+106
-10
lines changed

3 files changed

+106
-10
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Release 9.0.1 (in development)
44
Bugs fixed
55
----------
66

7+
* #13942: autodoc: Restore the mapping interface for options objects.
8+
Patch by Adam Turner.
9+
* #13942: autodoc: Deprecate the mapping interface for options objects.
10+
Patch by Adam Turner.
711

812
Release 9.0.0 (released Nov 30, 2025)
913
=====================================

doc/usage/extensions/autodoc.rst

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,9 +1393,8 @@ autodoc provides the following additional events:
13931393
:param name: the fully qualified name of the object
13941394
:param obj: the object itself
13951395
:param options: the options given to the directive: an object with attributes
1396-
``inherited_members``, ``undoc_members``, ``show_inheritance`` and
1397-
``no-index`` that are true if the flag option of same name was given to the
1398-
auto directive
1396+
corresponding to the options used in the auto directive, e.g.
1397+
``inherited_members``, ``undoc_members``, or ``show_inheritance``.
13991398
:param lines: the lines of the docstring, see above
14001399

14011400
.. event:: autodoc-before-process-signature (app, obj, bound_method)
@@ -1424,9 +1423,8 @@ autodoc provides the following additional events:
14241423
:param name: the fully qualified name of the object
14251424
:param obj: the object itself
14261425
:param options: the options given to the directive: an object with attributes
1427-
``inherited_members``, ``undoc_members``, ``show_inheritance`` and
1428-
``no-index`` that are true if the flag option of same name was given to the
1429-
auto directive
1426+
corresponding to the options used in the auto directive, e.g.
1427+
``inherited_members``, ``undoc_members``, or ``show_inheritance``.
14301428
:param signature: function signature, as a string of the form
14311429
``'(parameter_1, parameter_2)'``, or ``None`` if introspection didn't
14321430
succeed and signature wasn't specified in the directive.
@@ -1487,6 +1485,5 @@ member should be included in the documentation by using the following event:
14871485
:param skip: a boolean indicating if autodoc will skip this member if the
14881486
user handler does not override the decision
14891487
:param options: the options given to the directive: an object with attributes
1490-
``inherited_members``, ``undoc_members``, ``show_inheritance`` and
1491-
``no-index`` that are true if the flag option of same name was given to the
1492-
auto directive
1488+
corresponding to the options used in the auto directive, e.g.
1489+
``inherited_members``, ``undoc_members``, or ``show_inheritance``.

sphinx/ext/autodoc/_directive_options.py

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
from __future__ import annotations
22

3+
import warnings
34
from typing import TYPE_CHECKING
45

56
from docutils.utils import assemble_option_dict
67

8+
from sphinx.deprecation import RemovedInSphinx11Warning
79
from sphinx.ext.autodoc._sentinels import ALL, EMPTY, SUPPRESS
810
from sphinx.locale import __
911

1012
if TYPE_CHECKING:
11-
from collections.abc import Mapping, Set
13+
from collections.abc import Iterable, Iterator, Mapping, Set
1214
from typing import Any, Final, Literal, Self
1315

1416
from sphinx.ext.autodoc._property_types import _AutodocObjType
@@ -90,6 +92,99 @@ def copy(self) -> Self:
9092
def from_directive_options(cls, opts: Mapping[str, Any], /) -> Self:
9193
return cls(**{k.replace('-', '_'): v for k, v in opts.items() if v is not None})
9294

95+
# Mapping interface:
96+
97+
def __getitem__(self, item: str) -> Any:
98+
warnings.warn(
99+
'The mapping interface for autodoc options objects is deprecated, '
100+
'and will be removed in Sphinx 11. Use attribute access instead.',
101+
RemovedInSphinx11Warning,
102+
stacklevel=2,
103+
)
104+
try:
105+
return getattr(self, item)
106+
except AttributeError:
107+
raise KeyError(item) from None
108+
109+
def __setitem__(self, key: str, value: Any) -> None:
110+
msg = f'{self.__class__.__name__!r} object does not support indexed assignment'
111+
raise TypeError(msg)
112+
113+
def __delitem__(self, key: str) -> None:
114+
msg = f'{self.__class__.__name__!r} object does not support indexed deletion'
115+
raise TypeError(msg)
116+
117+
def __contains__(self, item: str) -> bool:
118+
warnings.warn(
119+
'The mapping interface for autodoc options objects is deprecated, '
120+
'and will be removed in Sphinx 11. Use attribute access instead.',
121+
RemovedInSphinx11Warning,
122+
stacklevel=2,
123+
)
124+
return hasattr(self, item)
125+
126+
def __keys(self) -> list[str]:
127+
return [key for key in dir(self) if not key.startswith('_')]
128+
129+
def __iter__(self) -> Iterator[str]:
130+
warnings.warn(
131+
'The mapping interface for autodoc options objects is deprecated, '
132+
'and will be removed in Sphinx 11. Use attribute access instead.',
133+
RemovedInSphinx11Warning,
134+
stacklevel=2,
135+
)
136+
yield from self.__keys()
137+
138+
def __len__(self) -> int:
139+
warnings.warn(
140+
'The mapping interface for autodoc options objects is deprecated, '
141+
'and will be removed in Sphinx 11. Use attribute access instead.',
142+
RemovedInSphinx11Warning,
143+
stacklevel=2,
144+
)
145+
return len(self.__keys())
146+
147+
def keys(self) -> Iterable[str]:
148+
warnings.warn(
149+
'The mapping interface for autodoc options objects is deprecated, '
150+
'and will be removed in Sphinx 11. Use attribute access instead.',
151+
RemovedInSphinx11Warning,
152+
stacklevel=2,
153+
)
154+
yield from self.__keys()
155+
156+
def items(self) -> Iterable[tuple[str, Any]]:
157+
warnings.warn(
158+
'The mapping interface for autodoc options objects is deprecated, '
159+
'and will be removed in Sphinx 11. Use attribute access instead.',
160+
RemovedInSphinx11Warning,
161+
stacklevel=2,
162+
)
163+
for key in self.__keys():
164+
yield key, getattr(self, key)
165+
166+
def values(self) -> Iterable[Any]:
167+
warnings.warn(
168+
'The mapping interface for autodoc options objects is deprecated, '
169+
'and will be removed in Sphinx 11. Use attribute access instead.',
170+
RemovedInSphinx11Warning,
171+
stacklevel=2,
172+
)
173+
for key in self.__keys():
174+
yield getattr(self, key)
175+
176+
def get(self, key: str, default: Any | None = None) -> Any | None:
177+
warnings.warn(
178+
'The mapping interface for autodoc options objects is deprecated, '
179+
'and will be removed in Sphinx 11. Use attribute access instead.',
180+
RemovedInSphinx11Warning,
181+
stacklevel=2,
182+
)
183+
try:
184+
return getattr(self, key)
185+
except AttributeError:
186+
return default
187+
93188

94189
def identity(x: Any) -> Any:
95190
return x

0 commit comments

Comments
 (0)