Skip to content

Commit

Permalink
[cleanup] remove deprecated objects and improve deprecation private i…
Browse files Browse the repository at this point in the history
…nterface (sphinx-doc#12185)

This removes the deprecated objects that should have been removed in 7.x.
  • Loading branch information
picnixz committed Mar 25, 2024
1 parent 0ef96a7 commit 2e05fd2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 65 deletions.
18 changes: 0 additions & 18 deletions sphinx/addnodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,6 @@
from sphinx.application import Sphinx
from sphinx.util.typing import ExtensionMetadata

# deprecated name -> (object to return, canonical path or empty string)
_DEPRECATED_OBJECTS = {
'meta': (nodes.meta, 'docutils.nodes.meta'),
'docutils_meta': (nodes.meta, 'docutils.nodes.meta'),
}


def __getattr__(name: str) -> Any:
if name not in _DEPRECATED_OBJECTS:
msg = f'module {__name__!r} has no attribute {name!r}'
raise AttributeError(msg)

from sphinx.deprecation import _deprecation_warning

deprecated_object, canonical_name = _DEPRECATED_OBJECTS[name]
_deprecation_warning(__name__, name, canonical_name, remove=(7, 0))
return deprecated_object


class document(nodes.document):
"""The document root element patched by Sphinx.
Expand Down
14 changes: 7 additions & 7 deletions sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ def init_highlighter(self) -> None:

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

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

@property
def script_files(self) -> list[_JavaScript]:
_deprecation_warning(__name__, f'{self.__class__.__name__}.script_files', '',
remove=(9, 0))
canonical_name = f'{self.__class__.__name__}.script_files'
_deprecation_warning(__name__, canonical_name, remove=(9, 0))
return self._js_files

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


# deprecated name -> (object to return, canonical path or empty string)
_DEPRECATED_OBJECTS = {
# deprecated name -> (object to return, canonical path or empty string, removal version)
_DEPRECATED_OBJECTS: dict[str, tuple[Any, str, tuple[int, int]]] = {
'Stylesheet': (_CascadingStyleSheet, 'sphinx.builders.html._assets._CascadingStyleSheet', (9, 0)), # NoQA: E501
'JavaScript': (_JavaScript, 'sphinx.builders.html._assets._JavaScript', (9, 0)),
}
Expand Down
35 changes: 22 additions & 13 deletions sphinx/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,26 @@ class RemovedInSphinx90Warning(PendingDeprecationWarning):
def _deprecation_warning(
module: str,
attribute: str,
canonical_name: str,
canonical_name: str = '',
*,
remove: tuple[int, int],
raises: bool = False,
) -> None:
"""Helper function for module-level deprecations using __getattr__
"""Helper function for module-level deprecations using ``__getattr__``.
Exemplar usage:
:param module: The module containing a deprecated object.
:param attribute: The name of the deprecated object.
:param canonical_name: Optional fully-qualified name for its replacement.
:param remove: Target version for removal.
:param raises: Indicate whether to raise an exception instead of a warning.
.. code:: python
When *raises* is ``True``, an :exc:`AttributeError` is raised instead
of emitting a warning so that it is easy to locate deprecated objects
in tests that could suppress deprecation warnings.
# deprecated name -> (object to return, canonical path or empty string)
Usage::
# deprecated name -> (object to return, canonical path or empty string, removal version)
_DEPRECATED_OBJECTS = {
'deprecated_name': (object_to_return, 'fully_qualified_replacement_name', (8, 0)),
}
Expand All @@ -54,14 +63,14 @@ def __getattr__(name: str) -> Any:
msg = f'removal version {remove!r} is invalid!'
raise RuntimeError(msg)

qualified_name = f'{module}.{attribute}'
qualname = f'{module}.{attribute}'
if canonical_name:
message = (
f'The alias {qualified_name!r} is deprecated, use {canonical_name!r} instead.'
)
message = f'The alias {qualname!r} is deprecated, use {canonical_name!r} instead.'
else:
message = f'{qualified_name!r} is deprecated.'
message = f'{qualname!r} is deprecated.'

if raises:
raise AttributeError(message)

warnings.warn(
message + ' Check CHANGES for Sphinx API modifications.', warning_class, stacklevel=3
)
message = f'{message} Check CHANGES for Sphinx API modifications.'
warnings.warn(message, warning_class, stacklevel=3)
5 changes: 3 additions & 2 deletions sphinx/testing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
if TYPE_CHECKING:
from collections.abc import Mapping
from pathlib import Path
from typing import Any, Final
from typing import Any
from xml.etree.ElementTree import ElementTree

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


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

Expand Down
8 changes: 5 additions & 3 deletions sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _xml_name_checker() -> re.Pattern[str]:


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

from sphinx.deprecation import _deprecation_warning

deprecated_object, canonical_name = _DEPRECATED_OBJECTS[name]
_deprecation_warning(__name__, name, canonical_name, remove=(8, 0))
info = _DEPRECATED_OBJECTS[name]
deprecated_object, canonical_name = info[:2]
remove = info[2] if len(info) == 3 else (8, 0)
_deprecation_warning(__name__, name, canonical_name, remove=remove)
return deprecated_object
17 changes: 0 additions & 17 deletions sphinx/util/docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,6 @@
from sphinx.environment import BuildEnvironment
from sphinx.util.typing import RoleFunction

# deprecated name -> (object to return, canonical path or empty string)
_DEPRECATED_OBJECTS = {
'__version_info__': (docutils.__version_info__, 'docutils.__version_info__'),
}


def __getattr__(name: str) -> Any:
if name not in _DEPRECATED_OBJECTS:
msg = f'module {__name__!r} has no attribute {name!r}'
raise AttributeError(msg)

from sphinx.deprecation import _deprecation_warning

deprecated_object, canonical_name = _DEPRECATED_OBJECTS[name]
_deprecation_warning(__name__, name, canonical_name, remove=(7, 0))
return deprecated_object


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

Expand Down
10 changes: 5 additions & 5 deletions sphinx/util/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ def _format_literal_enum_arg(arg: enum.Enum, /, *, mode: str) -> str:
return f':py:attr:`{enum_cls.__module__}.{enum_cls.__qualname__}.{arg.name}`'


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


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

from sphinx.deprecation import _deprecation_warning

deprecated_object, canonical_name = _DEPRECATED_OBJECTS[name]
_deprecation_warning(__name__, name, canonical_name, remove=(8, 0))
deprecated_object, canonical_name, remove = _DEPRECATED_OBJECTS[name]
_deprecation_warning(__name__, name, canonical_name, remove=remove)
return deprecated_object

0 comments on commit 2e05fd2

Please sign in to comment.