@@ -13,15 +13,23 @@ class Code(BaseCommand):
1313 """A parsed representation of a generic G/M/T-code"""
1414
1515 @classmethod
16- def from_json (cls , data ) :
16+ def from_json (cls , data : dict [ str , object ] | str ) -> "Code" :
1717 """Deserialize an instance of this class from JSON deserialized dictionary"""
18- data ["result" ] = None if data ["result" ] is None else list (map (Message .from_json , data ["result" ]))
19- data ["parameters" ] = list (map (CodeParameter .from_json , data ["parameters" ]))
18+ if isinstance (data , str ):
19+ raise TypeError ("Code.from_json expects a decoded JSON dictionary" )
20+ data = dict (data )
21+ result = data .get ("result" )
22+ parameters = data .get ("parameters" )
23+ data ["result" ] = None if not isinstance (result , list ) else [Message .from_json (item )
24+ for item in result if isinstance (item , dict )]
25+ data ["parameters" ] = [] if not isinstance (parameters , list ) else [CodeParameter .from_json (
26+ item ) for item in parameters if isinstance (item , dict )]
2027 if "channel" in data :
2128 data ["channel" ] = CodeChannel (data ["channel" ])
22- return cls (** data )
29+ command = str (data .pop ("command" , "" ))
30+ return cls (command = command , ** data )
2331
24- def __init__ (self , ** kwargs ):
32+ def __init__ (self , command : str = "" , ** kwargs : object ):
2533 # The connection ID this code was received from. If this is 0, the code originates from an internal DCS task
2634 # Usually there is no need to populate this property.
2735 # It is internally overwritten by the control server on receipt
@@ -73,14 +81,14 @@ def __init__(self, **kwargs):
7381 # List of parsed code parameters
7482 self .parameters : List [CodeParameter ] = []
7583
76- super ().__init__ (** kwargs )
84+ super ().__init__ (command = command , ** kwargs )
7785
7886 @property
7987 def is_from_file_channel (self ) -> bool :
8088 """Check if this code is from a file channel"""
8189 return self .channel is CodeChannel .File or self .channel is CodeChannel .File2
8290
83- def parameter (self , letter : str , default = None ):
91+ def parameter (self , letter : str , default : object = None ) -> CodeParameter | None :
8492 """Retrieve the parameter whose letter equals c or generate a default parameter"""
8593 letter = letter .upper ()
8694 param = [param for param in self .parameters if param .letter .upper () == letter ]
@@ -90,7 +98,7 @@ def parameter(self, letter: str, default=None):
9098 return CodeParameter .simple_param (letter , default )
9199 return None
92100
93- def get_unprecedented_string (self , quote : bool = False ):
101+ def get_unprecedented_string (self , quote : bool = False ) -> str :
94102 """
95103 Reconstruct an unprecedented string from the parameter list or
96104 retrieve the parameter which does not have a letter assigned.
@@ -103,7 +111,7 @@ def get_unprecedented_string(self, quote: bool = False):
103111 str_list .append (f"{ param .letter } { param .string_value } " )
104112 return " " .join (str_list )
105113
106- def __str__ (self ):
114+ def __str__ (self ) -> str :
107115 """Convert the parsed code back to a text-based G/M/T-code"""
108116 if self .keyword != KeywordType .KeywordNone :
109117 if self .keywordArgument is not None :
@@ -129,7 +137,7 @@ def __str__(self):
129137
130138 return "" .join (str_list )
131139
132- def short_str (self ):
140+ def short_str (self ) -> str :
133141 """Convert only the command portion to a text-based G/M/T-code (e.g. G28)"""
134142 if self .type == CodeType .Comment :
135143 return "(comment)"
@@ -143,7 +151,7 @@ def short_str(self):
143151
144152 return f"{ prefix } { self .type } "
145153
146- def keyword_to_str (self ):
154+ def keyword_to_str (self ) -> str :
147155 """Convert the keyword to a string"""
148156 return {
149157 KeywordType .If : "if" ,
@@ -157,7 +165,7 @@ def keyword_to_str(self):
157165 KeywordType .Set : "set" ,
158166 KeywordType .Echo : "echo" ,
159167 KeywordType .Global : "global" ,
160- }. get ( self .keyword )
168+ }[ self .keyword ]
161169
162- def is_flag_set (self , flag : CodeFlags ):
170+ def is_flag_set (self , flag : CodeFlags ) -> bool :
163171 return self .flags & flag != 0
0 commit comments