11"""TopicList module for OpenProficiency library."""
22
33import json
4- from datetime import datetime
5- from typing import Optional , Dict , Any , Union
4+ from typing import Dict , Any , Union , List , cast
65from .Topic import Topic
76
87
@@ -15,7 +14,7 @@ def __init__(
1514 owner : str ,
1615 name : str ,
1716 # Optional
18- description : str = ""
17+ description : str = "" ,
1918 ):
2019 # Required
2120 self .owner = owner
@@ -65,7 +64,7 @@ def from_json(cls, json_data: str) -> "TopicList":
6564
6665 # Verify input is json string
6766 try :
68- data = json .loads (json_data )
67+ data = cast ( Dict [ str , Any ], json .loads (json_data ) )
6968 except TypeError :
7069 raise TypeError ("Unable to import. 'json_data' must be a JSON string" )
7170 except Exception as e :
@@ -79,7 +78,7 @@ def from_json(cls, json_data: str) -> "TopicList":
7978 )
8079
8180 # Add each topic
82- topics = data .get ("topics" , {})
81+ topics = cast ( Dict [ str , Any ], data .get ("topics" , {}) )
8382 for topic_id , topic_data in topics .items ():
8483
8584 # Find or create Topic
@@ -88,20 +87,21 @@ def from_json(cls, json_data: str) -> "TopicList":
8887 topic = topic_list .add_topic (Topic (id = topic_id ))
8988
9089 if isinstance (topic_data , dict ):
91- topic .description = topic_data .get ("description" , "" )
90+ topic_dict = cast (Dict [str , Any ], topic_data )
91+ topic .description = topic_dict .get ("description" , "" )
9292
9393 # Add subtopics
9494 cls ._add_subtopics_recursive (
9595 topic_list = topic_list ,
9696 parent_topic = topic ,
97- subtopics = topic_data .get ("subtopics" , []),
97+ subtopics = cast ( List [ Any ], topic_dict .get ("subtopics" , []) ),
9898 )
9999
100100 # Add pretopics
101101 cls ._add_pretopics_recursive (
102102 topic_list = topic_list ,
103103 child_topic = topic ,
104- pretopics = topic_data .get ("pretopics" , []),
104+ pretopics = cast ( List [ Any ], topic_dict .get ("pretopics" , []) ),
105105 )
106106
107107 else :
@@ -113,13 +113,13 @@ def from_json(cls, json_data: str) -> "TopicList":
113113 def _add_subtopics_recursive (
114114 topic_list : "TopicList" ,
115115 parent_topic : Topic ,
116- subtopics : list ,
116+ subtopics : List [ Any ] ,
117117 ) -> None :
118118 """
119119 Process subtopics and add them to the topic list.
120120 Handles nested subtopics at any depth using an iterative approach.
121121 """
122- stack = [(subtopics , parent_topic )]
122+ stack : List [ tuple [ List [ Any ], Topic ]] = [(subtopics , parent_topic )]
123123
124124 while stack :
125125 current_subtopics , current_parent = stack .pop ()
@@ -136,16 +136,17 @@ def _add_subtopics_recursive(
136136
137137 # Handle dictionary with id and optional nested subtopics
138138 elif isinstance (subtopic_object , dict ) and "id" in subtopic_object :
139+ subtopic_dict = cast (Dict [str , Any ], subtopic_object )
139140 # Check if the topic already exists
140- subtopic = topic_list .get_topic (subtopic_object ["id" ])
141+ subtopic = topic_list .get_topic (subtopic_dict ["id" ])
141142 if subtopic is None :
142143 subtopic = Topic (
143- id = subtopic_object ["id" ],
144- description = subtopic_object .get ("description" , "" ),
144+ id = subtopic_dict ["id" ],
145+ description = subtopic_dict .get ("description" , "" ),
145146 )
146147
147148 # Queue nested subtopics for processing
148- nested_subtopics = subtopic_object .get ("subtopics" , [])
149+ nested_subtopics = cast ( List [ Any ], subtopic_dict .get ("subtopics" , []) )
149150 if nested_subtopics :
150151 stack .append ((nested_subtopics , subtopic ))
151152
@@ -158,14 +159,14 @@ def _add_subtopics_recursive(
158159 def _add_pretopics_recursive (
159160 topic_list : "TopicList" ,
160161 child_topic : Topic ,
161- pretopics : list ,
162+ pretopics : List [ Any ] ,
162163 ) -> None :
163164 """
164165 Process pretopics and add them to the topic list.
165166 Handles nested pretopics at any depth using an iterative approach.
166167 Pretopics inherit description from child topic if not explicitly set.
167168 """
168- stack = [(pretopics , child_topic )]
169+ stack : List [ tuple [ List [ Any ], Topic ]] = [(pretopics , child_topic )]
169170
170171 while stack :
171172 current_pretopics , current_child = stack .pop ()
@@ -178,24 +179,21 @@ def _add_pretopics_recursive(
178179 # Check if the topic already exists
179180 pretopic = topic_list .get_topic (pretopic_object )
180181 if pretopic is None :
181- pretopic = Topic (
182- id = pretopic_object , description = current_child .description
183- )
182+ pretopic = Topic (id = pretopic_object , description = current_child .description )
184183
185184 # Handle dictionary with id and optional nested pretopics
186185 elif isinstance (pretopic_object , dict ) and "id" in pretopic_object :
186+ pretopic_dict = cast (Dict [str , Any ], pretopic_object )
187187 # Check if the topic already exists
188- pretopic = topic_list .get_topic (pretopic_object ["id" ])
188+ pretopic = topic_list .get_topic (pretopic_dict ["id" ])
189189 if pretopic is None :
190190 pretopic = Topic (
191- id = pretopic_object ["id" ],
192- description = pretopic_object .get (
193- "description" , current_child .description
194- ),
191+ id = pretopic_dict ["id" ],
192+ description = pretopic_dict .get ("description" , current_child .description ),
195193 )
196194
197195 # Queue nested pretopics for processing
198- nested_pretopics = pretopic_object .get ("pretopics" , [])
196+ nested_pretopics = cast ( List [ Any ], pretopic_dict .get ("pretopics" , []) )
199197 if nested_pretopics :
200198 stack .append ((nested_pretopics , pretopic ))
201199
@@ -204,9 +202,9 @@ def _add_pretopics_recursive(
204202 topic_list .add_topic (pretopic )
205203 current_child .add_pretopic (pretopic )
206204
207- def to_dict (self ) -> dict :
205+ def to_dict (self ) -> Dict [ str , Any ] :
208206 """
209- Export the TopicList to a JSON string .
207+ Export the TopicList to a dictionary .
210208 """
211209
212210 # Create dictionary
0 commit comments