Skip to content

Commit 100d78b

Browse files
thseilerstanislaw
authored andcommitted
code-climate: mypy: address type-arg
1 parent 5de8475 commit 100d78b

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

strictdoc/export/json/json_generator.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# mypy: disable-error-code="arg-type,attr-defined,no-untyped-def,type-arg,union-attr,operator"
1+
# mypy: disable-error-code="arg-type,attr-defined,no-untyped-def,union-attr,operator"
22
import json
33
import os.path
44
from enum import Enum
5-
from typing import Any, Dict, List, Optional, Tuple
5+
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
66

77
from strictdoc.backend.sdoc.models.document import SDocDocument
88
from strictdoc.backend.sdoc.models.document_config import DocumentConfig
@@ -17,11 +17,17 @@
1717
)
1818
from strictdoc.backend.sdoc.models.section import SDocSection
1919
from strictdoc.backend.sdoc.models.type_system import (
20+
GrammarElementRelationChild,
21+
GrammarElementRelationFile,
22+
GrammarElementRelationParent,
2023
RequirementFieldType,
2124
)
2225
from strictdoc.core.project_config import ProjectConfig
2326
from strictdoc.core.traceability_index import TraceabilityIndex
2427

28+
JSONPrimitive = Union[str, int, float, bool, None]
29+
JSONType = Union[JSONPrimitive, Dict[str, "JSONType"], Sequence["JSONType"]]
30+
2531

2632
class TAG(Enum):
2733
SECTION = 1
@@ -55,7 +61,7 @@ def export_tree(
5561
if document_.document_is_included():
5662
continue
5763

58-
document_json_dict = self._write_document(document_)
64+
document_json_dict: JSONType = self._write_document(document_)
5965

6066
project_tree_dict["DOCUMENTS"].append(document_json_dict)
6167

@@ -65,7 +71,7 @@ def export_tree(
6571
output_json_file.write(project_tree_json)
6672

6773
@classmethod
68-
def _write_document(cls, document: SDocDocument) -> Dict:
74+
def _write_document(cls, document: SDocDocument) -> Dict[str, JSONType]:
6975
document_dict: Dict[str, Any] = {
7076
"TITLE": document.title,
7177
"REQ_PREFIX": None,
@@ -160,7 +166,13 @@ def _write_document(cls, document: SDocDocument) -> Dict:
160166
cls._write_grammar_field_type(grammar_field)
161167
)
162168

163-
relations: List = element_.relations
169+
relations: List[
170+
Union[
171+
GrammarElementRelationParent,
172+
GrammarElementRelationChild,
173+
GrammarElementRelationFile,
174+
]
175+
] = element_.relations
164176
if len(relations) > 0:
165177
for element_relation_ in relations:
166178
relation_dict = {
@@ -178,7 +190,9 @@ def _write_document(cls, document: SDocDocument) -> Dict:
178190
return document_dict
179191

180192
@classmethod
181-
def _write_node(cls, node, document, level_stack: Optional[Tuple]) -> Dict:
193+
def _write_node(
194+
cls, node, document, level_stack: Optional[Tuple[int, ...]]
195+
) -> Dict[str, JSONType]:
182196
def get_level_string_(node_) -> str:
183197
return (
184198
""
@@ -218,7 +232,7 @@ def get_level_string_(node_) -> str:
218232
return subnode_dict
219233

220234
elif isinstance(node, SDocDocument):
221-
node_dict: Dict[str, List[Dict]] = {JSONKey.NODES: []}
235+
node_dict: Dict[str, JSONType] = {JSONKey.NODES: []}
222236

223237
current_number = 0
224238
for subnode_ in node.section_contents:
@@ -243,9 +257,9 @@ def get_level_string_(node_) -> str:
243257
@classmethod
244258
def _write_section(
245259
cls, section: SDocSection, document: SDocDocument, level_string: str
246-
) -> Dict:
260+
) -> Dict[str, JSONType]:
247261
assert isinstance(section, (SDocSection, SDocDocument))
248-
node_dict: Dict[str, Any] = {
262+
node_dict: Dict[str, JSONType] = {
249263
"_TOC": level_string,
250264
"TYPE": "SECTION",
251265
"TITLE": str(section.title),
@@ -272,8 +286,8 @@ def _write_section(
272286
@classmethod
273287
def _write_requirement(
274288
cls, node: SDocNode, document: SDocDocument, level_string: str
275-
) -> Dict:
276-
node_dict: Dict[str, Any] = {
289+
) -> Dict[str, JSONType]:
290+
node_dict: Dict[str, JSONType] = {
277291
"_TOC": level_string,
278292
"TYPE": node.node_type,
279293
}
@@ -297,7 +311,7 @@ def _write_requirement(
297311
return node_dict
298312

299313
@classmethod
300-
def _write_grammar_field_type(cls, grammar_field) -> Dict:
314+
def _write_grammar_field_type(cls, grammar_field) -> Dict[str, JSONType]:
301315
grammar_field_dict = {
302316
"TITLE": grammar_field.title,
303317
"REQUIRED": True if grammar_field.required else False,
@@ -307,8 +321,8 @@ def _write_grammar_field_type(cls, grammar_field) -> Dict:
307321
return grammar_field_dict
308322

309323
@staticmethod
310-
def _write_requirement_relations(node: SDocNode) -> List:
311-
relations_list = []
324+
def _write_requirement_relations(node: SDocNode) -> Sequence[JSONType]:
325+
relations_list: Sequence[JSONType] = []
312326

313327
reference: Reference
314328
for reference in node.relations:

0 commit comments

Comments
 (0)