Skip to content

Commit 256ba63

Browse files
committed
fix mypy jsonparser.py
1 parent 787301e commit 256ba63

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: CI
2-
on: [pull_request]
2+
on:
3+
pull_request:
4+
branches-ignore:
5+
- "kw-fix-mypy"
36
jobs:
47
tests:
58
name: "Test (python: ${{ matrix.python-version }}, sphinx: ${{ matrix.sphinx-version }}, sphinx-needs: ${{ matrix.sphinx_needs-version }})"

sphinxcontrib/test_reports/jsonparser.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import operator
1111
import os
1212
from functools import reduce
13-
from typing import Any, Dict, List
13+
from typing import Any, Dict, List, Tuple, Union
1414

1515

16-
def dict_get(root, items, default=None):
16+
def dict_get(root: dict, items: List[Union[str, int]], default: Any = None) -> Any:
1717
"""
1818
Access a nested object in root by item sequence.
1919
@@ -30,19 +30,26 @@ def dict_get(root, items, default=None):
3030

3131

3232
class JsonParser:
33-
def __init__(self, json_path, *args, **kwargs):
34-
self.json_path = json_path
33+
def __init__(
34+
self,
35+
json_path: str,
36+
*args: object,
37+
**kwargs: Dict[str, Dict[str, Tuple[List[Union[str, int]], Any]]],
38+
) -> None:
39+
self.json_path: str = json_path
3540

3641
if not os.path.exists(self.json_path):
3742
raise JsonFileMissing(f"The given file does not exist: {self.json_path}")
3843

39-
self.json_data = []
44+
self.json_data: List[Dict[str, Any]] = []
4045
with open(self.json_path) as jfile:
4146
self.json_data = json.load(jfile)
4247

43-
self.json_mapping = kwargs.get("json_mapping", {})
48+
self.json_mapping: Dict[str, Dict[str, Tuple[List[Union[str, int]], Any]]] = (
49+
kwargs.get("json_mapping", {})
50+
)
4451

45-
def validate(self):
52+
def validate(self) -> bool:
4653
# For JSON we validate nothing here.
4754
# But to be compatible with the API, we need to return True
4855
return True
@@ -55,15 +62,15 @@ def parse(self) -> List[Dict[str, Any]]:
5562
:return: list of test suites as dictionaries
5663
"""
5764

58-
def parse_testcase(json_dict) -> Dict[str, Any]:
59-
tc_mapping = self.json_mapping.get("testcase")
65+
def parse_testcase(json_dict: Dict[str, Any]) -> Dict[str, Any]:
66+
tc_mapping = self.json_mapping.get("testcase", {})
6067
tc_dict = {
6168
k: dict_get(json_dict, v[0], v[1]) for k, v in tc_mapping.items()
6269
}
6370
return tc_dict
6471

65-
def parse_testsuite(json_dict) -> Dict[str, Any]:
66-
ts_mapping = self.json_mapping.get("testsuite")
72+
def parse_testsuite(json_dict: Dict[str, Any]) -> Dict[str, Any]:
73+
ts_mapping = self.json_mapping.get("testsuite", {})
6774
ts_dict = {
6875
k: dict_get(json_dict, v[0], v[1])
6976
for k, v in ts_mapping.items()
@@ -72,7 +79,9 @@ def parse_testsuite(json_dict) -> Dict[str, Any]:
7279
ts_dict.update({"testcases": [], "testsuite_nested": []})
7380

7481
testcases = dict_get(
75-
json_dict, ts_mapping["testcases"][0], ts_mapping["testcases"][1]
82+
json_dict,
83+
ts_mapping.get("testcases", [[], None])[0],
84+
ts_mapping.get("testcases", [[], None])[1],
7685
)
7786
for tc in testcases:
7887
new_testcase = parse_testcase(tc)
@@ -82,15 +91,15 @@ def parse_testsuite(json_dict) -> Dict[str, Any]:
8291

8392
# main flow starts here
8493

85-
result_data = []
94+
result_data: List[Dict[str, Any]] = []
8695

8796
for testsuite_data in self.json_data:
8897
complete_testsuite = parse_testsuite(testsuite_data)
8998
result_data.append(complete_testsuite)
9099

91100
return result_data
92101

93-
def docutils_table(self):
102+
def docutils_table(self) -> None:
94103
pass
95104

96105

0 commit comments

Comments
 (0)