summaryrefslogtreecommitdiff
path: root/sphinx/deprecation.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2019-01-03 00:38:23 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2019-01-03 10:54:39 +0900
commit4938b71bcab570382901481f5624e50e68f89b43 (patch)
tree6b68dd1361d43533c090490b16b27e27b44f043f /sphinx/deprecation.py
parent860d7ab5062d348ac78dd984045356ea2bb33bb5 (diff)
downloadsphinx-git-4938b71bcab570382901481f5624e50e68f89b43.tar.gz
refactor: Add compat module to avoid recursive import
Diffstat (limited to 'sphinx/deprecation.py')
-rw-r--r--sphinx/deprecation.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/sphinx/deprecation.py b/sphinx/deprecation.py
index bc346bff3..ea4099a93 100644
--- a/sphinx/deprecation.py
+++ b/sphinx/deprecation.py
@@ -8,7 +8,9 @@
:license: BSD, see LICENSE for details.
"""
+import sys
import warnings
+from importlib import import_module
if False:
# For type annotation
@@ -26,6 +28,34 @@ class RemovedInSphinx40Warning(PendingDeprecationWarning):
RemovedInNextVersionWarning = RemovedInSphinx30Warning
+def deprecated_alias(modname, objects, warning):
+ # type: (str, Dict, Type[Warning]) -> None
+ module = sys.modules.get(modname)
+ if module is None:
+ module = import_module(modname)
+
+ sys.modules[modname] = _ModuleWrapper(module, modname, objects, warning) # type: ignore
+
+
+class _ModuleWrapper(object):
+ def __init__(self, module, modname, objects, warning):
+ # type: (Any, str, Dict, Type[Warning]) -> None
+ self._module = module
+ self._modname = modname
+ self._objects = objects
+ self._warning = warning
+
+ def __getattr__(self, name):
+ # type: (str) -> Any
+ if name in self._objects:
+ warnings.warn("%s.%s is now deprecated. Please refer CHANGES to grasp"
+ "the changes of Sphinx API." % (self._modname, name),
+ self._warning, stacklevel=3)
+ return self._objects[name]
+
+ return getattr(self._module, name)
+
+
class DeprecatedDict(dict):
"""A deprecated dict which warns on each access."""