mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
autodoc: Use logging module to emit warnings
This commit is contained in:
parent
4173a0bfe1
commit
7a194f5296
2
CHANGES
2
CHANGES
@ -13,6 +13,8 @@ Incompatible changes
|
||||
* #3929: apidoc: Move sphinx.apidoc to sphinx.ext.apidoc
|
||||
* #4226: apidoc: Generate new style makefile (make-mode)
|
||||
* #4274: sphinx-build returns 2 as an exit code on argument error
|
||||
* autodoc does not generate warnings messages to the generated document even if
|
||||
:confval:`keep_warnings` is True. They are only emitted to stderr.
|
||||
|
||||
Deprecated
|
||||
----------
|
||||
|
@ -354,8 +354,7 @@ class Documenter(object):
|
||||
explicit_modname, path, base, args, retann = \
|
||||
py_ext_sig_re.match(self.name).groups() # type: ignore
|
||||
except AttributeError:
|
||||
self.directive.warn('invalid signature for auto%s (%r)' %
|
||||
(self.objtype, self.name))
|
||||
logger.warning('invalid signature for auto%s (%r)' % (self.objtype, self.name))
|
||||
return False
|
||||
|
||||
# support explicit module and class name separation via ::
|
||||
@ -432,8 +431,7 @@ class Documenter(object):
|
||||
|
||||
if PY2:
|
||||
errmsg = errmsg.decode('utf-8') # type: ignore
|
||||
logger.debug(errmsg)
|
||||
self.directive.warn(errmsg)
|
||||
logger.warning(errmsg)
|
||||
self.env.note_reread()
|
||||
return False
|
||||
|
||||
@ -493,8 +491,8 @@ class Documenter(object):
|
||||
try:
|
||||
args = self.format_args()
|
||||
except Exception as err:
|
||||
self.directive.warn('error while formatting arguments for '
|
||||
'%s: %s' % (self.fullname, err))
|
||||
logger.warning('error while formatting arguments for %s: %s' %
|
||||
(self.fullname, err))
|
||||
args = None
|
||||
|
||||
retann = self.retann
|
||||
@ -623,8 +621,8 @@ class Documenter(object):
|
||||
members.append((mname, self.get_attr(self.object, mname)))
|
||||
except AttributeError:
|
||||
if mname not in analyzed_member_names:
|
||||
self.directive.warn('missing attribute %s in object %s'
|
||||
% (mname, self.fullname))
|
||||
logger.warning('missing attribute %s in object %s' %
|
||||
(mname, self.fullname))
|
||||
elif self.options.inherited_members:
|
||||
# safe_getmembers() uses dir() which pulls in members from all
|
||||
# base classes
|
||||
@ -820,11 +818,11 @@ class Documenter(object):
|
||||
"""
|
||||
if not self.parse_name():
|
||||
# need a module to import
|
||||
self.directive.warn(
|
||||
logger.warning(
|
||||
'don\'t know which module to import for autodocumenting '
|
||||
'%r (try placing a "module" or "currentmodule" directive '
|
||||
'in the document, or giving an explicit module name)'
|
||||
% self.name)
|
||||
'in the document, or giving an explicit module name)' %
|
||||
self.name)
|
||||
return
|
||||
|
||||
# now, import the module and get object to document
|
||||
@ -910,15 +908,15 @@ class ModuleDocumenter(Documenter):
|
||||
def resolve_name(self, modname, parents, path, base):
|
||||
# type: (str, Any, str, Any) -> Tuple[str, List[unicode]]
|
||||
if modname is not None:
|
||||
self.directive.warn('"::" in automodule name doesn\'t make sense')
|
||||
logger.warning('"::" in automodule name doesn\'t make sense')
|
||||
return (path or '') + base, []
|
||||
|
||||
def parse_name(self):
|
||||
# type: () -> bool
|
||||
ret = Documenter.parse_name(self)
|
||||
if self.args or self.retann:
|
||||
self.directive.warn('signature arguments or return annotation '
|
||||
'given for automodule %s' % self.fullname)
|
||||
logger.warning('signature arguments or return annotation '
|
||||
'given for automodule %s' % self.fullname)
|
||||
return ret
|
||||
|
||||
def add_directive_header(self, sig):
|
||||
@ -949,7 +947,7 @@ class ModuleDocumenter(Documenter):
|
||||
# Sometimes __all__ is broken...
|
||||
if not isinstance(memberlist, (list, tuple)) or not \
|
||||
all(isinstance(entry, string_types) for entry in memberlist):
|
||||
self.directive.warn(
|
||||
logger.warning(
|
||||
'__all__ should be a list of strings, not %r '
|
||||
'(in module %s) -- ignoring __all__' %
|
||||
(memberlist, self.fullname))
|
||||
@ -962,10 +960,10 @@ class ModuleDocumenter(Documenter):
|
||||
try:
|
||||
ret.append((mname, safe_getattr(self.object, mname)))
|
||||
except AttributeError:
|
||||
self.directive.warn(
|
||||
logger.warning(
|
||||
'missing attribute mentioned in :members: or __all__: '
|
||||
'module %s, attribute %s' % (
|
||||
safe_getattr(self.object, '__name__', '???'), mname))
|
||||
'module %s, attribute %s' %
|
||||
(safe_getattr(self.object, '__name__', '???'), mname))
|
||||
return False, ret
|
||||
|
||||
|
||||
@ -1542,7 +1540,7 @@ class AutoDirective(Directive):
|
||||
|
||||
def warn(self, msg):
|
||||
# type: (unicode) -> None
|
||||
self.warnings.append(self.reporter.warning(msg, line=self.lineno))
|
||||
logger.warning(msg, line=self.lineno)
|
||||
|
||||
def run(self):
|
||||
# type: () -> List[nodes.Node]
|
||||
@ -1550,7 +1548,6 @@ class AutoDirective(Directive):
|
||||
# a set of dependent filenames
|
||||
self.reporter = self.state.document.reporter
|
||||
self.env = self.state.document.settings.env
|
||||
self.warnings = [] # type: List[unicode]
|
||||
self.result = ViewList()
|
||||
|
||||
try:
|
||||
@ -1585,7 +1582,7 @@ class AutoDirective(Directive):
|
||||
documenter = doc_class(self, self.arguments[0])
|
||||
documenter.generate(more_content=self.content)
|
||||
if not self.result:
|
||||
return self.warnings
|
||||
return []
|
||||
|
||||
logger.debug('[autodoc] output:\n%s', '\n'.join(self.result))
|
||||
|
||||
@ -1610,7 +1607,7 @@ class AutoDirective(Directive):
|
||||
node.document = self.state.document
|
||||
self.state.nested_parse(self.result, 0, node)
|
||||
self.state.memo.reporter = old_reporter
|
||||
return self.warnings + node.children
|
||||
return node.children
|
||||
|
||||
|
||||
def add_documenter(cls):
|
||||
|
@ -21,6 +21,7 @@ from docutils.statemachine import ViewList
|
||||
|
||||
from sphinx.ext.autodoc import AutoDirective, add_documenter, \
|
||||
ModuleLevelDocumenter, FunctionDocumenter, cut_lines, between, ALL
|
||||
from sphinx.util import logging
|
||||
|
||||
app = None
|
||||
|
||||
@ -30,7 +31,7 @@ def setup_module(rootdir, sphinx_test_tempdir):
|
||||
global app
|
||||
srcdir = sphinx_test_tempdir / 'autodoc-root'
|
||||
if not srcdir.exists():
|
||||
(rootdir/'test-root').copytree(srcdir)
|
||||
(rootdir / 'test-root').copytree(srcdir)
|
||||
app = SphinxTestApp(srcdir=srcdir)
|
||||
app.builder.env.app = app
|
||||
app.builder.env.temp_data['docname'] = 'dummy'
|
||||
@ -47,7 +48,7 @@ directive = options = None
|
||||
@pytest.fixture
|
||||
def setup_test():
|
||||
global options, directive
|
||||
global processed_docstrings, processed_signatures, _warnings
|
||||
global processed_docstrings, processed_signatures
|
||||
|
||||
options = Struct(
|
||||
inherited_members = False,
|
||||
@ -70,24 +71,17 @@ def setup_test():
|
||||
env = app.builder.env,
|
||||
genopt = options,
|
||||
result = ViewList(),
|
||||
warn = warnfunc,
|
||||
filename_set = set(),
|
||||
)
|
||||
|
||||
processed_docstrings = []
|
||||
processed_signatures = []
|
||||
_warnings = []
|
||||
|
||||
|
||||
_warnings = []
|
||||
processed_docstrings = []
|
||||
processed_signatures = []
|
||||
|
||||
|
||||
def warnfunc(msg):
|
||||
_warnings.append(msg)
|
||||
|
||||
|
||||
def process_docstring(app, what, name, obj, options, lines):
|
||||
processed_docstrings.append((what, name))
|
||||
if name == 'bar':
|
||||
@ -111,20 +105,21 @@ def skip_member(app, what, name, obj, skip, options):
|
||||
|
||||
@pytest.mark.usefixtures('setup_test')
|
||||
def test_generate():
|
||||
logging.setup(app, app._status, app._warning)
|
||||
|
||||
def assert_warns(warn_str, objtype, name, **kw):
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
inst.generate(**kw)
|
||||
assert len(directive.result) == 0, directive.result
|
||||
assert len(_warnings) == 1, _warnings
|
||||
assert warn_str in _warnings[0], _warnings
|
||||
del _warnings[:]
|
||||
assert warn_str in app._warning.getvalue()
|
||||
app._warning.truncate(0)
|
||||
|
||||
def assert_works(objtype, name, **kw):
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
inst.generate(**kw)
|
||||
assert directive.result
|
||||
# print '\n'.join(directive.result)
|
||||
assert len(_warnings) == 0, _warnings
|
||||
assert app._warning.getvalue() == ''
|
||||
del directive.result[:]
|
||||
|
||||
def assert_processes(items, objtype, name, **kw):
|
||||
@ -137,7 +132,7 @@ def test_generate():
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
inst.generate(**kw)
|
||||
# print '\n'.join(directive.result)
|
||||
assert len(_warnings) == 0, _warnings
|
||||
assert app._warning.getvalue() == ''
|
||||
assert item in directive.result
|
||||
del directive.result[:]
|
||||
|
||||
@ -145,7 +140,7 @@ def test_generate():
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
inst.options.member_order = member_order
|
||||
inst.generate(**kw)
|
||||
assert len(_warnings) == 0, _warnings
|
||||
assert app._warning.getvalue() == ''
|
||||
items = list(reversed(items))
|
||||
lineiter = iter(directive.result)
|
||||
# for line in directive.result:
|
||||
|
@ -21,6 +21,7 @@ from docutils.statemachine import ViewList
|
||||
|
||||
from sphinx.ext.autodoc import AutoDirective, add_documenter, \
|
||||
ModuleLevelDocumenter, FunctionDocumenter, cut_lines, between, ALL
|
||||
from sphinx.util import logging
|
||||
|
||||
app = None
|
||||
|
||||
@ -47,7 +48,7 @@ directive = options = None
|
||||
@pytest.fixture
|
||||
def setup_test():
|
||||
global options, directive
|
||||
global processed_docstrings, processed_signatures, _warnings
|
||||
global processed_docstrings, processed_signatures
|
||||
|
||||
options = Struct(
|
||||
inherited_members = False,
|
||||
@ -70,28 +71,24 @@ def setup_test():
|
||||
env = app.builder.env,
|
||||
genopt = options,
|
||||
result = ViewList(),
|
||||
warn = warnfunc,
|
||||
filename_set = set(),
|
||||
)
|
||||
|
||||
processed_docstrings = []
|
||||
processed_signatures = []
|
||||
_warnings = []
|
||||
|
||||
app._status.truncate(0)
|
||||
app._warning.truncate(0)
|
||||
|
||||
yield
|
||||
|
||||
AutoDirective._special_attrgetters.clear()
|
||||
|
||||
|
||||
_warnings = []
|
||||
processed_docstrings = []
|
||||
processed_signatures = []
|
||||
|
||||
|
||||
def warnfunc(msg):
|
||||
_warnings.append(msg)
|
||||
|
||||
|
||||
def process_docstring(app, what, name, obj, options, lines):
|
||||
processed_docstrings.append((what, name))
|
||||
if name == 'bar':
|
||||
@ -115,6 +112,8 @@ def skip_member(app, what, name, obj, skip, options):
|
||||
|
||||
@pytest.mark.usefixtures('setup_test')
|
||||
def test_parse_name():
|
||||
logging.setup(app, app._status, app._warning)
|
||||
|
||||
def verify(objtype, name, result):
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
assert inst.parse_name()
|
||||
@ -124,8 +123,7 @@ def test_parse_name():
|
||||
verify('module', 'test_autodoc', ('test_autodoc', [], None, None))
|
||||
verify('module', 'test.test_autodoc', ('test.test_autodoc', [], None, None))
|
||||
verify('module', 'test(arg)', ('test', [], 'arg', None))
|
||||
assert 'signature arguments' in _warnings[0]
|
||||
del _warnings[:]
|
||||
assert 'signature arguments' in app._warning.getvalue()
|
||||
|
||||
# for functions/classes
|
||||
verify('function', 'test_autodoc.raises',
|
||||
@ -241,7 +239,6 @@ def test_format_signature():
|
||||
# test exception handling (exception is caught and args is '')
|
||||
directive.env.config.autodoc_docstring_signature = False
|
||||
assert formatsig('function', 'int', int, None, None) == ''
|
||||
del _warnings[:]
|
||||
|
||||
# test processing by event handler
|
||||
assert formatsig('method', 'bar', H.foo1, None, None) == '42'
|
||||
@ -533,6 +530,8 @@ def test_docstring_property_processing():
|
||||
|
||||
@pytest.mark.usefixtures('setup_test')
|
||||
def test_new_documenter():
|
||||
logging.setup(app, app._status, app._warning)
|
||||
|
||||
class MyDocumenter(ModuleLevelDocumenter):
|
||||
objtype = 'integer'
|
||||
directivetype = 'data'
|
||||
@ -548,10 +547,11 @@ def test_new_documenter():
|
||||
add_documenter(MyDocumenter)
|
||||
|
||||
def assert_result_contains(item, objtype, name, **kw):
|
||||
app._warning.truncate(0)
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
inst.generate(**kw)
|
||||
# print '\n'.join(directive.result)
|
||||
assert len(_warnings) == 0, _warnings
|
||||
assert app._warning.getvalue() == ''
|
||||
assert item in directive.result
|
||||
del directive.result[:]
|
||||
|
||||
@ -593,20 +593,22 @@ def test_attrgetter_using():
|
||||
|
||||
@pytest.mark.usefixtures('setup_test')
|
||||
def test_generate():
|
||||
logging.setup(app, app._status, app._warning)
|
||||
|
||||
def assert_warns(warn_str, objtype, name, **kw):
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
inst.generate(**kw)
|
||||
assert len(directive.result) == 0, directive.result
|
||||
assert len(_warnings) == 1, _warnings
|
||||
assert warn_str in _warnings[0], _warnings
|
||||
del _warnings[:]
|
||||
|
||||
assert warn_str in app._warning.getvalue()
|
||||
app._warning.truncate(0)
|
||||
|
||||
def assert_works(objtype, name, **kw):
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
inst.generate(**kw)
|
||||
assert directive.result
|
||||
# print '\n'.join(directive.result)
|
||||
assert len(_warnings) == 0, _warnings
|
||||
assert app._warning.getvalue() == ''
|
||||
del directive.result[:]
|
||||
|
||||
def assert_processes(items, objtype, name, **kw):
|
||||
@ -619,7 +621,7 @@ def test_generate():
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
inst.generate(**kw)
|
||||
# print '\n'.join(directive.result)
|
||||
assert len(_warnings) == 0, _warnings
|
||||
assert app._warning.getvalue() == ''
|
||||
assert item in directive.result
|
||||
del directive.result[:]
|
||||
|
||||
@ -627,7 +629,7 @@ def test_generate():
|
||||
inst = AutoDirective._registry[objtype](directive, name)
|
||||
inst.options.member_order = member_order
|
||||
inst.generate(**kw)
|
||||
assert len(_warnings) == 0, _warnings
|
||||
assert app._warning.getvalue() == ''
|
||||
items = list(reversed(items))
|
||||
lineiter = iter(directive.result)
|
||||
# for line in directive.result:
|
||||
|
Loading…
Reference in New Issue
Block a user