From b62628f678fccad2ba0a77959a2e5a22ad0b7eff Mon Sep 17 00:00:00 2001 From: Zac-HD Date: Thu, 8 Feb 2018 00:29:51 +1100 Subject: [PATCH] Avoid misreporting line number --- sphinx/ext/doctest.py | 22 ++++++++++++++++++++-- tests/test_ext_doctest.py | 4 ++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/sphinx/ext/doctest.py b/sphinx/ext/doctest.py index 4b244b52d..2a889900d 100644 --- a/sphinx/ext/doctest.py +++ b/sphinx/ext/doctest.py @@ -380,6 +380,23 @@ Doctest summary return filename.encode(fs_encoding) return filename + @staticmethod + def get_line_number(node): + # type: (nodes.Node) -> Optional[int] + """Get the real line number or admit we don't know.""" + # TODO: Work out how to store or calculate real (file-relative) + # line numbers for doctest blocks in docstrings. + if ':docstring of ' in path.basename(node.source or ''): + # The line number is given relative to the stripped docstring, + # not the file. This is correct where it is set, in + # `docutils.nodes.Node.setup_child`, but Sphinx should report + # relative to the file, not the docstring. + return None + if node.line is not None: + # TODO: find the root cause of this off by one error. + return node.line - 1 + return None + def test_doc(self, docname, doctree): # type: (unicode, nodes.Node) -> None groups = {} # type: Dict[unicode, TestGroup] @@ -408,12 +425,13 @@ Doctest summary for node in doctree.traverse(condition): source = node['test'] if 'test' in node else node.astext() filename = self.get_filename_for_node(node, docname) + line_number = self.get_line_number(node) if not source: logger.warning('no code/output in %s block at %s:%s', node.get('testnodetype', 'doctest'), - filename, node.line) + filename, line_number) code = TestCode(source, type=node.get('testnodetype', 'doctest'), - filename=filename, lineno=node.line, + filename=filename, lineno=line_number, options=node.get('options')) node_groups = node.get('groups', ['default']) if '*' in node_groups: diff --git a/tests/test_ext_doctest.py b/tests/test_ext_doctest.py index 24a0bc1cd..482e60d53 100644 --- a/tests/test_ext_doctest.py +++ b/tests/test_ext_doctest.py @@ -71,8 +71,8 @@ def test_reporting_with_autodoc(app, status, warning, capfd): failures = [l for l in lines if l.startswith('File')] expected = [ 'File "dir/inner.rst", line 1, in default', - 'File "dir/bar.py", line 2, in default', - 'File "foo.py", line 3, in default', + 'File "dir/bar.py", line ?, in default', + 'File "foo.py", line ?, in default', 'File "index.rst", line 4, in default', ] for location in expected: