Show deprecation warning in directive_helper()

This commit is contained in:
Takeshi KOMIYA 2018-02-10 20:06:54 +09:00
parent 446eab49b8
commit 8b8836b6d2
4 changed files with 18 additions and 6 deletions

View File

@ -25,6 +25,7 @@ Deprecated
``sphinx.util.import_object()`` instead.
* Drop function based directive support. For now, Sphinx only supports class
based directives.
* ``sphinx.util.docutils.directive_helper()`` is deprecated.
Features added
--------------

View File

@ -703,13 +703,12 @@ class Sphinx(object):
'already registered, it will be overridden'),
self._setting_up_extension[-1], name,
type='app', subtype='add_directive')
directive = directive_helper(obj, content, arguments, **options)
directives.register_directive(name, directive)
if not isclass(obj) or not issubclass(obj, Directive):
warnings.warn('function based directive support is now deprecated. '
'Use class based directive instead.',
RemovedInSphinx30Warning)
directive = directive_helper(obj, content, arguments, **options)
directives.register_directive(name, directive)
else:
directives.register_directive(name, obj)
def add_role(self, name, role):
# type: (unicode, Any) -> None

View File

@ -12,9 +12,11 @@ from __future__ import print_function
import traceback
import warnings
from inspect import isclass
from types import MethodType
from typing import TYPE_CHECKING
from docutils.parsers.rst import Directive
from pkg_resources import iter_entry_points
from six import iteritems, itervalues
@ -186,8 +188,12 @@ class SphinxComponentRegistry(object):
(domain, name, obj, has_content, argument_spec, option_spec))
if domain not in self.domains:
raise ExtensionError(__('domain %s not yet registered') % domain)
directives = self.domain_directives.setdefault(domain, {})
directives[name] = directive_helper(obj, has_content, argument_spec, **option_spec)
if not isclass(obj) or not issubclass(obj, Directive):
directives[name] = directive_helper(obj, has_content, argument_spec, **option_spec)
else:
directives[name] = obj
def add_role_to_domain(self, domain, name, role):
# type: (unicode, unicode, Union[RoleFunction, XRefRole]) -> None

View File

@ -12,6 +12,7 @@ from __future__ import absolute_import
import re
import types
import warnings
from contextlib import contextmanager
from copy import copy
from distutils.version import LooseVersion
@ -24,6 +25,7 @@ from docutils.parsers.rst import directives, roles, convert_directive_function
from docutils.statemachine import StateMachine
from docutils.utils import Reporter
from sphinx.deprecation import RemovedInSphinx30Warning
from sphinx.errors import ExtensionError
from sphinx.locale import __
from sphinx.util import logging
@ -192,6 +194,10 @@ def is_html5_writer_available():
def directive_helper(obj, has_content=None, argument_spec=None, **option_spec):
# type: (Any, bool, Tuple[int, int, bool], Any) -> Any
warnings.warn('function based directive support is now deprecated. '
'Use class based directive instead.',
RemovedInSphinx30Warning)
if isinstance(obj, (types.FunctionType, types.MethodType)):
obj.content = has_content # type: ignore
obj.arguments = argument_spec or (0, 0, False) # type: ignore