Skip to content

Commit 0e7ab59

Browse files
fix: Handle empty error in JSON output
PR #1: #1 Co-authored-by: Timothée Mazzucotelli <[email protected]>
1 parent 196b484 commit 0e7ab59

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/mkdocstrings/handlers/python/collector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ def collect(self, identifier: str, config: dict) -> CollectorItem:
152152
error = "\n".join(("Error while loading JSON:", stdout, traceback.format_exc()))
153153
raise CollectionError(error) from exception
154154

155-
error = result.get("error")
156-
if error:
155+
if "error" in result:
156+
error = result["error"]
157157
if "traceback" in result:
158158
error += f"\n{result['traceback']}"
159159
raise CollectionError(error)

tests/test_collector.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
"""Tests for the `collector` module."""
3+
from unittest import mock
4+
5+
import pytest
6+
7+
from mkdocstrings.handlers.base import CollectionError
8+
from mkdocstrings.handlers.python import collector
9+
10+
11+
def test_init():
12+
"""Test init for collector.PythonCollector."""
13+
assert collector.PythonCollector()
14+
15+
16+
@pytest.mark.parametrize(
17+
("retval", "exp_res"),
18+
[
19+
({"error": "error1", "traceback": "hello"}, "error1\nhello"),
20+
({"error": "error1"}, "error1"),
21+
({"error": "", "traceback": "hello"}, "\nhello"),
22+
],
23+
)
24+
def test_collect_result_error(retval, exp_res):
25+
"""Test handling of errors when collecting an object.
26+
27+
Args:
28+
retval: Return value to mock `json.loads` with.
29+
exp_res: Expected result.
30+
"""
31+
with mock.patch("mkdocstrings.handlers.python.collector.json.loads") as m_loads:
32+
with pytest.raises(CollectionError) as excinfo: # noqa: PT012
33+
m_loads.return_value = retval
34+
obj = collector.PythonCollector()
35+
assert obj.collect("", {})
36+
assert str(excinfo.value) == exp_res

0 commit comments

Comments
 (0)