Skip to content

Commit 2e05fd2

Browse files
authored
[cleanup] remove deprecated objects and improve deprecation private interface (#12185)
This removes the deprecated objects that should have been removed in 7.x.
1 parent 0ef96a7 commit 2e05fd2

File tree

7 files changed

+42
-65
lines changed

7 files changed

+42
-65
lines changed

sphinx/addnodes.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,6 @@
1414
from sphinx.application import Sphinx
1515
from sphinx.util.typing import ExtensionMetadata
1616

17-
# deprecated name -> (object to return, canonical path or empty string)
18-
_DEPRECATED_OBJECTS = {
19-
'meta': (nodes.meta, 'docutils.nodes.meta'),
20-
'docutils_meta': (nodes.meta, 'docutils.nodes.meta'),
21-
}
22-
23-
24-
def __getattr__(name: str) -> Any:
25-
if name not in _DEPRECATED_OBJECTS:
26-
msg = f'module {__name__!r} has no attribute {name!r}'
27-
raise AttributeError(msg)
28-
29-
from sphinx.deprecation import _deprecation_warning
30-
31-
deprecated_object, canonical_name = _DEPRECATED_OBJECTS[name]
32-
_deprecation_warning(__name__, name, canonical_name, remove=(7, 0))
33-
return deprecated_object
34-
3517

3618
class document(nodes.document):
3719
"""The document root element patched by Sphinx.

sphinx/builders/html/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ def init_highlighter(self) -> None:
307307

308308
@property
309309
def css_files(self) -> list[_CascadingStyleSheet]:
310-
_deprecation_warning(__name__, f'{self.__class__.__name__}.css_files', '',
311-
remove=(9, 0))
310+
_deprecation_warning(__name__, f'{self.__class__.__name__}.css_files', remove=(9, 0))
312311
return self._css_files
313312

314313
def init_css_files(self) -> None:
@@ -334,8 +333,8 @@ def add_css_file(self, filename: str, **kwargs: Any) -> None:
334333

335334
@property
336335
def script_files(self) -> list[_JavaScript]:
337-
_deprecation_warning(__name__, f'{self.__class__.__name__}.script_files', '',
338-
remove=(9, 0))
336+
canonical_name = f'{self.__class__.__name__}.script_files'
337+
_deprecation_warning(__name__, canonical_name, remove=(9, 0))
339338
return self._js_files
340339

341340
def init_js_files(self) -> None:
@@ -1340,7 +1339,8 @@ def setup(app: Sphinx) -> ExtensionMetadata:
13401339
app.add_config_value('html_search_scorer', '', '')
13411340
app.add_config_value('html_scaled_image_link', True, 'html')
13421341
app.add_config_value('html_baseurl', '', 'html')
1343-
app.add_config_value('html_codeblock_linenos_style', 'inline', 'html', # RemovedInSphinx70Warning # NoQA: E501
1342+
# removal is indefinitely on hold (ref: https://github.com/sphinx-doc/sphinx/issues/10265)
1343+
app.add_config_value('html_codeblock_linenos_style', 'inline', 'html',
13441344
ENUM('table', 'inline'))
13451345
app.add_config_value('html_math_renderer', None, 'env')
13461346
app.add_config_value('html4_writer', False, 'html')
@@ -1373,8 +1373,8 @@ def setup(app: Sphinx) -> ExtensionMetadata:
13731373
}
13741374

13751375

1376-
# deprecated name -> (object to return, canonical path or empty string)
1377-
_DEPRECATED_OBJECTS = {
1376+
# deprecated name -> (object to return, canonical path or empty string, removal version)
1377+
_DEPRECATED_OBJECTS: dict[str, tuple[Any, str, tuple[int, int]]] = {
13781378
'Stylesheet': (_CascadingStyleSheet, 'sphinx.builders.html._assets._CascadingStyleSheet', (9, 0)), # NoQA: E501
13791379
'JavaScript': (_JavaScript, 'sphinx.builders.html._assets._JavaScript', (9, 0)),
13801380
}

sphinx/deprecation.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,26 @@ class RemovedInSphinx90Warning(PendingDeprecationWarning):
1919
def _deprecation_warning(
2020
module: str,
2121
attribute: str,
22-
canonical_name: str,
22+
canonical_name: str = '',
2323
*,
2424
remove: tuple[int, int],
25+
raises: bool = False,
2526
) -> None:
26-
"""Helper function for module-level deprecations using __getattr__
27+
"""Helper function for module-level deprecations using ``__getattr__``.
2728
28-
Exemplar usage:
29+
:param module: The module containing a deprecated object.
30+
:param attribute: The name of the deprecated object.
31+
:param canonical_name: Optional fully-qualified name for its replacement.
32+
:param remove: Target version for removal.
33+
:param raises: Indicate whether to raise an exception instead of a warning.
2934
30-
.. code:: python
35+
When *raises* is ``True``, an :exc:`AttributeError` is raised instead
36+
of emitting a warning so that it is easy to locate deprecated objects
37+
in tests that could suppress deprecation warnings.
3138
32-
# deprecated name -> (object to return, canonical path or empty string)
39+
Usage::
40+
41+
# deprecated name -> (object to return, canonical path or empty string, removal version)
3342
_DEPRECATED_OBJECTS = {
3443
'deprecated_name': (object_to_return, 'fully_qualified_replacement_name', (8, 0)),
3544
}
@@ -54,14 +63,14 @@ def __getattr__(name: str) -> Any:
5463
msg = f'removal version {remove!r} is invalid!'
5564
raise RuntimeError(msg)
5665

57-
qualified_name = f'{module}.{attribute}'
66+
qualname = f'{module}.{attribute}'
5867
if canonical_name:
59-
message = (
60-
f'The alias {qualified_name!r} is deprecated, use {canonical_name!r} instead.'
61-
)
68+
message = f'The alias {qualname!r} is deprecated, use {canonical_name!r} instead.'
6269
else:
63-
message = f'{qualified_name!r} is deprecated.'
70+
message = f'{qualname!r} is deprecated.'
71+
72+
if raises:
73+
raise AttributeError(message)
6474

65-
warnings.warn(
66-
message + ' Check CHANGES for Sphinx API modifications.', warning_class, stacklevel=3
67-
)
75+
message = f'{message} Check CHANGES for Sphinx API modifications.'
76+
warnings.warn(message, warning_class, stacklevel=3)

sphinx/testing/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
if TYPE_CHECKING:
2525
from collections.abc import Mapping
2626
from pathlib import Path
27-
from typing import Any, Final
27+
from typing import Any
2828
from xml.etree.ElementTree import ElementTree
2929

3030
from docutils.nodes import Node
@@ -242,7 +242,8 @@ def _clean_up_global_state() -> None:
242242
sphinx.pycode.ModuleAnalyzer.cache.clear()
243243

244244

245-
_DEPRECATED_OBJECTS: Final[dict[str, tuple[object, str, tuple[int, int]]]] = {
245+
# deprecated name -> (object to return, canonical path or '', removal version)
246+
_DEPRECATED_OBJECTS: dict[str, tuple[Any, str, tuple[int, int]]] = {
246247
'strip_escseq': (strip_colors, 'sphinx.util.console.strip_colors', (9, 0)),
247248
}
248249

sphinx/util/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def _xml_name_checker() -> re.Pattern[str]:
266266

267267

268268
# deprecated name -> (object to return, canonical path or empty string)
269-
_DEPRECATED_OBJECTS = {
269+
_DEPRECATED_OBJECTS: dict[str, tuple[Any, str] | tuple[Any, str, tuple[int, int]]] = {
270270
'path_stabilize': (_osutil.path_stabilize, 'sphinx.util.osutil.path_stabilize'),
271271
'display_chunk': (_display.display_chunk, 'sphinx.util.display.display_chunk'),
272272
'status_iterator': (_display.status_iterator, 'sphinx.util.display.status_iterator'),
@@ -294,6 +294,8 @@ def __getattr__(name: str) -> Any:
294294

295295
from sphinx.deprecation import _deprecation_warning
296296

297-
deprecated_object, canonical_name = _DEPRECATED_OBJECTS[name]
298-
_deprecation_warning(__name__, name, canonical_name, remove=(8, 0))
297+
info = _DEPRECATED_OBJECTS[name]
298+
deprecated_object, canonical_name = info[:2]
299+
remove = info[2] if len(info) == 3 else (8, 0)
300+
_deprecation_warning(__name__, name, canonical_name, remove=remove)
299301
return deprecated_object

sphinx/util/docutils.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,6 @@
3838
from sphinx.environment import BuildEnvironment
3939
from sphinx.util.typing import RoleFunction
4040

41-
# deprecated name -> (object to return, canonical path or empty string)
42-
_DEPRECATED_OBJECTS = {
43-
'__version_info__': (docutils.__version_info__, 'docutils.__version_info__'),
44-
}
45-
46-
47-
def __getattr__(name: str) -> Any:
48-
if name not in _DEPRECATED_OBJECTS:
49-
msg = f'module {__name__!r} has no attribute {name!r}'
50-
raise AttributeError(msg)
51-
52-
from sphinx.deprecation import _deprecation_warning
53-
54-
deprecated_object, canonical_name = _DEPRECATED_OBJECTS[name]
55-
_deprecation_warning(__name__, name, canonical_name, remove=(7, 0))
56-
return deprecated_object
57-
5841

5942
additional_nodes: set[type[Element]] = set()
6043

sphinx/util/typing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ def _format_literal_enum_arg(arg: enum.Enum, /, *, mode: str) -> str:
434434
return f':py:attr:`{enum_cls.__module__}.{enum_cls.__qualname__}.{arg.name}`'
435435

436436

437-
# deprecated name -> (object to return, canonical path or empty string)
438-
_DEPRECATED_OBJECTS = {
439-
'stringify': (stringify_annotation, 'sphinx.util.typing.stringify_annotation'),
437+
# deprecated name -> (object to return, canonical path or empty string, removal version)
438+
_DEPRECATED_OBJECTS: dict[str, tuple[Any, str, tuple[int, int]]] = {
439+
'stringify': (stringify_annotation, 'sphinx.util.typing.stringify_annotation', (8, 0)),
440440
}
441441

442442

@@ -447,6 +447,6 @@ def __getattr__(name: str) -> Any:
447447

448448
from sphinx.deprecation import _deprecation_warning
449449

450-
deprecated_object, canonical_name = _DEPRECATED_OBJECTS[name]
451-
_deprecation_warning(__name__, name, canonical_name, remove=(8, 0))
450+
deprecated_object, canonical_name, remove = _DEPRECATED_OBJECTS[name]
451+
_deprecation_warning(__name__, name, canonical_name, remove=remove)
452452
return deprecated_object

0 commit comments

Comments
 (0)