Preserve backwards compatibility

This commit is contained in:
Peter Bell 2020-07-16 15:48:51 +01:00
parent ea0fbd21d5
commit 0000239776
17 changed files with 206 additions and 171 deletions

View File

@ -23,16 +23,18 @@ from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
deprecated_alias('sphinx.builders.applehelp',
{
'AppleHelpCodeSigningFailed': (
'sphinxcontrib.applehelp.AppleHelpCodeSigningFailed',
AppleHelpCodeSigningFailed),
'AppleHelpIndexerFailed': (
'sphinxcontrib.applehelp.AppleHelpIndexerFailed',
AppleHelpIndexerFailed),
'AppleHelpBuilder': (
'sphinxcontrib.applehelp.AppleHelpBuilder', AppleHelpBuilder),
'AppleHelpCodeSigningFailed': AppleHelpCodeSigningFailed,
'AppleHelpIndexerFailed': AppleHelpIndexerFailed,
'AppleHelpBuilder': AppleHelpBuilder,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'AppleHelpCodeSigningFailed':
'sphinxcontrib.applehelp.AppleHelpCodeSigningFailed',
'AppleHelpIndexerFailed':
'sphinxcontrib.applehelp.AppleHelpIndexerFailed',
'AppleHelpBuilder': 'sphinxcontrib.applehelp.AppleHelpBuilder',
})
def setup(app: Sphinx) -> Dict[str, Any]:

View File

@ -21,10 +21,12 @@ from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
deprecated_alias('sphinx.builders.devhelp',
{
'DevhelpBuilder': (
'sphinxcontrib.devhelp.DevhelpBuilder', DevhelpBuilder),
'DevhelpBuilder': DevhelpBuilder,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'DevhelpBuilder': 'sphinxcontrib.devhelp.DevhelpBuilder'
})
def setup(app: Sphinx) -> Dict[str, Any]:

View File

@ -49,10 +49,12 @@ class DirectoryHTMLBuilder(StandaloneHTMLBuilder):
# for compatibility
deprecated_alias('sphinx.builders.html',
{
'DirectoryHTMLBuilder': (
'sphinx.builders.dirhtml.DirectoryHTMLBuilder', DirectoryHTMLBuilder),
'DirectoryHTMLBuilder': DirectoryHTMLBuilder,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'DirectoryHTMLBuilder': 'sphinx.builders.dirhtml.DirectoryHTMLBuilder',
})
def setup(app: Sphinx) -> Dict[str, Any]:

View File

@ -22,16 +22,19 @@ from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
deprecated_alias('sphinx.builders.htmlhelp',
{
'chm_locales': ('sphinxcontrib.htmlhelp.chm_locales', chm_locales),
'chm_htmlescape': (
'sphinxcontrib.htmlhelp.chm_htmlescape', chm_htmlescape),
'HTMLHelpBuilder': (
'sphinxcontrib.htmlhelp.HTMLHelpBuilder', HTMLHelpBuilder),
'default_htmlhelp_basename': (
'sphinxcontrib.htmlhelp.default_htmlhelp_basename',
default_htmlhelp_basename),
'chm_locales': chm_locales,
'chm_htmlescape': chm_htmlescape,
'HTMLHelpBuilder': HTMLHelpBuilder,
'default_htmlhelp_basename': default_htmlhelp_basename,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'chm_locales': 'sphinxcontrib.htmlhelp.chm_locales',
'chm_htmlescape': 'sphinxcontrib.htmlhelp.chm_htmlescape',
'HTMLHelpBuilder': 'sphinxcontrib.htmlhelp.HTMLHelpBuilder',
'default_htmlhelp_basename':
'sphinxcontrib.htmlhelp.default_htmlhelp_basename',
})
def setup(app: Sphinx) -> Dict[str, Any]:

View File

@ -20,10 +20,14 @@ from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
deprecated_alias('sphinx.builders.qthelp',
{
'render_file': ('sphinxcontrib.qthelp.render_file', render_file),
'QtHelpBuilder': ('sphinxcontrib.qthelp.QtHelpBuilder', QtHelpBuilder),
'render_file': render_file,
'QtHelpBuilder': QtHelpBuilder,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'render_file': 'sphinxcontrib.qthelp.render_file',
'QtHelpBuilder': 'sphinxcontrib.qthelp.QtHelpBuilder',
})
def setup(app: Sphinx) -> Dict[str, Any]:

View File

@ -191,11 +191,13 @@ class SingleFileHTMLBuilder(StandaloneHTMLBuilder):
# for compatibility
deprecated_alias('sphinx.builders.html',
{
'SingleFileHTMLBuilder': (
'sphinx.builders.singlehtml.SingleFileHTMLBuilder',
SingleFileHTMLBuilder),
'SingleFileHTMLBuilder': SingleFileHTMLBuilder,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'SingleFileHTMLBuilder':
'sphinx.builders.singlehtml.SingleFileHTMLBuilder',
})
def setup(app: Sphinx) -> Dict[str, Any]:

View File

@ -11,7 +11,7 @@
import sys
import warnings
from importlib import import_module
from typing import Any, Dict, Tuple
from typing import Any, Dict
if False:
# For type annotation
@ -29,62 +29,39 @@ class RemovedInSphinx50Warning(PendingDeprecationWarning):
RemovedInNextVersionWarning = RemovedInSphinx40Warning
def deprecated_alias(modname: str, objects: Dict[str, Tuple[str, object]],
warning: "Type[Warning]") -> None:
def deprecated_alias(modname: str, objects: Dict[str, object],
warning: "Type[Warning]", names: Dict[str, str] = None) -> None:
module = import_module(modname)
sys.modules[modname] = _ModuleWrapperDeprecatedAlias( # type: ignore
module, modname, objects, warning)
sys.modules[modname] = _ModuleWrapper( # type: ignore
module, modname, objects, warning, names)
class _ModuleWrapperDeprecatedAlias:
def __init__(self, module: Any, modname: str,
aliases: Dict[str, Tuple[str, object]],
warning: "Type[Warning]") -> None:
if not all(isinstance(v, tuple) for v in aliases.values()):
raise TypeError('deprecated_alias expects aliases to be a dict mapping: '
'name -> tuple[canonical name, object]')
self._module = module
self._modname = modname
self._aliases = aliases
self._warning = warning
def __getattr__(self, name: str) -> Any:
if name in self._aliases:
canonical_name, obj = self._aliases[name]
warnings.warn(
"The alias '{}.{}' is deprecated, use '{}' instead. Check CHANGES for "
"Sphinx API modifications.".format(self._modname, name, canonical_name),
self._warning, stacklevel=3)
return obj
return getattr(self._module, name)
def deprecated_attribute(modname: str, objects: Dict[str, object],
warning: "Type[Warning]") -> None:
module = import_module(modname)
sys.modules[modname] = _ModuleWrapperDeprecatedAttr( # type: ignore
module, modname, objects, warning)
class _ModuleWrapperDeprecatedAttr:
class _ModuleWrapper:
def __init__(self, module: Any, modname: str,
objects: Dict[str, object],
warning: "Type[Warning]") -> None:
warning: "Type[Warning]",
names: Dict[str, str]) -> None:
self._module = module
self._modname = modname
self._objects = objects
self._warning = warning
self._names = names
def __getattr__(self, name: str) -> Any:
if name in self._objects:
if name not in self._objects:
return getattr(self._module, name)
canonical_name = self._names.get(name, None)
if canonical_name is not None:
warnings.warn(
"The alias '{}.{}' is deprecated, use '{}' instead. Check CHANGES for "
"Sphinx API modifications.".format(self._modname, name, canonical_name),
self._warning, stacklevel=3)
else:
warnings.warn("{}.{} is deprecated. Check CHANGES for Sphinx "
"API modifications.".format(self._modname, name),
self._warning, stacklevel=3)
return self._objects[name]
return getattr(self._module, name)
return self._objects[name]
class DeprecatedDict(dict):

View File

@ -280,34 +280,53 @@ from sphinx.domains.index import IndexDirective # noqa
deprecated_alias('sphinx.directives',
{
'Highlight': ('sphinx.directives.code.Highlight', Highlight),
'CodeBlock': ('sphinx.directives.code.CodeBlock', CodeBlock),
'LiteralInclude': (
'sphinx.directives.code.LiteralInclude', LiteralInclude),
'TocTree': ('sphinx.directives.other.TocTree', TocTree),
'Author': ('sphinx.directives.other.Author', Author),
'Index': ('sphinx.directives.other.IndexDirective', IndexDirective),
'VersionChange': ('sphinx.directives.other.VersionChange', VersionChange),
'SeeAlso': ('sphinx.directives.other.SeeAlso', SeeAlso),
'TabularColumns': (
'sphinx.directives.other.TabularColumns', TabularColumns),
'Centered': ('sphinx.directives.other.Centered', Centered),
'Acks': ('sphinx.directives.other.Acks', Acks),
'HList': ('sphinx.directives.other.HList', HList),
'Only': ('sphinx.directives.other.Only', Only),
'Include': ('sphinx.directives.other.Include', Include),
'Class': ('sphinx.directives.other.Class', Class),
'Figure': ('sphinx.directives.patches.Figure', Figure),
'Meta': ('sphinx.directives.patches.Meta', Meta),
'Highlight': Highlight,
'CodeBlock': CodeBlock,
'LiteralInclude': LiteralInclude,
'TocTree': TocTree,
'Author': Author,
'Index': IndexDirective,
'VersionChange': VersionChange,
'SeeAlso': SeeAlso,
'TabularColumns': TabularColumns,
'Centered': Centered,
'Acks': Acks,
'HList': HList,
'Only': Only,
'Include': Include,
'Class': Class,
'Figure': Figure,
'Meta': Meta,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'Highlight': 'sphinx.directives.code.Highlight',
'CodeBlock': 'sphinx.directives.code.CodeBlock',
'LiteralInclude': 'sphinx.directives.code.LiteralInclude',
'TocTree': 'sphinx.directives.other.TocTree',
'Author': 'sphinx.directives.other.Author',
'Index': 'sphinx.directives.other.IndexDirective',
'VersionChange': 'sphinx.directives.other.VersionChange',
'SeeAlso': 'sphinx.directives.other.SeeAlso',
'TabularColumns': 'sphinx.directives.other.TabularColumns',
'Centered': 'sphinx.directives.other.Centered',
'Acks': 'sphinx.directives.other.Acks',
'HList': 'sphinx.directives.other.HList',
'Only': 'sphinx.directives.other.Only',
'Include': 'sphinx.directives.other.Include',
'Class': 'sphinx.directives.other.Class',
'Figure': 'sphinx.directives.patches.Figure',
'Meta': 'sphinx.directives.patches.Meta',
})
deprecated_alias('sphinx.directives',
{
'DescDirective': (
'sphinx.directives.ObjectDescription', ObjectDescription),
'DescDirective': ObjectDescription,
},
RemovedInSphinx50Warning)
RemovedInSphinx50Warning,
{
'DescDirective': 'sphinx.directives.ObjectDescription',
})
def setup(app: "Sphinx") -> Dict[str, Any]:

View File

@ -366,9 +366,12 @@ from sphinx.domains.index import IndexDirective # NOQA
deprecated_alias('sphinx.directives.other',
{
'Index': ('sphinx.domains.index.IndexDirective', IndexDirective),
'Index': IndexDirective,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'Index': 'sphinx.domains.index.IndexDirective',
})
def setup(app: "Sphinx") -> Dict[str, Any]:

View File

@ -29,7 +29,7 @@ from typing import Any, List, Tuple
import sphinx.locale
from sphinx import __display_version__, package_dir
from sphinx.cmd.quickstart import EXTENSIONS
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_attribute
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
from sphinx.locale import __
from sphinx.util import rst
from sphinx.util.osutil import FileAvoidWrite, ensuredir
@ -516,11 +516,11 @@ def main(argv: List[str] = sys.argv[1:]) -> int:
return 0
deprecated_attribute('sphinx.ext.apidoc',
{
'INITPY': '__init__.py',
},
RemovedInSphinx40Warning)
deprecated_alias('sphinx.ext.apidoc',
{
'INITPY': '__init__.py',
},
RemovedInSphinx40Warning)
# So program can be started with "python -m sphinx.apidoc ..."

View File

@ -188,10 +188,17 @@ from sphinx.ext.autodoc.mock import ( # NOQA
deprecated_alias('sphinx.ext.autodoc.importer',
{
'_MockModule': ('sphinx.ext.autodoc.mock._MockModule', _MockModule),
'_MockObject': ('sphinx.ext.autodoc.mock._MockObject', _MockObject),
'MockFinder': ('sphinx.ext.autodoc.mock.MockFinder', MockFinder),
'MockLoader': ('sphinx.ext.autodoc.mock.MockLoader', MockLoader),
'mock': ('sphinx.ext.autodoc.mock.mock', mock),
'_MockModule': _MockModule,
'_MockObject': _MockObject,
'MockFinder': MockFinder,
'MockLoader': MockLoader,
'mock': mock,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'_MockModule': 'sphinx.ext.autodoc.mock._MockModule',
'_MockObject': 'sphinx.ext.autodoc.mock._MockObject',
'MockFinder': 'sphinx.ext.autodoc.mock.MockFinder',
'MockLoader': 'sphinx.ext.autodoc.mock.MockLoader',
'mock': 'sphinx.ext.autodoc.mock.mock',
})

View File

@ -26,7 +26,7 @@ from sphinx import package_dir
from sphinx.application import Sphinx
from sphinx.builders import Builder
from sphinx.config import Config
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_attribute
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
from sphinx.errors import SphinxError
from sphinx.locale import _, __
from sphinx.util import logging, sha1
@ -370,13 +370,13 @@ def html_visit_displaymath(self: HTMLTranslator, node: nodes.math_block) -> None
raise nodes.SkipNode
deprecated_attribute('sphinx.ext.imgmath',
{
'DOC_BODY': DOC_BODY,
'DOC_BODY_PREVIEW': DOC_BODY_PREVIEW,
'DOC_HEAD': DOC_HEAD,
},
RemovedInSphinx40Warning)
deprecated_alias('sphinx.ext.imgmath',
{
'DOC_BODY': DOC_BODY,
'DOC_BODY_PREVIEW': DOC_BODY_PREVIEW,
'DOC_HEAD': DOC_HEAD,
},
RemovedInSphinx40Warning)
def setup(app: Sphinx) -> Dict[str, Any]:

View File

@ -226,8 +226,11 @@ def read_doc(app: "Sphinx", env: BuildEnvironment, filename: str) -> nodes.docum
deprecated_alias('sphinx.io',
{
'FiletypeNotFoundError': (
'sphinx.errors.FiletypeNotFoundError', FiletypeNotFoundError),
'get_filetype': ('sphinx.util.get_filetype', get_filetype),
'FiletypeNotFoundError': FiletypeNotFoundError,
'get_filetype': get_filetype,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'FiletypeNotFoundError': 'sphinx.errors.FiletypeNotFoundError',
'get_filetype': 'sphinx.util.get_filetype',
})

View File

@ -407,14 +407,16 @@ from sphinx.domains.citation import ( # NOQA
deprecated_alias('sphinx.transforms',
{
'CitationReferences': (
'sphinx.domains.citation.CitationReferenceTransform',
CitationReferenceTransform),
'SmartQuotesSkipper': (
'sphinx.domains.citation.CitationDefinitionTransform',
CitationDefinitionTransform),
'CitationReferences': CitationReferenceTransform,
'SmartQuotesSkipper': CitationDefinitionTransform,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'CitationReferences':
'sphinx.domains.citation.CitationReferenceTransform',
'SmartQuotesSkipper':
'sphinx.domains.citation.CitationDefinitionTransform',
})
def setup(app: "Sphinx") -> Dict[str, Any]:

View File

@ -15,8 +15,7 @@ import textwrap
import warnings
from typing import Any, Callable
from sphinx.deprecation import (
RemovedInSphinx40Warning, deprecated_alias, deprecated_attribute)
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import terminal_safe
@ -92,13 +91,20 @@ def execfile_(filepath: str, _globals: Any, open: Callable = open) -> None:
deprecated_alias('sphinx.util.pycompat',
{
'NoneType': ('sphinx.util.typing.NoneType', NoneType),
'TextIOWrapper': ('io.TextIOWrapper', io.TextIOWrapper),
'htmlescape': ('html.escape', html.escape),
'indent': ('textwrap.indent', textwrap.indent),
'terminal_safe': ('sphinx.util.console.terminal_safe', terminal_safe),
'sys_encoding': ('sys.getdefaultencoding', sys.getdefaultencoding()),
'NoneType': NoneType,
'TextIOWrapper': io.TextIOWrapper,
'htmlescape': html.escape,
'indent': textwrap.indent,
'terminal_safe': terminal_safe,
'sys_encoding': sys.getdefaultencoding(),
'u': '',
},
RemovedInSphinx40Warning)
deprecated_attribute('sphinx.util.pycompat', {'u': ''}, RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'NoneType': 'sphinx.util.typing.NoneType',
'TextIOWrapper': 'io.TextIOWrapper',
'htmlescape': 'html.escape',
'indent': 'textwrap.indent',
'terminal_safe': 'sphinx.util.console.terminal_safe',
'sys_encoding': 'sys.getdefaultencoding',
})

View File

@ -11,7 +11,7 @@
import re
from typing import Dict
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_attribute
from sphinx.deprecation import RemovedInSphinx40Warning, deprecated_alias
tex_replacements = [
@ -109,12 +109,12 @@ _tex_hlescape_map = {} # type: Dict[int, str]
_tex_hlescape_map_without_unicode = {} # type: Dict[int, str]
deprecated_attribute('sphinx.util.texescape',
{
'tex_escape_map': _tex_escape_map,
'tex_hl_escape_map_new': _tex_hlescape_map,
},
RemovedInSphinx40Warning)
deprecated_alias('sphinx.util.texescape',
{
'tex_escape_map': _tex_escape_map,
'tex_hl_escape_map_new': _tex_hlescape_map,
},
RemovedInSphinx40Warning)
def escape(s: str, latex_engine: str = None) -> str:

View File

@ -2131,30 +2131,33 @@ from sphinx.builders.latex.util import ExtBabel # NOQA
deprecated_alias('sphinx.writers.latex',
{
'ADDITIONAL_SETTINGS': (
'sphinx.builders.latex.constants.ADDITIONAL_SETTINGS',
constants.ADDITIONAL_SETTINGS),
'DEFAULT_SETTINGS': (
'sphinx.builders.latex.constants.DEFAULT_SETTINGS',
constants.DEFAULT_SETTINGS),
'LUALATEX_DEFAULT_FONTPKG': (
'sphinx.builders.latex.constants.LUALATEX_DEFAULT_FONTPKG',
constants.LUALATEX_DEFAULT_FONTPKG),
'PDFLATEX_DEFAULT_FONTPKG': (
'sphinx.builders.latex.constants.PDFLATEX_DEFAULT_FONTPKG',
constants.PDFLATEX_DEFAULT_FONTPKG),
'SHORTHANDOFF': (
'sphinx.builders.latex.constants.SHORTHANDOFF',
constants.SHORTHANDOFF),
'XELATEX_DEFAULT_FONTPKG': (
'sphinx.builders.latex.constants.XELATEX_DEFAULT_FONTPKG',
constants.XELATEX_DEFAULT_FONTPKG),
'XELATEX_GREEK_DEFAULT_FONTPKG': (
'sphinx.builders.latex.constants.XELATEX_GREEK_DEFAULT_FONTPKG',
constants.XELATEX_GREEK_DEFAULT_FONTPKG),
'ExtBabel': ('sphinx.builders.latex.util.ExtBabel', ExtBabel),
'ADDITIONAL_SETTINGS': constants.ADDITIONAL_SETTINGS,
'DEFAULT_SETTINGS': constants.DEFAULT_SETTINGS,
'LUALATEX_DEFAULT_FONTPKG': constants.LUALATEX_DEFAULT_FONTPKG,
'PDFLATEX_DEFAULT_FONTPKG': constants.PDFLATEX_DEFAULT_FONTPKG,
'SHORTHANDOFF': constants.SHORTHANDOFF,
'XELATEX_DEFAULT_FONTPKG': constants.XELATEX_DEFAULT_FONTPKG,
'XELATEX_GREEK_DEFAULT_FONTPKG': constants.XELATEX_GREEK_DEFAULT_FONTPKG,
'ExtBabel': ExtBabel,
},
RemovedInSphinx40Warning)
RemovedInSphinx40Warning,
{
'ADDITIONAL_SETTINGS':
'sphinx.builders.latex.constants.ADDITIONAL_SETTINGS',
'DEFAULT_SETTINGS':
'sphinx.builders.latex.constants.DEFAULT_SETTINGS',
'LUALATEX_DEFAULT_FONTPKG':
'sphinx.builders.latex.constants.LUALATEX_DEFAULT_FONTPKG',
'PDFLATEX_DEFAULT_FONTPKG':
'sphinx.builders.latex.constants.PDFLATEX_DEFAULT_FONTPKG',
'SHORTHANDOFF':
'sphinx.builders.latex.constants.SHORTHANDOFF',
'XELATEX_DEFAULT_FONTPKG':
'sphinx.builders.latex.constants.XELATEX_DEFAULT_FONTPKG',
'XELATEX_GREEK_DEFAULT_FONTPKG':
'sphinx.builders.latex.constants.XELATEX_GREEK_DEFAULT_FONTPKG',
'ExtBabel': 'sphinx.builders.latex.util.ExtBabel',
})
# FIXME: Workaround to avoid circular import
# refs: https://github.com/sphinx-doc/sphinx/issues/5433