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"
2
2
import json
3
3
import os .path
4
4
from enum import Enum
5
- from typing import Any , Dict , List , Optional , Tuple
5
+ from typing import Any , Dict , List , Optional , Sequence , Tuple , Union
6
6
7
7
from strictdoc .backend .sdoc .models .document import SDocDocument
8
8
from strictdoc .backend .sdoc .models .document_config import DocumentConfig
17
17
)
18
18
from strictdoc .backend .sdoc .models .section import SDocSection
19
19
from strictdoc .backend .sdoc .models .type_system import (
20
+ GrammarElementRelationChild ,
21
+ GrammarElementRelationFile ,
22
+ GrammarElementRelationParent ,
20
23
RequirementFieldType ,
21
24
)
22
25
from strictdoc .core .project_config import ProjectConfig
23
26
from strictdoc .core .traceability_index import TraceabilityIndex
24
27
28
+ JSONPrimitive = Union [str , int , float , bool , None ]
29
+ JSONType = Union [JSONPrimitive , Dict [str , "JSONType" ], Sequence ["JSONType" ]]
30
+
25
31
26
32
class TAG (Enum ):
27
33
SECTION = 1
@@ -55,7 +61,7 @@ def export_tree(
55
61
if document_ .document_is_included ():
56
62
continue
57
63
58
- document_json_dict = self ._write_document (document_ )
64
+ document_json_dict : JSONType = self ._write_document (document_ )
59
65
60
66
project_tree_dict ["DOCUMENTS" ].append (document_json_dict )
61
67
@@ -65,7 +71,7 @@ def export_tree(
65
71
output_json_file .write (project_tree_json )
66
72
67
73
@classmethod
68
- def _write_document (cls , document : SDocDocument ) -> Dict :
74
+ def _write_document (cls , document : SDocDocument ) -> Dict [ str , JSONType ] :
69
75
document_dict : Dict [str , Any ] = {
70
76
"TITLE" : document .title ,
71
77
"REQ_PREFIX" : None ,
@@ -160,7 +166,13 @@ def _write_document(cls, document: SDocDocument) -> Dict:
160
166
cls ._write_grammar_field_type (grammar_field )
161
167
)
162
168
163
- relations : List = element_ .relations
169
+ relations : List [
170
+ Union [
171
+ GrammarElementRelationParent ,
172
+ GrammarElementRelationChild ,
173
+ GrammarElementRelationFile ,
174
+ ]
175
+ ] = element_ .relations
164
176
if len (relations ) > 0 :
165
177
for element_relation_ in relations :
166
178
relation_dict = {
@@ -178,7 +190,9 @@ def _write_document(cls, document: SDocDocument) -> Dict:
178
190
return document_dict
179
191
180
192
@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 ]:
182
196
def get_level_string_ (node_ ) -> str :
183
197
return (
184
198
""
@@ -218,7 +232,7 @@ def get_level_string_(node_) -> str:
218
232
return subnode_dict
219
233
220
234
elif isinstance (node , SDocDocument ):
221
- node_dict : Dict [str , List [ Dict ] ] = {JSONKey .NODES : []}
235
+ node_dict : Dict [str , JSONType ] = {JSONKey .NODES : []}
222
236
223
237
current_number = 0
224
238
for subnode_ in node .section_contents :
@@ -243,9 +257,9 @@ def get_level_string_(node_) -> str:
243
257
@classmethod
244
258
def _write_section (
245
259
cls , section : SDocSection , document : SDocDocument , level_string : str
246
- ) -> Dict :
260
+ ) -> Dict [ str , JSONType ] :
247
261
assert isinstance (section , (SDocSection , SDocDocument ))
248
- node_dict : Dict [str , Any ] = {
262
+ node_dict : Dict [str , JSONType ] = {
249
263
"_TOC" : level_string ,
250
264
"TYPE" : "SECTION" ,
251
265
"TITLE" : str (section .title ),
@@ -272,8 +286,8 @@ def _write_section(
272
286
@classmethod
273
287
def _write_requirement (
274
288
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 ] = {
277
291
"_TOC" : level_string ,
278
292
"TYPE" : node .node_type ,
279
293
}
@@ -297,7 +311,7 @@ def _write_requirement(
297
311
return node_dict
298
312
299
313
@classmethod
300
- def _write_grammar_field_type (cls , grammar_field ) -> Dict :
314
+ def _write_grammar_field_type (cls , grammar_field ) -> Dict [ str , JSONType ] :
301
315
grammar_field_dict = {
302
316
"TITLE" : grammar_field .title ,
303
317
"REQUIRED" : True if grammar_field .required else False ,
@@ -307,8 +321,8 @@ def _write_grammar_field_type(cls, grammar_field) -> Dict:
307
321
return grammar_field_dict
308
322
309
323
@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 ] = []
312
326
313
327
reference : Reference
314
328
for reference in node .relations :
0 commit comments