Restore and deprecate the str interface to the asset classes

This commit is contained in:
Adam Turner
2023-08-17 15:40:34 +01:00
parent ff15b6f3b2
commit ab71cc5156
2 changed files with 26 additions and 0 deletions

View File

@@ -16,6 +16,10 @@ Features added
Bugs fixed
----------
* Restored the the :py:class:`str` interface of the asset classes
(``_CascadingStyleSheet`` and ``_JavaScript``), which several extensions relied upon.
This will be removed in Sphinx 9.
Testing
-------

View File

@@ -1,9 +1,11 @@
from __future__ import annotations
import os
import warnings
import zlib
from typing import TYPE_CHECKING
from sphinx.deprecation import RemovedInSphinx90Warning
from sphinx.errors import ThemeError
if TYPE_CHECKING:
@@ -51,6 +53,16 @@ class _CascadingStyleSheet:
msg = f'{self.__class__.__name__} is immutable'
raise AttributeError(msg)
def __getattr__(self, key):
warnings.warn('The str interface for _CascadingStyleSheet objects is deprecated. '
'Use css.filename instead.', RemovedInSphinx90Warning, stacklevel=2)
return getattr(os.fspath(self.filename), key)
def __getitem__(self, key):
warnings.warn('The str interface for _CascadingStyleSheet objects is deprecated. '
'Use css.filename instead.', RemovedInSphinx90Warning, stacklevel=2)
return os.fspath(self.filename)[key]
class _JavaScript:
filename: str | os.PathLike[str]
@@ -93,6 +105,16 @@ class _JavaScript:
msg = f'{self.__class__.__name__} is immutable'
raise AttributeError(msg)
def __getattr__(self, key):
warnings.warn('The str interface for _JavaScript objects is deprecated. '
'Use js.filename instead.', RemovedInSphinx90Warning, stacklevel=2)
return getattr(os.fspath(self.filename), key)
def __getitem__(self, key):
warnings.warn('The str interface for _JavaScript objects is deprecated. '
'Use js.filename instead.', RemovedInSphinx90Warning, stacklevel=2)
return os.fspath(self.filename)[key]
def _file_checksum(outdir: Path, filename: str | os.PathLike[str]) -> str:
filename = os.fspath(filename)