Skip to content

Commit bb96d0f

Browse files
authored
Improve error messages for see also parsing (#306)
* More informative error message for see also parse error. * Improve _error_location method output. * TST: Update test suite. * CI: Update sphinx==1.6.5 job from Python 3.5 to 3.6
1 parent 180a924 commit bb96d0f

File tree

4 files changed

+18
-32
lines changed

4 files changed

+18
-32
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- python: 3.7
2323
- python: 3.6
2424
env: SPHINX_SPEC="==2.1.0" SPHINXOPTS=""
25-
- python: 3.5
25+
- python: 3.6
2626
env: SPHINX_SPEC="==1.6.5" SPHINXOPTS=""
2727

2828
before_install:

numpydoc/docscrape.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def parse_item_name(text):
294294
"""Match ':role:`name`' or 'name'."""
295295
m = self._func_rgx.match(text)
296296
if not m:
297-
raise ParseError("%s is not a item name" % text)
297+
self._error_location(f"Error parsing See Also entry {line!r}")
298298
role = m.group('role')
299299
name = m.group('name') if role else m.group('name2')
300300
return name, role, m.end()
@@ -329,7 +329,7 @@ def parse_item_name(text):
329329
rest = list(filter(None, [description]))
330330
items.append((funcs, rest))
331331
else:
332-
raise ParseError("%s is not a item name" % line)
332+
self._error_location(f"Error parsing See Also entry {line!r}")
333333
return items
334334

335335
def _parse_index(self, section, content):
@@ -418,8 +418,8 @@ def _error_location(self, msg, error=True):
418418
filename = inspect.getsourcefile(self._obj)
419419
except TypeError:
420420
filename = None
421-
msg = msg + (" in the docstring of %s in %s."
422-
% (self._obj, filename))
421+
msg += f" in the docstring of {self._obj.__name__}"
422+
msg += f" in {filename}." if filename else ""
423423
if error:
424424
raise ValueError(msg)
425425
else:

numpydoc/tests/test_docscrape.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ def test_section_twice():
295295
-----
296296
That should break...
297297
"""
298-
assert_raises(ValueError, NumpyDocString, doc_text)
298+
with pytest.raises(ValueError, match="The section Notes appears twice"):
299+
NumpyDocString(doc_text)
299300

300301
# if we have a numpydoc object, we know where the error came from
301302
class Dummy:
@@ -332,19 +333,11 @@ def dummy_func(arg):
332333
Second note.
333334
"""
334335

335-
try:
336+
with pytest.raises(ValueError, match="Dummy class"):
336337
SphinxClassDoc(Dummy)
337-
except ValueError as e:
338-
# python 3 version or python 2 version
339-
assert ("test_section_twice.<locals>.Dummy" in str(e)
340-
or 'test_docscrape.Dummy' in str(e))
341338

342-
try:
339+
with pytest.raises(ValueError, match="dummy_func"):
343340
SphinxFunctionDoc(dummy_func)
344-
except ValueError as e:
345-
# python 3 version or python 2 version
346-
assert ("test_section_twice.<locals>.dummy_func" in str(e)
347-
or 'function dummy_func' in str(e))
348341

349342

350343
def test_notes(doc):
@@ -842,13 +835,9 @@ def test_see_also_parse_error():
842835
--------
843836
:func:`~foo`
844837
""")
845-
with assert_raises(ParseError) as err:
838+
with pytest.raises(ValueError, match="See Also entry ':func:`~foo`'"):
846839
NumpyDocString(text)
847840

848-
s1 = str(r":func:`~foo` is not a item name in '\n z(x,theta)\n\n See Also\n --------\n :func:`~foo`\n '")
849-
s2 = str(err.value)
850-
assert s1 == s2
851-
852841

853842
def test_see_also_print():
854843
class Dummy:
@@ -901,19 +890,16 @@ class BadSection:
901890
"""
902891
pass
903892

904-
with warnings.catch_warnings(record=True) as w:
905-
warnings.filterwarnings('always', '', UserWarning)
893+
with pytest.warns(UserWarning, match="Unknown section Mope") as record:
906894
NumpyDocString(doc_text)
907-
assert len(w) == 1
908-
assert "Unknown section Mope" == str(w[0].message)
895+
assert len(record) == 1
909896

910-
with warnings.catch_warnings(record=True) as w:
911-
warnings.filterwarnings('always', '', UserWarning)
897+
# SphinxClassDoc has _obj.__name__ == "BadSection". Test that this is
898+
# included in the message
899+
msg_match = "Unknown section Nope in the docstring of BadSection"
900+
with pytest.warns(UserWarning, match=msg_match) as record:
912901
SphinxClassDoc(BadSection)
913-
assert len(w) == 1
914-
assert('test_docscrape.test_unknown_section.<locals>.BadSection'
915-
in str(w[0].message)
916-
or 'test_docscrape.BadSection' in str(w[0].message))
902+
assert len(record) == 1
917903

918904

919905
doc7 = NumpyDocString("""

numpydoc/tests/test_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_render_object_returns_correct_exit_status():
8787
'numpydoc.tests.test_main._capture_stdout')
8888
assert exit_status == 0
8989

90-
with pytest.raises(numpydoc.docscrape.ParseError):
90+
with pytest.raises(ValueError):
9191
numpydoc.__main__.render_object(
9292
'numpydoc.tests.test_main._invalid_docstring')
9393

0 commit comments

Comments
 (0)