|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import warnings |
3 | 4 | from typing import TYPE_CHECKING |
4 | 5 |
|
5 | 6 | from docutils.utils import assemble_option_dict |
6 | 7 |
|
| 8 | +from sphinx.deprecation import RemovedInSphinx11Warning |
7 | 9 | from sphinx.ext.autodoc._sentinels import ALL, EMPTY, SUPPRESS |
8 | 10 | from sphinx.locale import __ |
9 | 11 |
|
10 | 12 | if TYPE_CHECKING: |
11 | | - from collections.abc import Mapping, Set |
| 13 | + from collections.abc import Iterable, Iterator, Mapping, Set |
12 | 14 | from typing import Any, Final, Literal, Self |
13 | 15 |
|
14 | 16 | from sphinx.ext.autodoc._property_types import _AutodocObjType |
@@ -90,6 +92,99 @@ def copy(self) -> Self: |
90 | 92 | def from_directive_options(cls, opts: Mapping[str, Any], /) -> Self: |
91 | 93 | return cls(**{k.replace('-', '_'): v for k, v in opts.items() if v is not None}) |
92 | 94 |
|
| 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 | + |
93 | 188 |
|
94 | 189 | def identity(x: Any) -> Any: |
95 | 190 | return x |
|
0 commit comments