Skip to content

Commit 81caa87

Browse files
authored
Merge pull request #2270 from strictdoc-project/stanislaw/code-climate/address-type-arg
code-climate: mypy: address type-arg: JSON generator
2 parents 2282e39 + 4842e38 commit 81caa87

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

strictdoc/export/json/json_generator.py

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# mypy: disable-error-code="arg-type,attr-defined,no-untyped-def,type-arg,union-attr,operator"
21
import json
32
import os.path
43
from enum import Enum
5-
from typing import Any, Dict, List, Optional, Tuple
4+
from typing import Any, Dict, List, Tuple, Union
65

76
from strictdoc.backend.sdoc.models.document import SDocDocument
87
from strictdoc.backend.sdoc.models.document_config import DocumentConfig
98
from strictdoc.backend.sdoc.models.document_from_file import DocumentFromFile
109
from strictdoc.backend.sdoc.models.document_grammar import DocumentGrammar
10+
from strictdoc.backend.sdoc.models.model import SDocElementIF
1111
from strictdoc.backend.sdoc.models.node import SDocNode
1212
from strictdoc.backend.sdoc.models.reference import (
1313
ChildReqReference,
@@ -17,10 +17,13 @@
1717
)
1818
from strictdoc.backend.sdoc.models.section import SDocSection
1919
from strictdoc.backend.sdoc.models.type_system import (
20+
GrammarElementField,
2021
RequirementFieldType,
2122
)
23+
from strictdoc.core.document_tree import DocumentTree
2224
from strictdoc.core.project_config import ProjectConfig
2325
from strictdoc.core.traceability_index import TraceabilityIndex
26+
from strictdoc.helpers.cast import assert_cast
2427

2528

2629
class TAG(Enum):
@@ -42,15 +45,18 @@ def export_tree(
4245
traceability_index: TraceabilityIndex,
4346
project_config: ProjectConfig,
4447
output_json_root: str,
45-
):
46-
project_tree_dict = {
48+
) -> None:
49+
project_tree_dict: Dict[str, Any] = {
4750
"_COMMENT": (
4851
"Fields with _ are metadata. "
4952
"Fields without _ are the actual document/section/requirement/other content."
5053
),
5154
"DOCUMENTS": [],
5255
}
53-
for document_ in traceability_index.document_tree.document_list:
56+
document_tree: DocumentTree = assert_cast(
57+
traceability_index.document_tree, DocumentTree
58+
)
59+
for document_ in document_tree.document_list:
5460
if not project_config.export_included_documents:
5561
if document_.document_is_included():
5662
continue
@@ -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, Any]:
6975
document_dict: Dict[str, Any] = {
7076
"TITLE": document.title,
7177
"REQ_PREFIX": None,
@@ -149,7 +155,7 @@ def _write_document(cls, document: SDocDocument) -> Dict:
149155
document_grammar: DocumentGrammar = document.grammar
150156

151157
for element_ in document_grammar.elements:
152-
element_dict = {
158+
element_dict: Dict[str, Any] = {
153159
"NODE_TYPE": element_.tag,
154160
"FIELDS": [],
155161
"RELATIONS": [],
@@ -160,10 +166,9 @@ def _write_document(cls, document: SDocDocument) -> Dict:
160166
cls._write_grammar_field_type(grammar_field)
161167
)
162168

163-
relations: List = element_.relations
164-
if len(relations) > 0:
165-
for element_relation_ in relations:
166-
relation_dict = {
169+
if len(element_.relations) > 0:
170+
for element_relation_ in element_.relations:
171+
relation_dict: Dict[str, str] = {
167172
"TYPE": element_relation_.relation_type,
168173
}
169174
if element_relation_.relation_role is not None:
@@ -178,8 +183,15 @@ def _write_document(cls, document: SDocDocument) -> Dict:
178183
return document_dict
179184

180185
@classmethod
181-
def _write_node(cls, node, document, level_stack: Optional[Tuple]) -> Dict:
182-
def get_level_string_(node_) -> str:
186+
def _write_node(
187+
cls,
188+
node: SDocElementIF,
189+
document: SDocDocument,
190+
level_stack: Tuple[int, ...],
191+
) -> Dict[str, Any]:
192+
def get_level_string_(
193+
node_: Union[SDocNode, SDocSection, SDocDocument],
194+
) -> str:
183195
return (
184196
""
185197
if node_.ng_resolved_custom_level == "None"
@@ -190,8 +202,8 @@ def get_level_string_(node_) -> str:
190202
)
191203
)
192204

193-
if isinstance(node, (SDocSection, SDocDocument)):
194-
section_dict = cls._write_section(
205+
if isinstance(node, SDocSection):
206+
section_dict: Dict[str, Any] = cls._write_section(
195207
node, document, get_level_string_(node)
196208
)
197209

@@ -200,10 +212,10 @@ def get_level_string_(node_) -> str:
200212
if subnode_.ng_resolved_custom_level is None:
201213
current_number += 1
202214

203-
subnode_dict = cls._write_node(
215+
section_subnode_dict: Dict[str, Any] = cls._write_node(
204216
subnode_, document, level_stack + (current_number,)
205217
)
206-
section_dict[JSONKey.NODES].append(subnode_dict)
218+
section_dict[JSONKey.NODES].append(section_subnode_dict)
207219

208220
return section_dict
209221

@@ -218,32 +230,49 @@ def get_level_string_(node_) -> str:
218230
return subnode_dict
219231

220232
elif isinstance(node, SDocDocument):
221-
node_dict: Dict[str, List[Dict]] = {JSONKey.NODES: []}
233+
node_dict: Dict[str, Any] = cls._write_included_document(
234+
node, get_level_string_(node)
235+
)
222236

223237
current_number = 0
224238
for subnode_ in node.section_contents:
225239
if subnode_.ng_resolved_custom_level is None:
226240
current_number += 1
227-
subnode_dict = cls._write_node(
241+
document_subnode_dict: Dict[str, Any] = cls._write_node(
228242
subnode_, document, level_stack + (current_number,)
229243
)
230-
node_dict[JSONKey.NODES].append(subnode_dict)
244+
node_dict[JSONKey.NODES].append(document_subnode_dict)
231245

232246
return node_dict
233247

234248
elif isinstance(node, DocumentFromFile):
249+
resolved_document = assert_cast(
250+
node.resolved_document, SDocDocument
251+
)
235252
subnode_dict = cls._write_node(
236-
node.resolved_document, document, level_stack
253+
resolved_document, document, level_stack
237254
)
238255
return subnode_dict
239256

240257
else:
241258
raise NotImplementedError # pragma: no cover
242259

260+
@classmethod
261+
def _write_included_document(
262+
cls, node: SDocDocument, level_string: str
263+
) -> Dict[str, Any]:
264+
node_dict: Dict[str, Any] = {
265+
"_TOC": level_string,
266+
"TYPE": "SECTION",
267+
"TITLE": node.reserved_title,
268+
JSONKey.NODES: [],
269+
}
270+
return node_dict
271+
243272
@classmethod
244273
def _write_section(
245274
cls, section: SDocSection, document: SDocDocument, level_string: str
246-
) -> Dict:
275+
) -> Dict[str, Any]:
247276
assert isinstance(section, (SDocSection, SDocDocument))
248277
node_dict: Dict[str, Any] = {
249278
"_TOC": level_string,
@@ -272,7 +301,7 @@ def _write_section(
272301
@classmethod
273302
def _write_requirement(
274303
cls, node: SDocNode, document: SDocDocument, level_string: str
275-
) -> Dict:
304+
) -> Dict[str, Any]:
276305
node_dict: Dict[str, Any] = {
277306
"_TOC": level_string,
278307
"TYPE": node.node_type,
@@ -281,7 +310,8 @@ def _write_requirement(
281310
if node.mid_permanent or document.config.enable_mid:
282311
node_dict["MID"] = node.reserved_mid
283312

284-
element = document.grammar.elements_by_type[node.node_type]
313+
document_grammar = assert_cast(document.grammar, DocumentGrammar)
314+
element = document_grammar.elements_by_type[node.node_type]
285315

286316
for element_field in element.fields:
287317
field_name = element_field.title
@@ -297,17 +327,19 @@ def _write_requirement(
297327
return node_dict
298328

299329
@classmethod
300-
def _write_grammar_field_type(cls, grammar_field) -> Dict:
330+
def _write_grammar_field_type(
331+
cls, grammar_field: GrammarElementField
332+
) -> Dict[str, str]:
301333
grammar_field_dict = {
302334
"TITLE": grammar_field.title,
303-
"REQUIRED": True if grammar_field.required else False,
335+
"REQUIRED": "True" if grammar_field.required else "False",
304336
# FIXME: Support more grammar types.
305337
"TYPE": RequirementFieldType.STRING,
306338
}
307339
return grammar_field_dict
308340

309341
@staticmethod
310-
def _write_requirement_relations(node: SDocNode) -> List:
342+
def _write_requirement_relations(node: SDocNode) -> List[Dict[str, str]]:
311343
relations_list = []
312344

313345
reference: Reference

0 commit comments

Comments
 (0)